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 
17 #include <algorithm>
18 
21 
22 /*
23  The following types can be used to direct pybind11-generated docstrings
24  to have have more explicit types (e.g., `list[str]` instead of `list`).
25  Just use these in place of existing types.
26 
27  There is no additional enforcement of types at runtime.
28 */
29 
30 template <typename... Types>
31 class Tuple : public tuple {
32  using tuple::tuple;
33 };
34 
35 template <typename K, typename V>
36 class Dict : public dict {
37  using dict::dict;
38 };
39 
40 template <typename T>
41 class List : public list {
42  using list::list;
43 };
44 
45 template <typename T>
46 class Set : public set {
47  using set::set;
48 };
49 
50 template <typename T>
51 class Iterable : public iterable {
52  using iterable::iterable;
53 };
54 
55 template <typename T>
56 class Iterator : public iterator {
57  using iterator::iterator;
58 };
59 
60 template <typename Signature>
61 class Callable;
62 
63 template <typename Return, typename... Args>
64 class Callable<Return(Args...)> : public function {
65  using function::function;
66 };
67 
68 template <typename T>
69 class Type : public type {
70  using type::type;
71 };
72 
73 template <typename... Types>
74 class Union : public object {
75  PYBIND11_OBJECT_DEFAULT(Union, object, PyObject_Type)
76  using object::object;
77 };
78 
79 template <typename T>
80 class Optional : public object {
81  PYBIND11_OBJECT_DEFAULT(Optional, object, PyObject_Type)
82  using object::object;
83 };
84 
85 template <typename T>
86 class TypeGuard : public bool_ {
87  using bool_::bool_;
88 };
89 
90 template <typename T>
91 class TypeIs : public bool_ {
92  using bool_::bool_;
93 };
94 
95 class NoReturn : public none {
96  using none::none;
97 };
98 
99 class Never : public none {
100  using none::none;
101 };
102 
103 #if defined(__cpp_nontype_template_args) && __cpp_nontype_template_args >= 201911L
104 # define PYBIND11_TYPING_H_HAS_STRING_LITERAL
105 template <size_t N>
106 struct StringLiteral {
107  constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); }
108  char name[N];
109 };
110 
111 template <StringLiteral... StrLits>
112 class Literal : public object {
113  PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type)
114 };
115 
116 // Example syntax for creating a TypeVar.
117 // typedef typing::TypeVar<"T"> TypeVarT;
118 template <StringLiteral>
119 class TypeVar : public object {
120  PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type)
121  using object::object;
122 };
123 #endif
124 
126 
128 
129 template <typename... Types>
130 struct handle_type_name<typing::Tuple<Types...>> {
131  static constexpr auto name = const_name("tuple[")
133  + const_name("]");
134 };
135 
136 template <>
137 struct handle_type_name<typing::Tuple<>> {
138  // PEP 484 specifies this syntax for an empty tuple
139  static constexpr auto name = const_name("tuple[()]");
140 };
141 
142 template <typename T>
143 struct handle_type_name<typing::Tuple<T, ellipsis>> {
144  // PEP 484 specifies this syntax for a variable-length tuple
145  static constexpr auto name
146  = const_name("tuple[") + make_caster<T>::name + const_name(", ...]");
147 };
148 
149 template <typename K, typename V>
150 struct handle_type_name<typing::Dict<K, V>> {
151  static constexpr auto name = const_name("dict[") + make_caster<K>::name + const_name(", ")
153 };
154 
155 template <typename T>
156 struct handle_type_name<typing::List<T>> {
157  static constexpr auto name = const_name("list[") + make_caster<T>::name + const_name("]");
158 };
159 
160 template <typename T>
161 struct handle_type_name<typing::Set<T>> {
162  static constexpr auto name = const_name("set[") + make_caster<T>::name + const_name("]");
163 };
164 
165 template <typename T>
166 struct handle_type_name<typing::Iterable<T>> {
167  static constexpr auto name = const_name("Iterable[") + make_caster<T>::name + const_name("]");
168 };
169 
170 template <typename T>
171 struct handle_type_name<typing::Iterator<T>> {
172  static constexpr auto name = const_name("Iterator[") + make_caster<T>::name + const_name("]");
173 };
174 
175 template <typename Return, typename... Args>
176 struct handle_type_name<typing::Callable<Return(Args...)>> {
178  static constexpr auto name
181 };
182 
183 template <typename Return>
184 struct handle_type_name<typing::Callable<Return(ellipsis)>> {
185  // PEP 484 specifies this syntax for defining only return types of callables
187  static constexpr auto name
189 };
190 
191 template <typename T>
192 struct handle_type_name<typing::Type<T>> {
193  static constexpr auto name = const_name("type[") + make_caster<T>::name + const_name("]");
194 };
195 
196 template <typename... Types>
197 struct handle_type_name<typing::Union<Types...>> {
198  static constexpr auto name = const_name("Union[")
200  + const_name("]");
201 };
202 
203 template <typename T>
204 struct handle_type_name<typing::Optional<T>> {
205  static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
206 };
207 
208 template <typename T>
209 struct handle_type_name<typing::TypeGuard<T>> {
210  static constexpr auto name = const_name("TypeGuard[") + make_caster<T>::name + const_name("]");
211 };
212 
213 template <typename T>
214 struct handle_type_name<typing::TypeIs<T>> {
215  static constexpr auto name = const_name("TypeIs[") + make_caster<T>::name + const_name("]");
216 };
217 
218 template <>
219 struct handle_type_name<typing::NoReturn> {
220  static constexpr auto name = const_name("NoReturn");
221 };
222 
223 template <>
224 struct handle_type_name<typing::Never> {
225  static constexpr auto name = const_name("Never");
226 };
227 
228 #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
229 template <typing::StringLiteral... Literals>
230 struct handle_type_name<typing::Literal<Literals...>> {
231  static constexpr auto name = const_name("Literal[")
232  + pybind11::detail::concat(const_name(Literals.name)...)
233  + const_name("]");
234 };
235 template <typing::StringLiteral StrLit>
236 struct handle_type_name<typing::TypeVar<StrLit>> {
237  static constexpr auto name = const_name(StrLit.name);
238 };
239 #endif
240 
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:186
Set
Definition: typing.h:46
name
Annotation for function names.
Definition: attr.h:51
cast.h
set
Definition: pytypes.h:2234
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:2168
Dict
Definition: typing.h:36
NoReturn
Definition: typing.h:95
TypeGuard
Definition: typing.h:86
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
type
Definition: pytypes.h:1527
detail
Definition: testSerializationNonlinear.cpp:69
Iterator
Definition: typing.h:56
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
iterator
Definition: pytypes.h:1462
Never
Definition: typing.h:99
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:675
bool_
Definition: pytypes.h:1800
object
Definition: pytypes.h:364
dict
Definition: pytypes.h:2109
Type
Definition: typing.h:69
type_caster
Definition: cast.h:38
tuple::tuple
tuple(SzType size=0)
Definition: pytypes.h:2085
object::object
object()=default
none::none
none()
Definition: pytypes.h:1791
Union
Definition: typing.h:74
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:1803
pytypes.h
str
Definition: pytypes.h:1560
PYBIND11_OBJECT_DEFAULT
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
Definition: pytypes.h:1447
handle_type_name
Definition: cast.h:901
iterable
Definition: pytypes.h:1553
list::list
list(SzType size=0)
Definition: pytypes.h:2174
void_type
Helper type to replace 'void' in some expressions.
Definition: wrap/pybind11/include/pybind11/detail/common.h:834
Iterable
Definition: typing.h:51
tuple
Definition: pytypes.h:2079
TypeIs
Definition: typing.h:91
function
Definition: pytypes.h:2254
N
#define N
Definition: igam.h:9
List
Definition: typing.h:41
set::set
set()
Definition: pytypes.h:2237
Tuple
Definition: typing.h:31
none
Definition: pytypes.h:1788
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:177
Optional
Definition: typing.h:80
dict::dict
dict()
Definition: pytypes.h:2112
Callable
Definition: typing.h:61


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:08:57