|
| 1 | +/* |
| 2 | + pybind11/typing.h: Convenience wrapper classes for basic Python types |
| 3 | + with more explicit annotations. |
| 4 | +
|
| 5 | + Copyright (c) 2023 Dustin Spicuzza <[email protected]> |
| 6 | +
|
| 7 | + All rights reserved. Use of this source code is governed by a |
| 8 | + BSD-style license that can be found in the LICENSE file. |
| 9 | +*/ |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include "detail/common.h" |
| 14 | +#include "cast.h" |
| 15 | +#include "pytypes.h" |
| 16 | + |
| 17 | +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
| 18 | +PYBIND11_NAMESPACE_BEGIN(typing) |
| 19 | + |
| 20 | +/* |
| 21 | + The following types can be used to direct pybind11-generated docstrings |
| 22 | + to have have more explicit types (e.g., `list[str]` instead of `list`). |
| 23 | + Just use these in place of existing types. |
| 24 | +
|
| 25 | + There is no additional enforcement of types at runtime. |
| 26 | +*/ |
| 27 | + |
| 28 | +template <typename... Types> |
| 29 | +class Tuple : public tuple { |
| 30 | + using tuple::tuple; |
| 31 | +}; |
| 32 | + |
| 33 | +template <typename K, typename V> |
| 34 | +class Dict : public dict { |
| 35 | + using dict::dict; |
| 36 | +}; |
| 37 | + |
| 38 | +template <typename T> |
| 39 | +class List : public list { |
| 40 | + using list::list; |
| 41 | +}; |
| 42 | + |
| 43 | +template <typename T> |
| 44 | +class Set : public set { |
| 45 | + using set::set; |
| 46 | +}; |
| 47 | + |
| 48 | +template <typename Signature> |
| 49 | +class Callable; |
| 50 | + |
| 51 | +template <typename Return, typename... Args> |
| 52 | +class Callable<Return(Args...)> : public function { |
| 53 | + using function::function; |
| 54 | +}; |
| 55 | + |
| 56 | +PYBIND11_NAMESPACE_END(typing) |
| 57 | + |
| 58 | +PYBIND11_NAMESPACE_BEGIN(detail) |
| 59 | + |
| 60 | +template <typename... Types> |
| 61 | +struct handle_type_name<typing::Tuple<Types...>> { |
| 62 | + static constexpr auto name |
| 63 | + = const_name("tuple[") + concat(make_caster<Types>::name...) + const_name("]"); |
| 64 | +}; |
| 65 | + |
| 66 | +template <> |
| 67 | +struct handle_type_name<typing::Tuple<>> { |
| 68 | + // PEP 484 specifies this syntax for an empty tuple |
| 69 | + static constexpr auto name = const_name("tuple[()]"); |
| 70 | +}; |
| 71 | + |
| 72 | +template <typename K, typename V> |
| 73 | +struct handle_type_name<typing::Dict<K, V>> { |
| 74 | + static constexpr auto name = const_name("dict[") + make_caster<K>::name + const_name(", ") |
| 75 | + + make_caster<V>::name + const_name("]"); |
| 76 | +}; |
| 77 | + |
| 78 | +template <typename T> |
| 79 | +struct handle_type_name<typing::List<T>> { |
| 80 | + static constexpr auto name = const_name("list[") + make_caster<T>::name + const_name("]"); |
| 81 | +}; |
| 82 | + |
| 83 | +template <typename T> |
| 84 | +struct handle_type_name<typing::Set<T>> { |
| 85 | + static constexpr auto name = const_name("set[") + make_caster<T>::name + const_name("]"); |
| 86 | +}; |
| 87 | + |
| 88 | +template <typename Return, typename... Args> |
| 89 | +struct handle_type_name<typing::Callable<Return(Args...)>> { |
| 90 | + using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>; |
| 91 | + static constexpr auto name = const_name("Callable[[") + concat(make_caster<Args>::name...) |
| 92 | + + const_name("], ") + make_caster<retval_type>::name |
| 93 | + + const_name("]"); |
| 94 | +}; |
| 95 | + |
| 96 | +PYBIND11_NAMESPACE_END(detail) |
| 97 | +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |
0 commit comments