typing.h
Go to the documentation of this file.
1 /*
2  pybind11/typing.h: Convenience wrapper classes for basic Python types
3  with more explicit annotations.
4 
5  Copyright (c) 2023 Dustin Spicuzza <dustin@virtualroadside.com>
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 
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 T>
49 class Iterable : public iterable {
50  using iterable::iterable;
51 };
52 
53 template <typename T>
54 class Iterator : public iterator {
55  using iterator::iterator;
56 };
57 
58 template <typename Signature>
59 class Callable;
60 
61 template <typename Return, typename... Args>
62 class Callable<Return(Args...)> : public function {
63  using function::function;
64 };
65 
66 template <typename T>
67 class Type : public type {
68  using type::type;
69 };
70 
71 template <typename... Types>
72 class Union : public object {
73  PYBIND11_OBJECT_DEFAULT(Union, object, PyObject_Type)
74  using object::object;
75 };
76 
77 template <typename T>
78 class Optional : public object {
79  PYBIND11_OBJECT_DEFAULT(Optional, object, PyObject_Type)
80  using object::object;
81 };
82 
83 template <typename T>
84 class TypeGuard : public bool_ {
85  using bool_::bool_;
86 };
87 
88 template <typename T>
89 class TypeIs : public bool_ {
90  using bool_::bool_;
91 };
92 
93 class NoReturn : public none {
94  using none::none;
95 };
96 
97 class Never : public none {
98  using none::none;
99 };
100 
101 #if defined(__cpp_nontype_template_parameter_class)
102 template <size_t N>
103 struct StringLiteral {
104  constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); }
105  char name[N];
106 };
107 
108 template <StringLiteral... StrLits>
109 class Literal : public object {
110  PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type)
111 };
112 
113 // Example syntax for creating a TypeVar.
114 // typedef typing::TypeVar<"T"> TypeVarT;
115 template <StringLiteral>
116 class TypeVar : public object {
117  PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type)
118  using object::object;
119 };
120 #endif
121 
123 
125 
126 template <typename... Types>
127 struct handle_type_name<typing::Tuple<Types...>> {
128  static constexpr auto name = const_name("tuple[")
130  + const_name("]");
131 };
132 
133 template <>
134 struct handle_type_name<typing::Tuple<>> {
135  // PEP 484 specifies this syntax for an empty tuple
136  static constexpr auto name = const_name("tuple[()]");
137 };
138 
139 template <typename T>
140 struct handle_type_name<typing::Tuple<T, ellipsis>> {
141  // PEP 484 specifies this syntax for a variable-length tuple
142  static constexpr auto name
143  = const_name("tuple[") + make_caster<T>::name + const_name(", ...]");
144 };
145 
146 template <typename K, typename V>
147 struct handle_type_name<typing::Dict<K, V>> {
148  static constexpr auto name = const_name("dict[") + make_caster<K>::name + const_name(", ")
150 };
151 
152 template <typename T>
153 struct handle_type_name<typing::List<T>> {
154  static constexpr auto name = const_name("list[") + make_caster<T>::name + const_name("]");
155 };
156 
157 template <typename T>
158 struct handle_type_name<typing::Set<T>> {
159  static constexpr auto name = const_name("set[") + make_caster<T>::name + const_name("]");
160 };
161 
162 template <typename T>
163 struct handle_type_name<typing::Iterable<T>> {
164  static constexpr auto name = const_name("Iterable[") + make_caster<T>::name + const_name("]");
165 };
166 
167 template <typename T>
168 struct handle_type_name<typing::Iterator<T>> {
169  static constexpr auto name = const_name("Iterator[") + make_caster<T>::name + const_name("]");
170 };
171 
172 template <typename Return, typename... Args>
173 struct handle_type_name<typing::Callable<Return(Args...)>> {
175  static constexpr auto name
178 };
179 
180 template <typename Return>
181 struct handle_type_name<typing::Callable<Return(ellipsis)>> {
182  // PEP 484 specifies this syntax for defining only return types of callables
184  static constexpr auto name
186 };
187 
188 template <typename T>
189 struct handle_type_name<typing::Type<T>> {
190  static constexpr auto name = const_name("type[") + make_caster<T>::name + const_name("]");
191 };
192 
193 template <typename... Types>
194 struct handle_type_name<typing::Union<Types...>> {
195  static constexpr auto name = const_name("Union[")
197  + const_name("]");
198 };
199 
200 template <typename T>
201 struct handle_type_name<typing::Optional<T>> {
202  static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
203 };
204 
205 template <typename T>
206 struct handle_type_name<typing::TypeGuard<T>> {
207  static constexpr auto name = const_name("TypeGuard[") + make_caster<T>::name + const_name("]");
208 };
209 
210 template <typename T>
211 struct handle_type_name<typing::TypeIs<T>> {
212  static constexpr auto name = const_name("TypeIs[") + make_caster<T>::name + const_name("]");
213 };
214 
215 template <>
216 struct handle_type_name<typing::NoReturn> {
217  static constexpr auto name = const_name("NoReturn");
218 };
219 
220 template <>
221 struct handle_type_name<typing::Never> {
222  static constexpr auto name = const_name("Never");
223 };
224 
225 #if defined(__cpp_nontype_template_parameter_class)
226 template <typing::StringLiteral... Literals>
227 struct handle_type_name<typing::Literal<Literals...>> {
228  static constexpr auto name = const_name("Literal[")
229  + pybind11::detail::concat(const_name(Literals.name)...)
230  + const_name("]");
231 };
232 template <typing::StringLiteral StrLit>
233 struct handle_type_name<typing::TypeVar<StrLit>> {
234  static constexpr auto name = const_name(StrLit.name);
235 };
236 #endif
237 
handle_type_name< typing::Callable< Return(ellipsis)> >::retval_type
conditional_t< std::is_same< Return, void >::value, void_type, Return > retval_type
Definition: typing.h:183
Set
Definition: typing.h:44
name
Annotation for function names.
Definition: attr.h:51
cast.h
set
Definition: pytypes.h:2232
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
const_name
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
list
Definition: pytypes.h:2166
Dict
Definition: typing.h:34
NoReturn
Definition: typing.h:93
TypeGuard
Definition: typing.h:84
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
type
Definition: pytypes.h:1525
detail
Definition: testSerializationNonlinear.cpp:70
Iterator
Definition: typing.h:54
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
iterator
Definition: pytypes.h:1460
Never
Definition: typing.h:97
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:656
bool_
Definition: pytypes.h:1798
object
Definition: pytypes.h:364
dict
Definition: pytypes.h:2107
Type
Definition: typing.h:67
type_caster
Definition: cast.h:38
tuple::tuple
tuple(SzType size=0)
Definition: pytypes.h:2083
object::object
object()=default
none::none
none()
Definition: pytypes.h:1789
Union
Definition: typing.h:72
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
concat
constexpr descr< 0 > concat()
Definition: descr.h:139
common.h
bool_::bool_
bool_()
Definition: pytypes.h:1801
pytypes.h
str
Definition: pytypes.h:1558
PYBIND11_OBJECT_DEFAULT
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
Definition: pytypes.h:1445
handle_type_name
Definition: cast.h:894
iterable
Definition: pytypes.h:1551
list::list
list(SzType size=0)
Definition: pytypes.h:2172
void_type
Helper type to replace 'void' in some expressions.
Definition: wrap/pybind11/include/pybind11/detail/common.h:815
Iterable
Definition: typing.h:49
tuple
Definition: pytypes.h:2077
TypeIs
Definition: typing.h:89
function
Definition: pytypes.h:2252
N
#define N
Definition: igam.h:9
List
Definition: typing.h:39
set::set
set()
Definition: pytypes.h:2235
Tuple
Definition: typing.h:29
none
Definition: pytypes.h:1786
handle_type_name< typing::Callable< Return(Args...)> >::retval_type
conditional_t< std::is_same< Return, void >::value, void_type, Return > retval_type
Definition: typing.h:174
Optional
Definition: typing.h:78
dict::dict
dict()
Definition: pytypes.h:2110
Callable
Definition: typing.h:59


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:08:26