File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -80,13 +80,24 @@ class Optional : public object {
80
80
using object::object;
81
81
};
82
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
+
83
93
class NoReturn : public none {
84
94
using none::none;
85
95
};
86
96
87
97
class Never : public none {
88
98
using none::none;
89
99
};
100
+
90
101
#if defined(__cpp_nontype_template_parameter_class)
91
102
template <size_t N>
92
103
struct StringLiteral {
@@ -183,6 +194,16 @@ struct handle_type_name<typing::Optional<T>> {
183
194
static constexpr auto name = const_name(" Optional[" ) + make_caster<T>::name + const_name(" ]" );
184
195
};
185
196
197
+ template <typename T>
198
+ struct handle_type_name <typing::TypeGuard<T>> {
199
+ static constexpr auto name = const_name(" TypeGuard[" ) + make_caster<T>::name + const_name(" ]" );
200
+ };
201
+
202
+ template <typename T>
203
+ struct handle_type_name <typing::TypeIs<T>> {
204
+ static constexpr auto name = const_name(" TypeIs[" ) + make_caster<T>::name + const_name(" ]" );
205
+ };
206
+
186
207
template <>
187
208
struct handle_type_name <typing::NoReturn> {
188
209
static constexpr auto name = const_name(" NoReturn" );
@@ -192,6 +213,7 @@ template <>
192
213
struct handle_type_name <typing::Never> {
193
214
static constexpr auto name = const_name(" Never" );
194
215
};
216
+
195
217
#if defined(__cpp_nontype_template_parameter_class)
196
218
template <typing::StringLiteral... Literals>
197
219
struct handle_type_name <typing::Literal<Literals...>> {
Original file line number Diff line number Diff line change @@ -892,8 +892,15 @@ TEST_SUBMODULE(pytypes, m) {
892
892
return list;
893
893
});
894
894
895
+ m.def (" annotate_type_guard" , [](py::object &o) -> py::typing::TypeGuard<py::str> {
896
+ return py::isinstance<py::str>(o);
897
+ });
898
+ m.def (" annotate_type_is" ,
899
+ [](py::object &o) -> py::typing::TypeIs<py::str> { return py::isinstance<py::str>(o); });
900
+
895
901
m.def (" annotate_no_return" , []() -> py::typing::NoReturn { throw 0 ; });
896
902
m.def (" annotate_never" , []() -> py::typing::Never { throw 0 ; });
903
+
897
904
m.def (" annotate_optional_to_object" ,
898
905
[](py::typing::Optional<int > &o) -> py::object { return o; });
899
906
Original file line number Diff line number Diff line change @@ -991,6 +991,17 @@ def test_optional_annotations(doc):
991
991
)
992
992
993
993
994
+ def test_type_guard_annotations (doc ):
995
+ assert (
996
+ doc (m .annotate_type_guard )
997
+ == "annotate_type_guard(arg0: object) -> TypeGuard[str]"
998
+ )
999
+
1000
+
1001
+ def test_type_is_annotations (doc ):
1002
+ assert doc (m .annotate_type_is ) == "annotate_type_is(arg0: object) -> TypeIs[str]"
1003
+
1004
+
994
1005
def test_no_return_annotation (doc ):
995
1006
assert doc (m .annotate_no_return ) == "annotate_no_return() -> NoReturn"
996
1007
You can’t perform that action at this time.
0 commit comments