From 65661fee3988faaa553ac7d742404454f5333b0c Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 16 Nov 2023 12:51:42 -0800 Subject: [PATCH] Change `detail::annotated_any()` to produce `pybind11.CppType(...)` Background: * https://github.com/python/mypy/issues/16306#issuecomment-1815191849 * https://github.com/python/mypy/issues/16306#issuecomment-1815246274 --- include/pybind11/detail/type_caster_base.h | 2 +- tests/test_cases_for_stubgen.py | 30 +++++++++++----------- tests/test_exceptions.py | 2 +- tests/test_numpy_dtypes.py | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 1b3762167..697c94bdd 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -1202,7 +1202,7 @@ protected: }; inline std::string annotated_any(const std::string &cpp_type) { - return "Annotated[Any, \"" + cpp_type + "\"]"; + return "Annotated[Any, pybind11.CppType(\"" + cpp_type + "\")]"; } PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti) { diff --git a/tests/test_cases_for_stubgen.py b/tests/test_cases_for_stubgen.py index 6bebacb2b..59d801aa0 100644 --- a/tests/test_cases_for_stubgen.py +++ b/tests/test_cases_for_stubgen.py @@ -11,28 +11,28 @@ TEST_CASES = { "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", "m.basics.Point.length_unit.__doc__": "Members:\n\n mm\n\n pixel\n\n inch", "m.basics.Point.angle_unit.__doc__": "Members:\n\n radian\n\n degree", - "m.pass_user_type.__doc__": 'pass_user_type(arg0: Annotated[Any, "test_cases_for_stubgen::UserType"]) -> None\n', - "m.return_user_type.__doc__": 'return_user_type() -> Annotated[Any, "test_cases_for_stubgen::UserType"]\n', + "m.pass_user_type.__doc__": 'pass_user_type(arg0: Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]) -> None\n', + "m.return_user_type.__doc__": 'return_user_type() -> Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]\n', "m.MapIntUserType.keys.__doc__": "keys(self: pybind11_tests.cases_for_stubgen.MapIntUserType) -> pybind11_tests.cases_for_stubgen.KeysView[int]\n", - "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', - "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', - "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', + "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', + "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', + "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', "m.MapUserTypeInt.values.__doc__": "values(self: pybind11_tests.cases_for_stubgen.MapUserTypeInt) -> pybind11_tests.cases_for_stubgen.ValuesView[int]\n", - "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', + "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', "m.MapFloatUserType.keys.__doc__": "keys(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[float]\n", - "m.MapFloatUserType.values.__doc__": 'values(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[Annotated[Any, "test_cases_for_stubgen::UserType"]]\n', - "m.MapFloatUserType.__iter__.__doc__": '__iter__(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[tuple[float, Annotated[Any, "test_cases_for_stubgen::UserType"]]]\n', - "m.MapUserTypeFloat.keys.__doc__": 'keys(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[Annotated[Any, "test_cases_for_stubgen::UserType"]]\n', + "m.MapFloatUserType.values.__doc__": 'values(self: pybind11_tests.cases_for_stubgen.MapFloatUserType) -> Iterator[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]\n', + "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', + "m.MapUserTypeFloat.keys.__doc__": 'keys(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]\n', "m.MapUserTypeFloat.values.__doc__": "values(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[float]\n", - "m.MapUserTypeFloat.__iter__.__doc__": '__iter__(self: pybind11_tests.cases_for_stubgen.MapUserTypeFloat) -> Iterator[tuple[Annotated[Any, "test_cases_for_stubgen::UserType"], float]]\n', + "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', "m.pass_std_array_int_2.__doc__": "pass_std_array_int_2(arg0: Annotated[list[int], FixedSize(2)]) -> None\n", "m.return_std_array_int_3.__doc__": "return_std_array_int_3() -> Annotated[list[int], FixedSize(3)]\n", "m.nested_case_01a.__doc__": "nested_case_01a(arg0: list[Annotated[list[int], FixedSize(2)]]) -> None\n", - "m.nested_case_02a.__doc__": 'nested_case_02a(arg0: list[Annotated[Any, "test_cases_for_stubgen::UserType"]]) -> None\n', - "m.nested_case_03a.__doc__": 'nested_case_03a(arg0: dict[Annotated[list[int], FixedSize(2)], Annotated[Any, "test_cases_for_stubgen::UserType"]]) -> None\n', - "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', - "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', - "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', + "m.nested_case_02a.__doc__": 'nested_case_02a(arg0: list[Annotated[Any, pybind11.CppType("test_cases_for_stubgen::UserType")]]) -> None\n', + "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', + "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', + "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', + "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', } diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 336754ca1..22b9d4d46 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -424,4 +424,4 @@ def test_fn_cast_int_exception(): def test_return_exception_void(): with pytest.raises(TypeError) as excinfo: m.return_exception_void() - assert 'Annotated[Any, "exception"]' in str(excinfo.value) + assert 'Annotated[Any, pybind11.CppType("exception")]' in str(excinfo.value) diff --git a/tests/test_numpy_dtypes.py b/tests/test_numpy_dtypes.py index 7653c770b..01b4cc01e 100644 --- a/tests/test_numpy_dtypes.py +++ b/tests/test_numpy_dtypes.py @@ -343,7 +343,7 @@ def test_complex_array(): def test_signature(doc): assert ( doc(m.create_rec_nested) == "create_rec_nested(arg0: int) " - '-> numpy.ndarray[Annotated[Any, "NestedStruct"]]' + '-> numpy.ndarray[Annotated[Any, pybind11.CppType("NestedStruct")]]' )