Skip to content

Commit 65661fe

Browse files
committed
Change detail::annotated_any() to produce pybind11.CppType(...)
Background: * python/mypy#16306 (comment) * python/mypy#16306 (comment)
1 parent 2b2ffeb commit 65661fe

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

include/pybind11/detail/type_caster_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ class type_caster_base : public type_caster_generic {
12021202
};
12031203

12041204
inline std::string annotated_any(const std::string &cpp_type) {
1205-
return "Annotated[Any, \"" + cpp_type + "\"]";
1205+
return "Annotated[Any, pybind11.CppType(\"" + cpp_type + "\")]";
12061206
}
12071207

12081208
PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti) {

tests/test_cases_for_stubgen.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@
1111
"m.basics.Point.distance_to.__doc__": "distance_to(*args, **kwargs)\nOverloaded function.\n\n1. distance_to(self: pybind11_tests.cases_for_stubgen.basics.Point, x: float, y: float) -> float\n\n2. distance_to(self: pybind11_tests.cases_for_stubgen.basics.Point, other: pybind11_tests.cases_for_stubgen.basics.Point) -> float\n",
1212
"m.basics.Point.length_unit.__doc__": "Members:\n\n mm\n\n pixel\n\n inch",
1313
"m.basics.Point.angle_unit.__doc__": "Members:\n\n radian\n\n degree",
14-
"m.pass_user_type.__doc__": 'pass_user_type(arg0: Annotated[Any, "test_cases_for_stubgen::UserType"]) -> None\n',
15-
"m.return_user_type.__doc__": 'return_user_type() -> Annotated[Any, "test_cases_for_stubgen::UserType"]\n',
14+
"m.pass_user_type.__doc__": 'pass_user_type(arg0: Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]) -> None\n',
15+
"m.return_user_type.__doc__": 'return_user_type() -> Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]\n',
1616
"m.MapIntUserType.keys.__doc__": "keys(self: pybind11_tests.cases_for_stubgen.MapIntUserType) -> pybind11_tests.cases_for_stubgen.KeysView[int]\n",
17-
"m.MapIntUserType.values.__doc__": 'values(self: pybind11_tests.cases_for_stubgen.MapIntUserType) -> pybind11_tests.cases_for_stubgen.ValuesView[Annotated[Any, "test_cases_for_stubgen::UserType"]]\n',
18-
"m.MapIntUserType.items.__doc__": 'items(self: pybind11_tests.cases_for_stubgen.MapIntUserType) -> pybind11_tests.cases_for_stubgen.ItemsView[int, Annotated[Any, "test_cases_for_stubgen::UserType"]]\n',
19-
"m.MapUserTypeInt.keys.__doc__": 'keys(self: pybind11_tests.cases_for_stubgen.MapUserTypeInt) -> pybind11_tests.cases_for_stubgen.KeysView[Annotated[Any, "test_cases_for_stubgen::UserType"]]\n',
17+
"m.MapIntUserType.values.__doc__": 'values(self: pybind11_tests.cases_for_stubgen.MapIntUserType) -> pybind11_tests.cases_for_stubgen.ValuesView[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]\n',
18+
"m.MapIntUserType.items.__doc__": 'items(self: pybind11_tests.cases_for_stubgen.MapIntUserType) -> pybind11_tests.cases_for_stubgen.ItemsView[int, Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]\n',
19+
"m.MapUserTypeInt.keys.__doc__": 'keys(self: pybind11_tests.cases_for_stubgen.MapUserTypeInt) -> pybind11_tests.cases_for_stubgen.KeysView[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]\n',
2020
"m.MapUserTypeInt.values.__doc__": "values(self: pybind11_tests.cases_for_stubgen.MapUserTypeInt) -> pybind11_tests.cases_for_stubgen.ValuesView[int]\n",
21-
"m.MapUserTypeInt.items.__doc__": 'items(self: pybind11_tests.cases_for_stubgen.MapUserTypeInt) -> pybind11_tests.cases_for_stubgen.ItemsView[Annotated[Any, "test_cases_for_stubgen::UserType"], int]\n',
21+
"m.MapUserTypeInt.items.__doc__": 'items(self: pybind11_tests.cases_for_stubgen.MapUserTypeInt) -> pybind11_tests.cases_for_stubgen.ItemsView[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")], int]\n',
2222
"m.MapFloatUserType.keys.__doc__": "keys(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[float]\n",
23-
"m.MapFloatUserType.values.__doc__": 'values(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[Annotated[Any, "test_cases_for_stubgen::UserType"]]\n',
24-
"m.MapFloatUserType.__iter__.__doc__": '__iter__(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[tuple[float, Annotated[Any, "test_cases_for_stubgen::UserType"]]]\n',
25-
"m.MapUserTypeFloat.keys.__doc__": 'keys(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[Annotated[Any, "test_cases_for_stubgen::UserType"]]\n',
23+
"m.MapFloatUserType.values.__doc__": 'values(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]\n',
24+
"m.MapFloatUserType.__iter__.__doc__": '__iter__(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[tuple[float, Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]]\n',
25+
"m.MapUserTypeFloat.keys.__doc__": 'keys(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]\n',
2626
"m.MapUserTypeFloat.values.__doc__": "values(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[float]\n",
27-
"m.MapUserTypeFloat.__iter__.__doc__": '__iter__(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[tuple[Annotated[Any, "test_cases_for_stubgen::UserType"], float]]\n',
27+
"m.MapUserTypeFloat.__iter__.__doc__": '__iter__(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[tuple[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")], float]]\n',
2828
"m.pass_std_array_int_2.__doc__": "pass_std_array_int_2(arg0: Annotated[list[int], FixedSize(2)]) -> None\n",
2929
"m.return_std_array_int_3.__doc__": "return_std_array_int_3() -> Annotated[list[int], FixedSize(3)]\n",
3030
"m.nested_case_01a.__doc__": "nested_case_01a(arg0: list[Annotated[list[int], FixedSize(2)]]) -> None\n",
31-
"m.nested_case_02a.__doc__": 'nested_case_02a(arg0: list[Annotated[Any, "test_cases_for_stubgen::UserType"]]) -> None\n',
32-
"m.nested_case_03a.__doc__": 'nested_case_03a(arg0: dict[Annotated[list[int], FixedSize(2)], Annotated[Any, "test_cases_for_stubgen::UserType"]]) -> None\n',
33-
"m.nested_case_04a.__doc__": 'nested_case_04a(arg0: dict[list[Annotated[list[int], FixedSize(2)]], list[Annotated[Any, "test_cases_for_stubgen::UserType"]]]) -> None\n',
34-
"m.nested_case_05a.__doc__": 'nested_case_05a(arg0: list[dict[list[Annotated[list[int], FixedSize(2)]], list[Annotated[Any, "test_cases_for_stubgen::UserType"]]]]) -> None\n',
35-
"m.nested_case_06a.__doc__": 'nested_case_06a(arg0: dict[dict[list[Annotated[list[int], FixedSize(2)]], list[Annotated[Any, "test_cases_for_stubgen::UserType"]]], list[dict[list[Annotated[list[int], FixedSize(2)]], list[Annotated[Any, "test_cases_for_stubgen::UserType"]]]]]) -> None\n',
31+
"m.nested_case_02a.__doc__": 'nested_case_02a(arg0: list[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]) -> None\n',
32+
"m.nested_case_03a.__doc__": 'nested_case_03a(arg0: dict[Annotated[list[int], FixedSize(2)], Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]) -> None\n',
33+
"m.nested_case_04a.__doc__": 'nested_case_04a(arg0: dict[list[Annotated[list[int], FixedSize(2)]], list[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]]) -> None\n',
34+
"m.nested_case_05a.__doc__": 'nested_case_05a(arg0: list[dict[list[Annotated[list[int], FixedSize(2)]], list[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]]]) -> None\n',
35+
"m.nested_case_06a.__doc__": 'nested_case_06a(arg0: dict[dict[list[Annotated[list[int], FixedSize(2)]], list[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]], list[dict[list[Annotated[list[int], FixedSize(2)]], list[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]]]]) -> None\n',
3636
}
3737

3838

tests/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,4 @@ def test_fn_cast_int_exception():
424424
def test_return_exception_void():
425425
with pytest.raises(TypeError) as excinfo:
426426
m.return_exception_void()
427-
assert 'Annotated[Any, "exception<void>"]' in str(excinfo.value)
427+
assert 'Annotated[Any, pybind11.CppType("exception<void>")]' in str(excinfo.value)

tests/test_numpy_dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def test_complex_array():
343343
def test_signature(doc):
344344
assert (
345345
doc(m.create_rec_nested) == "create_rec_nested(arg0: int) "
346-
'-> numpy.ndarray[Annotated[Any, "NestedStruct"]]'
346+
'-> numpy.ndarray[Annotated[Any, pybind11.CppType("NestedStruct")]]'
347347
)
348348

349349

0 commit comments

Comments
 (0)