From fc436dd6cb55c4de214be55728d6e3dbf76af106 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Wed, 14 May 2025 09:58:15 +0100 Subject: [PATCH 01/24] Fix Python 3.8 type hints and add module prefix These type hints are invalid in Python 3.8. Add `typing.` prefix to remove ambiguity. --- include/pybind11/cast.h | 10 ++++----- include/pybind11/detail/common.h | 21 ++++++++++++++++++ include/pybind11/functional.h | 2 +- include/pybind11/pybind11.h | 2 +- include/pybind11/typing.h | 38 ++++++++++++++++---------------- 5 files changed, 47 insertions(+), 26 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 3bedb9d1a3..fca02ffe8d 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1359,7 +1359,7 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name("Union[set, frozenset]"); + static constexpr auto name = const_name("typing.Union[set, frozenset]"); }; template <> struct handle_type_name { @@ -1395,11 +1395,11 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name("Iterable"); + static constexpr auto name = const_name(PYBIND11_ITERABLE_TYPE_HINT); }; template <> struct handle_type_name { - static constexpr auto name = const_name("Iterator"); + static constexpr auto name = const_name(PYBIND11_ITERATOR_TYPE_HINT); }; template <> struct handle_type_name { @@ -1407,7 +1407,7 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name("Callable"); + static constexpr auto name = const_name(PYBIND11_CALLABLE_TYPE_HINT); }; template <> struct handle_type_name { @@ -1419,7 +1419,7 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name("Sequence"); + static constexpr auto name = const_name(PYBIND11_SEQUENCE_TYPE_HINT); }; template <> struct handle_type_name { diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index e9d954bc49..49ab52e281 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -239,6 +239,27 @@ # define PYBIND11_SUBINTERPRETER_SUPPORT #endif +// 3.9 Compatibility +#if 0x03090000 <= PY_VERSION_HEX +# define PYBIND11_ITERABLE_TYPE_HINT "collections.abc.Iterable" +# define PYBIND11_ITERATOR_TYPE_HINT "collections.abc.Iterator" +# define PYBIND11_CALLABLE_TYPE_HINT "collections.abc.Callable" +# define PYBIND11_SEQUENCE_TYPE_HINT "collections.abc.Sequence" +# define PYBIND11_TUPLE_TYPE_HINT "tuple" +# define PYBIND11_DICT_TYPE_HINT "dict" +# define PYBIND11_LIST_TYPE_HINT "list" +# define PYBIND11_SET_TYPE_HINT "set" +#else +# define PYBIND11_ITERABLE_TYPE_HINT "typing.Iterable" +# define PYBIND11_ITERATOR_TYPE_HINT "typing.Iterator" +# define PYBIND11_CALLABLE_TYPE_HINT "typing.Callable" +# define PYBIND11_SEQUENCE_TYPE_HINT "typing.Sequence" +# define PYBIND11_TUPLE_TYPE_HINT "typing.Tuple" +# define PYBIND11_DICT_TYPE_HINT "typing.Dict" +# define PYBIND11_LIST_TYPE_HINT "typing.List" +# define PYBIND11_SET_TYPE_HINT "typing.Set" +#endif + // #define PYBIND11_STR_LEGACY_PERMISSIVE // If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject // (probably surprising and never documented, but this was the diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 85f9b7907d..d30e35e3c7 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -137,7 +137,7 @@ struct type_caster> { PYBIND11_TYPE_CASTER( type, - const_name("Callable[[") + const_name(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[[") + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]")); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 7108343f6f..9ecf7e014b 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1252,7 +1252,7 @@ PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods) template <> struct handle_type_name { - static constexpr auto name = const_name("Callable"); + static constexpr auto name = const_name(PYBIND11_CALLABLE_TYPE_HINT); }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 09ff5d801c..489f6d684e 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -146,7 +146,7 @@ PYBIND11_NAMESPACE_BEGIN(detail) template struct handle_type_name> { - static constexpr auto name = const_name("tuple[") + static constexpr auto name = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[") + ::pybind11::detail::concat(make_caster::name...) + const_name("]"); }; @@ -154,47 +154,47 @@ struct handle_type_name> { template <> struct handle_type_name> { // PEP 484 specifies this syntax for an empty tuple - static constexpr auto name = const_name("tuple[()]"); + static constexpr auto name = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[()]"); }; template struct handle_type_name> { // PEP 484 specifies this syntax for a variable-length tuple static constexpr auto name - = const_name("tuple[") + make_caster::name + const_name(", ...]"); + = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[") + make_caster::name + const_name(", ...]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("dict[") + make_caster::name + const_name(", ") + static constexpr auto name = const_name(PYBIND11_DICT_TYPE_HINT) + const_name("[") + make_caster::name + const_name(", ") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("list[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_LIST_TYPE_HINT) + const_name("[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("set[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_SET_TYPE_HINT) + const_name("[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("Iterable[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_ITERABLE_TYPE_HINT) + const_name("[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("Iterator[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_ITERATOR_TYPE_HINT) + const_name("[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { using retval_type = conditional_t::value, void_type, Return>; static constexpr auto name - = const_name("Callable[[") + = const_name(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[[") + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); @@ -204,7 +204,7 @@ template struct handle_type_name> { // PEP 484 specifies this syntax for defining only return types of callables using retval_type = conditional_t::value, void_type, Return>; - static constexpr auto name = const_name("Callable[..., ") + static constexpr auto name = const_name(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[..., ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); }; @@ -216,46 +216,46 @@ struct handle_type_name> { template struct handle_type_name> { - static constexpr auto name = const_name("Union[") + static constexpr auto name = const_name("typing.Union[") + ::pybind11::detail::concat(make_caster::name...) + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("Optional[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name("typing.Optional[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("Final[") + static constexpr auto name = const_name("typing.Final[") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("ClassVar[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name("typing.ClassVar[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("TypeGuard[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name("typing.TypeGuard[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("TypeIs[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name("typing.TypeIs[") + make_caster::name + const_name("]"); }; template <> struct handle_type_name { - static constexpr auto name = const_name("NoReturn"); + static constexpr auto name = const_name("typing.NoReturn"); }; template <> struct handle_type_name { - static constexpr auto name = const_name("Never"); + static constexpr auto name = const_name("typing.Never"); }; #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL) @@ -281,7 +281,7 @@ consteval auto sanitize_string_literal() { template struct handle_type_name> { static constexpr auto name - = const_name("Literal[") + = const_name("typing.Literal[") + pybind11::detail::concat(const_name(sanitize_string_literal().name)...) + const_name("]"); }; From 7f59a8bb94222d7d7eed4b5871f156bdad55def8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 May 2025 09:00:37 +0000 Subject: [PATCH 02/24] style: pre-commit fixes --- include/pybind11/typing.h | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 489f6d684e..24d111ef03 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -160,34 +160,39 @@ struct handle_type_name> { template struct handle_type_name> { // PEP 484 specifies this syntax for a variable-length tuple - static constexpr auto name - = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[") + make_caster::name + const_name(", ...]"); + static constexpr auto name = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[") + + make_caster::name + const_name(", ...]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_DICT_TYPE_HINT) + const_name("[") + make_caster::name + const_name(", ") - + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_DICT_TYPE_HINT) + const_name("[") + + make_caster::name + const_name(", ") + make_caster::name + + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_LIST_TYPE_HINT) + const_name("[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_LIST_TYPE_HINT) + const_name("[") + + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_SET_TYPE_HINT) + const_name("[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_SET_TYPE_HINT) + const_name("[") + + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_ITERABLE_TYPE_HINT) + const_name("[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_ITERABLE_TYPE_HINT) + const_name("[") + + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_ITERATOR_TYPE_HINT) + const_name("[") + make_caster::name + const_name("]"); + static constexpr auto name = const_name(PYBIND11_ITERATOR_TYPE_HINT) + const_name("[") + + make_caster::name + const_name("]"); }; template @@ -223,7 +228,8 @@ struct handle_type_name> { template struct handle_type_name> { - static constexpr auto name = const_name("typing.Optional[") + make_caster::name + const_name("]"); + static constexpr auto name + = const_name("typing.Optional[") + make_caster::name + const_name("]"); }; template @@ -235,17 +241,20 @@ struct handle_type_name> { template struct handle_type_name> { - static constexpr auto name = const_name("typing.ClassVar[") + make_caster::name + const_name("]"); + static constexpr auto name + = const_name("typing.ClassVar[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("typing.TypeGuard[") + make_caster::name + const_name("]"); + static constexpr auto name + = const_name("typing.TypeGuard[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("typing.TypeIs[") + make_caster::name + const_name("]"); + static constexpr auto name + = const_name("typing.TypeIs[") + make_caster::name + const_name("]"); }; template <> From cf1610c7ac6c370493e59768a67a93d6aaeab323 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 08:38:13 +0100 Subject: [PATCH 03/24] Add module prefix to Union --- include/pybind11/stl.h | 2 +- include/pybind11/stl/filesystem.h | 2 +- tests/test_pytypes.cpp | 2 +- tests/test_pytypes.py | 48 +++++++++++++++---------------- tests/test_stl.py | 16 +++++------ 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 4258f1584c..41657148e1 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -642,7 +642,7 @@ struct variant_caster> { using Type = V; PYBIND11_TYPE_CASTER(Type, - const_name("Union[") + const_name("typing.Union[") + ::pybind11::detail::concat(make_caster::name...) + const_name("]")); }; diff --git a/include/pybind11/stl/filesystem.h b/include/pybind11/stl/filesystem.h index 4246518809..de64193ac4 100644 --- a/include/pybind11/stl/filesystem.h +++ b/include/pybind11/stl/filesystem.h @@ -96,7 +96,7 @@ struct path_caster { return true; } - PYBIND11_TYPE_CASTER(T, io_name("Union[os.PathLike, str, bytes]", "pathlib.Path")); + PYBIND11_TYPE_CASTER(T, io_name("typing.Union[os.PathLike, str, bytes]", "pathlib.Path")); }; #endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 9d7c955d8b..657480a005 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -155,7 +155,7 @@ namespace detail { template <> struct type_caster { - PYBIND11_TYPE_CASTER(RealNumber, io_name("Union[float, int]", "float")); + PYBIND11_TYPE_CASTER(RealNumber, io_name("typing.Union[float, int]", "float")); static handle cast(const RealNumber &number, return_value_policy, handle) { return py::float_(number.value).release(); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index d1044d995d..84af11a511 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -130,7 +130,7 @@ def test_set(capture, doc): assert m.anyset_contains({"foo"}, "foo") assert doc(m.get_set) == "get_set() -> set" - assert doc(m.print_anyset) == "print_anyset(arg0: Union[set, frozenset]) -> None" + assert doc(m.print_anyset) == "print_anyset(arg0: typing.Union[set, frozenset]) -> None" def test_frozenset(capture, doc): @@ -989,21 +989,21 @@ def test_type_annotation(doc): def test_union_annotations(doc): assert ( doc(m.annotate_union) - == "annotate_union(arg0: list[Union[str, typing.SupportsInt, object]], arg1: str, arg2: typing.SupportsInt, arg3: object) -> list[Union[str, int, object]]" + == "annotate_union(arg0: list[typing.Union[str, typing.SupportsInt, object]], arg1: str, arg2: typing.SupportsInt, arg3: object) -> list[typing.Union[str, int, object]]" ) def test_union_typing_only(doc): assert ( doc(m.union_typing_only) - == "union_typing_only(arg0: list[Union[str]]) -> list[Union[int]]" + == "union_typing_only(arg0: list[typing.Union[str]]) -> list[typing.Union[int]]" ) def test_union_object_annotations(doc): assert ( doc(m.annotate_union_to_object) - == "annotate_union_to_object(arg0: Union[typing.SupportsInt, str]) -> object" + == "annotate_union_to_object(arg0: typing.Union[typing.SupportsInt, str]) -> object" ) @@ -1072,11 +1072,11 @@ def test_literal(doc): ) assert ( doc(m.identity_literal_arrow_with_io_name) - == 'identity_literal_arrow_with_io_name(arg0: Literal["->"], arg1: Union[float, int]) -> Literal["->"]' + == 'identity_literal_arrow_with_io_name(arg0: Literal["->"], arg1: typing.Union[float, int]) -> Literal["->"]' ) assert ( doc(m.identity_literal_arrow_with_callable) - == 'identity_literal_arrow_with_callable(arg0: Callable[[Literal["->"], Union[float, int]], float]) -> Callable[[Literal["->"], Union[float, int]], float]' + == 'identity_literal_arrow_with_callable(arg0: Callable[[Literal["->"], typing.Union[float, int]], float]) -> Callable[[Literal["->"], typing.Union[float, int]], float]' ) assert ( doc(m.identity_literal_all_special_chars) @@ -1160,7 +1160,7 @@ def test_module_attribute_types() -> None: assert module_annotations["foo"] == "pybind11_tests.pytypes.foo" assert ( module_annotations["foo_union"] - == "Union[pybind11_tests.pytypes.foo, pybind11_tests.pytypes.foo2, pybind11_tests.pytypes.foo3]" + == "typing.Union[pybind11_tests.pytypes.foo, pybind11_tests.pytypes.foo2, pybind11_tests.pytypes.foo3]" ) @@ -1239,10 +1239,10 @@ def test_final_annotation() -> None: def test_arg_return_type_hints(doc): - assert doc(m.half_of_number) == "half_of_number(arg0: Union[float, int]) -> float" + assert doc(m.half_of_number) == "half_of_number(arg0: typing.Union[float, int]) -> float" assert ( doc(m.half_of_number_convert) - == "half_of_number_convert(x: Union[float, int]) -> float" + == "half_of_number_convert(x: typing.Union[float, int]) -> float" ) assert ( doc(m.half_of_number_noconvert) == "half_of_number_noconvert(x: float) -> float" @@ -1255,52 +1255,52 @@ def test_arg_return_type_hints(doc): # std::vector assert ( doc(m.half_of_number_vector) - == "half_of_number_vector(arg0: collections.abc.Sequence[Union[float, int]]) -> list[float]" + == "half_of_number_vector(arg0: collections.abc.Sequence[typing.Union[float, int]]) -> list[float]" ) # Tuple assert ( doc(m.half_of_number_tuple) - == "half_of_number_tuple(arg0: tuple[Union[float, int], Union[float, int]]) -> tuple[float, float]" + == "half_of_number_tuple(arg0: tuple[typing.Union[float, int], typing.Union[float, int]]) -> tuple[float, float]" ) # Tuple assert ( doc(m.half_of_number_tuple_ellipsis) - == "half_of_number_tuple_ellipsis(arg0: tuple[Union[float, int], ...]) -> tuple[float, ...]" + == "half_of_number_tuple_ellipsis(arg0: tuple[typing.Union[float, int], ...]) -> tuple[float, ...]" ) # Dict assert ( doc(m.half_of_number_dict) - == "half_of_number_dict(arg0: dict[str, Union[float, int]]) -> dict[str, float]" + == "half_of_number_dict(arg0: dict[str, typing.Union[float, int]]) -> dict[str, float]" ) # List assert ( doc(m.half_of_number_list) - == "half_of_number_list(arg0: list[Union[float, int]]) -> list[float]" + == "half_of_number_list(arg0: list[typing.Union[float, int]]) -> list[float]" ) # List> assert ( doc(m.half_of_number_nested_list) - == "half_of_number_nested_list(arg0: list[list[Union[float, int]]]) -> list[list[float]]" + == "half_of_number_nested_list(arg0: list[list[typing.Union[float, int]]]) -> list[list[float]]" ) # Set assert ( doc(m.identity_set) - == "identity_set(arg0: set[Union[float, int]]) -> set[float]" + == "identity_set(arg0: set[typing.Union[float, int]]) -> set[float]" ) # Iterable assert ( doc(m.identity_iterable) - == "identity_iterable(arg0: Iterable[Union[float, int]]) -> Iterable[float]" + == "identity_iterable(arg0: Iterable[typing.Union[float, int]]) -> Iterable[float]" ) # Iterator assert ( doc(m.identity_iterator) - == "identity_iterator(arg0: Iterator[Union[float, int]]) -> Iterator[float]" + == "identity_iterator(arg0: Iterator[typing.Union[float, int]]) -> Iterator[float]" ) # Callable identity assert ( doc(m.identity_callable) - == "identity_callable(arg0: Callable[[Union[float, int]], float]) -> Callable[[Union[float, int]], float]" + == "identity_callable(arg0: Callable[[typing.Union[float, int]], float]) -> Callable[[typing.Union[float, int]], float]" ) # Callable identity assert ( @@ -1310,27 +1310,27 @@ def test_arg_return_type_hints(doc): # Nested Callable identity assert ( doc(m.identity_nested_callable) - == "identity_nested_callable(arg0: Callable[[Callable[[Union[float, int]], float]], Callable[[Union[float, int]], float]]) -> Callable[[Callable[[Union[float, int]], float]], Callable[[Union[float, int]], float]]" + == "identity_nested_callable(arg0: Callable[[Callable[[typing.Union[float, int]], float]], Callabletyping.Union[float, int]], float]]) -> Callable[[Callable[[typing.Union[float, int]], float]], Callable[[typing.Union[float, int]], float]]" ) # Callable assert ( doc(m.apply_callable) - == "apply_callable(arg0: Union[float, int], arg1: Callable[[Union[float, int]], float]) -> float" + == "apply_callable(arg0: typing.Union[float, int], arg1: Callable[[typing.Union[float, int]], float]) -> float" ) # Callable assert ( doc(m.apply_callable_ellipsis) - == "apply_callable_ellipsis(arg0: Union[float, int], arg1: Callable[..., float]) -> float" + == "apply_callable_ellipsis(arg0: typing.Union[float, int], arg1: Callable[..., float]) -> float" ) # Union assert ( doc(m.identity_union) - == "identity_union(arg0: Union[Union[float, int], str]) -> Union[float, str]" + == "identity_union(arg0: typing.Union[typing.Union[float, int], str]) -> typing.Union[float, str]" ) # Optional assert ( doc(m.identity_optional) - == "identity_optional(arg0: Optional[Union[float, int]]) -> Optional[float]" + == "identity_optional(arg0: Optional[typing.Union[float, int]]) -> Optional[float]" ) # TypeGuard assert ( diff --git a/tests/test_stl.py b/tests/test_stl.py index 96c84374ce..c952d034fe 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -276,19 +276,19 @@ def __fspath__(self): assert m.parent_path(PseudoBytesPath()) == Path("foo") assert ( doc(m.parent_path) - == "parent_path(arg0: Union[os.PathLike, str, bytes]) -> pathlib.Path" + == "parent_path(arg0: typing.Union[os.PathLike, str, bytes]) -> pathlib.Path" ) # std::vector assert m.parent_paths(["foo/bar", "foo/baz"]) == [Path("foo"), Path("foo")] assert ( doc(m.parent_paths) - == "parent_paths(arg0: collections.abc.Sequence[Union[os.PathLike, str, bytes]]) -> list[pathlib.Path]" + == "parent_paths(arg0: collections.abc.Sequence[typing.Union[os.PathLike, str, bytes]]) -> list[pathlib.Path]" ) # py::typing::List assert m.parent_paths_list(["foo/bar", "foo/baz"]) == [Path("foo"), Path("foo")] assert ( doc(m.parent_paths_list) - == "parent_paths_list(arg0: list[Union[os.PathLike, str, bytes]]) -> list[pathlib.Path]" + == "parent_paths_list(arg0: list[typing.Union[os.PathLike, str, bytes]]) -> list[pathlib.Path]" ) # Nested py::typing::List assert m.parent_paths_nested_list([["foo/bar"], ["foo/baz", "foo/buzz"]]) == [ @@ -297,13 +297,13 @@ def __fspath__(self): ] assert ( doc(m.parent_paths_nested_list) - == "parent_paths_nested_list(arg0: list[list[Union[os.PathLike, str, bytes]]]) -> list[list[pathlib.Path]]" + == "parent_paths_nested_list(arg0: list[list[typing.Union[os.PathLike, str, bytes]]]) -> list[list[pathlib.Path]]" ) # py::typing::Tuple assert m.parent_paths_tuple(("foo/bar", "foo/baz")) == (Path("foo"), Path("foo")) assert ( doc(m.parent_paths_tuple) - == "parent_paths_tuple(arg0: tuple[Union[os.PathLike, str, bytes], Union[os.PathLike, str, bytes]]) -> tuple[pathlib.Path, pathlib.Path]" + == "parent_paths_tuple(arg0: tuple[typing.Union[os.PathLike, str, bytes], typing.Union[os.PathLike, str, bytes]]) -> tuple[pathlib.Path, pathlib.Path]" ) # py::typing::Dict assert m.parent_paths_dict( @@ -319,7 +319,7 @@ def __fspath__(self): } assert ( doc(m.parent_paths_dict) - == "parent_paths_dict(arg0: dict[str, Union[os.PathLike, str, bytes]]) -> dict[str, pathlib.Path]" + == "parent_paths_dict(arg0: dict[str, typing.Union[os.PathLike, str, bytes]]) -> dict[str, pathlib.Path]" ) @@ -337,7 +337,7 @@ def test_variant(doc): assert ( doc(m.load_variant) - == "load_variant(arg0: Union[typing.SupportsInt, str, typing.SupportsFloat, None]) -> str" + == "load_variant(arg0: typing.Union[typing.SupportsInt, str, typing.SupportsFloat, None]) -> str" ) @@ -353,7 +353,7 @@ def test_variant_monostate(doc): assert ( doc(m.load_monostate_variant) - == "load_monostate_variant(arg0: Union[None, typing.SupportsInt, str]) -> str" + == "load_monostate_variant(arg0: typing.Union[None, typing.SupportsInt, str]) -> str" ) From 78b2f9176826218337ae467bdd8b5a11da0ed350 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 08:49:03 +0100 Subject: [PATCH 04/24] Rename macros --- include/pybind11/cast.h | 8 ++++---- include/pybind11/detail/common.h | 32 ++++++++++++++++---------------- include/pybind11/functional.h | 2 +- include/pybind11/pybind11.h | 2 +- include/pybind11/typing.h | 20 ++++++++++---------- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index fca02ffe8d..2db71f7d9a 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1395,11 +1395,11 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_ITERABLE_TYPE_HINT); + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_ITERABLE); }; template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_ITERATOR_TYPE_HINT); + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_ITERATOR); }; template <> struct handle_type_name { @@ -1407,7 +1407,7 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_CALLABLE_TYPE_HINT); + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_CALLABLE); }; template <> struct handle_type_name { @@ -1419,7 +1419,7 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_SEQUENCE_TYPE_HINT); + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_SEQUENCE); }; template <> struct handle_type_name { diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 49ab52e281..78d873d9f2 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -241,23 +241,23 @@ // 3.9 Compatibility #if 0x03090000 <= PY_VERSION_HEX -# define PYBIND11_ITERABLE_TYPE_HINT "collections.abc.Iterable" -# define PYBIND11_ITERATOR_TYPE_HINT "collections.abc.Iterator" -# define PYBIND11_CALLABLE_TYPE_HINT "collections.abc.Callable" -# define PYBIND11_SEQUENCE_TYPE_HINT "collections.abc.Sequence" -# define PYBIND11_TUPLE_TYPE_HINT "tuple" -# define PYBIND11_DICT_TYPE_HINT "dict" -# define PYBIND11_LIST_TYPE_HINT "list" -# define PYBIND11_SET_TYPE_HINT "set" +# define PYBIND11_TYPE_HINT_ITERABLE "collections.abc.Iterable" +# define PYBIND11_TYPE_HINT_ITERATOR "collections.abc.Iterator" +# define PYBIND11_TYPE_HINT_CALLABLE "collections.abc.Callable" +# define PYBIND11_TYPE_HINT_SEQUENCE "collections.abc.Sequence" +# define PYBIND11_TYPE_HINT_TUPLE "tuple" +# define PYBIND11_TYPE_HINT_DICT "dict" +# define PYBIND11_TYPE_HINT_LIST "list" +# define PYBIND11_TYPE_HINT_SET "set" #else -# define PYBIND11_ITERABLE_TYPE_HINT "typing.Iterable" -# define PYBIND11_ITERATOR_TYPE_HINT "typing.Iterator" -# define PYBIND11_CALLABLE_TYPE_HINT "typing.Callable" -# define PYBIND11_SEQUENCE_TYPE_HINT "typing.Sequence" -# define PYBIND11_TUPLE_TYPE_HINT "typing.Tuple" -# define PYBIND11_DICT_TYPE_HINT "typing.Dict" -# define PYBIND11_LIST_TYPE_HINT "typing.List" -# define PYBIND11_SET_TYPE_HINT "typing.Set" +# define PYBIND11_TYPE_HINT_ITERABLE "typing.Iterable" +# define PYBIND11_TYPE_HINT_ITERATOR "typing.Iterator" +# define PYBIND11_TYPE_HINT_CALLABLE "typing.Callable" +# define PYBIND11_TYPE_HINT_SEQUENCE "typing.Sequence" +# define PYBIND11_TYPE_HINT_TUPLE "typing.Tuple" +# define PYBIND11_TYPE_HINT_DICT "typing.Dict" +# define PYBIND11_TYPE_HINT_LIST "typing.List" +# define PYBIND11_TYPE_HINT_SET "typing.Set" #endif // #define PYBIND11_STR_LEGACY_PERMISSIVE diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index d30e35e3c7..c0dcbc52e4 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -137,7 +137,7 @@ struct type_caster> { PYBIND11_TYPE_CASTER( type, - const_name(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[[") + const_name(PYBIND11_TYPE_HINT_CALLABLE) + const_name("[[") + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]")); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 9ecf7e014b..8088b4fb50 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1252,7 +1252,7 @@ PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods) template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_CALLABLE_TYPE_HINT); + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_CALLABLE); }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 24d111ef03..79583fef8b 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -146,7 +146,7 @@ PYBIND11_NAMESPACE_BEGIN(detail) template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[") + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_TUPLE) + const_name("[") + ::pybind11::detail::concat(make_caster::name...) + const_name("]"); }; @@ -154,44 +154,44 @@ struct handle_type_name> { template <> struct handle_type_name> { // PEP 484 specifies this syntax for an empty tuple - static constexpr auto name = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[()]"); + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_TUPLE) + const_name("[()]"); }; template struct handle_type_name> { // PEP 484 specifies this syntax for a variable-length tuple - static constexpr auto name = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[") + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_TUPLE) + const_name("[") + make_caster::name + const_name(", ...]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_DICT_TYPE_HINT) + const_name("[") + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_DICT) + const_name("[") + make_caster::name + const_name(", ") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_LIST_TYPE_HINT) + const_name("[") + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_LIST) + const_name("[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_SET_TYPE_HINT) + const_name("[") + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_SET) + const_name("[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_ITERABLE_TYPE_HINT) + const_name("[") + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_ITERABLE) + const_name("[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_ITERATOR_TYPE_HINT) + const_name("[") + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_ITERATOR) + const_name("[") + make_caster::name + const_name("]"); }; @@ -199,7 +199,7 @@ template struct handle_type_name> { using retval_type = conditional_t::value, void_type, Return>; static constexpr auto name - = const_name(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[[") + = const_name(PYBIND11_TYPE_HINT_CALLABLE) + const_name("[[") + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); @@ -209,7 +209,7 @@ template struct handle_type_name> { // PEP 484 specifies this syntax for defining only return types of callables using retval_type = conditional_t::value, void_type, Return>; - static constexpr auto name = const_name(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[..., ") + static constexpr auto name = const_name(PYBIND11_TYPE_HINT_CALLABLE) + const_name("[..., ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); }; From ac9a3fdc88829cd1d33567147825a48fb83ffe75 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 08:49:55 +0100 Subject: [PATCH 05/24] Improve comment --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 78d873d9f2..5d68a96d65 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -239,7 +239,7 @@ # define PYBIND11_SUBINTERPRETER_SUPPORT #endif -// 3.9 Compatibility +// Python 3.9+ Compatibility #if 0x03090000 <= PY_VERSION_HEX # define PYBIND11_TYPE_HINT_ITERABLE "collections.abc.Iterable" # define PYBIND11_TYPE_HINT_ITERATOR "collections.abc.Iterator" From aaff2854e126f19adf885c5c7256dce7bcfe6ffa Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 08:53:57 +0100 Subject: [PATCH 06/24] Comment out 3.8 type hint macros Fixing this issue in Python 3.8 will require updating lots of tests. This can be added in a further pull request. --- include/pybind11/detail/common.h | 40 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 5d68a96d65..024267616d 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -239,26 +239,26 @@ # define PYBIND11_SUBINTERPRETER_SUPPORT #endif -// Python 3.9+ Compatibility -#if 0x03090000 <= PY_VERSION_HEX -# define PYBIND11_TYPE_HINT_ITERABLE "collections.abc.Iterable" -# define PYBIND11_TYPE_HINT_ITERATOR "collections.abc.Iterator" -# define PYBIND11_TYPE_HINT_CALLABLE "collections.abc.Callable" -# define PYBIND11_TYPE_HINT_SEQUENCE "collections.abc.Sequence" -# define PYBIND11_TYPE_HINT_TUPLE "tuple" -# define PYBIND11_TYPE_HINT_DICT "dict" -# define PYBIND11_TYPE_HINT_LIST "list" -# define PYBIND11_TYPE_HINT_SET "set" -#else -# define PYBIND11_TYPE_HINT_ITERABLE "typing.Iterable" -# define PYBIND11_TYPE_HINT_ITERATOR "typing.Iterator" -# define PYBIND11_TYPE_HINT_CALLABLE "typing.Callable" -# define PYBIND11_TYPE_HINT_SEQUENCE "typing.Sequence" -# define PYBIND11_TYPE_HINT_TUPLE "typing.Tuple" -# define PYBIND11_TYPE_HINT_DICT "typing.Dict" -# define PYBIND11_TYPE_HINT_LIST "typing.List" -# define PYBIND11_TYPE_HINT_SET "typing.Set" -#endif +// Python 3.9+ Compatibility #5663 +//#if 0x03090000 <= PY_VERSION_HEX +#define PYBIND11_TYPE_HINT_ITERABLE "collections.abc.Iterable" +#define PYBIND11_TYPE_HINT_ITERATOR "collections.abc.Iterator" +#define PYBIND11_TYPE_HINT_CALLABLE "collections.abc.Callable" +#define PYBIND11_TYPE_HINT_SEQUENCE "collections.abc.Sequence" +#define PYBIND11_TYPE_HINT_TUPLE "tuple" +#define PYBIND11_TYPE_HINT_DICT "dict" +#define PYBIND11_TYPE_HINT_LIST "list" +#define PYBIND11_TYPE_HINT_SET "set" +// #else +// # define PYBIND11_TYPE_HINT_ITERABLE "typing.Iterable" +// # define PYBIND11_TYPE_HINT_ITERATOR "typing.Iterator" +// # define PYBIND11_TYPE_HINT_CALLABLE "typing.Callable" +// # define PYBIND11_TYPE_HINT_SEQUENCE "typing.Sequence" +// # define PYBIND11_TYPE_HINT_TUPLE "typing.Tuple" +// # define PYBIND11_TYPE_HINT_DICT "typing.Dict" +// # define PYBIND11_TYPE_HINT_LIST "typing.List" +// # define PYBIND11_TYPE_HINT_SET "typing.Set" +// #endif // #define PYBIND11_STR_LEGACY_PERMISSIVE // If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject From f27cc9e207304e6e62fb1b2e751ed6694b5ffbc2 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 08:58:59 +0100 Subject: [PATCH 07/24] Add Iterable module prefix --- tests/test_pytypes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 84af11a511..d3eb32c133 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -51,7 +51,7 @@ def test_from_iterable(pytype, from_iter_func): def test_iterable(doc): - assert doc(m.get_iterable) == "get_iterable() -> Iterable" + assert doc(m.get_iterable) == "get_iterable() -> collections.abc.Iterable" lst = [1, 2, 3] i = m.get_first_item_from_iterable(lst) assert i == 1 @@ -955,7 +955,7 @@ def test_set_annotations(doc): def test_iterable_annotations(doc): assert ( doc(m.annotate_iterable_str) - == "annotate_iterable_str(arg0: Iterable[str]) -> None" + == "annotate_iterable_str(arg0: collections.abc.Iterable[str]) -> None" ) @@ -1290,7 +1290,7 @@ def test_arg_return_type_hints(doc): # Iterable assert ( doc(m.identity_iterable) - == "identity_iterable(arg0: Iterable[typing.Union[float, int]]) -> Iterable[float]" + == "identity_iterable(arg0: collections.abc.Iterable[typing.Union[float, int]]) -> collections.abc.Iterable[float]" ) # Iterator assert ( From 1d143be8575c3aba1c5d6663fe024f54e2658948 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:02:34 +0100 Subject: [PATCH 08/24] Add module prefix to Iterator --- tests/test_pytypes.py | 6 +++--- tests/test_sequences_and_iterators.py | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index d3eb32c133..1543d2f749 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -31,7 +31,7 @@ def test_int(doc): def test_iterator(doc): - assert doc(m.get_iterator) == "get_iterator() -> Iterator" + assert doc(m.get_iterator) == "get_iterator() -> collections.abc.Iterator" @pytest.mark.parametrize( @@ -962,7 +962,7 @@ def test_iterable_annotations(doc): def test_iterator_annotations(doc): assert ( doc(m.annotate_iterator_int) - == "annotate_iterator_int(arg0: Iterator[typing.SupportsInt]) -> None" + == "annotate_iterator_int(arg0: collections.abc.Iterator[typing.SupportsInt]) -> None" ) @@ -1295,7 +1295,7 @@ def test_arg_return_type_hints(doc): # Iterator assert ( doc(m.identity_iterator) - == "identity_iterator(arg0: Iterator[typing.Union[float, int]]) -> Iterator[float]" + == "identity_iterator(arg0: collections.abc.Iterator[typing.Union[float, int]]) -> Iterator[float]" ) # Callable identity assert ( diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 6fba6fba34..27183284ad 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -64,12 +64,12 @@ def test_generalized_iterators_simple(): def test_iterator_doc_annotations(): - assert m.IntPairs.nonref.__doc__.endswith("-> Iterator[tuple[int, int]]\n") - assert m.IntPairs.nonref_keys.__doc__.endswith("-> Iterator[int]\n") - assert m.IntPairs.nonref_values.__doc__.endswith("-> Iterator[int]\n") - assert m.IntPairs.simple_iterator.__doc__.endswith("-> Iterator[tuple[int, int]]\n") - assert m.IntPairs.simple_keys.__doc__.endswith("-> Iterator[int]\n") - assert m.IntPairs.simple_values.__doc__.endswith("-> Iterator[int]\n") + assert m.IntPairs.nonref.__doc__.endswith("-> collections.abc.Iterator[tuple[int, int]]\n") + assert m.IntPairs.nonref_keys.__doc__.endswith("-> collections.abc.Iterator[int]\n") + assert m.IntPairs.nonref_values.__doc__.endswith("-> collections.abc.Iterator[int]\n") + assert m.IntPairs.simple_iterator.__doc__.endswith("-> collections.abc.Iterator[tuple[int, int]]\n") + assert m.IntPairs.simple_keys.__doc__.endswith("-> collections.abc.Iterator[int]\n") + assert m.IntPairs.simple_values.__doc__.endswith("-> collections.abc.Iterator[int]\n") def test_iterator_referencing(): From 6806f1195cb258fb0aaa664589f82320c7c13a00 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:05:52 +0100 Subject: [PATCH 09/24] Add module prefix to Callable --- tests/test_callbacks.py | 6 +++--- tests/test_pytypes.py | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 9eab10ccb6..09dadd94c3 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -140,11 +140,11 @@ def test_cpp_function_roundtrip(): def test_function_signatures(doc): assert ( doc(m.test_callback3) - == "test_callback3(arg0: Callable[[typing.SupportsInt], int]) -> str" + == "test_callback3(arg0: collections.abc.Callable[[typing.SupportsInt], int]) -> str" ) assert ( doc(m.test_callback4) - == "test_callback4() -> Callable[[typing.SupportsInt], int]" + == "test_callback4() -> collections.abc.Callable[[typing.SupportsInt], int]" ) @@ -231,7 +231,7 @@ def test_custom_func2(): def test_callback_docstring(): assert ( m.test_tuple_unpacking.__doc__.strip() - == "test_tuple_unpacking(arg0: Callable) -> object" + == "test_tuple_unpacking(arg0: collections.abc.Callable) -> object" ) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 1543d2f749..6ae69d6e0e 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1300,27 +1300,27 @@ def test_arg_return_type_hints(doc): # Callable identity assert ( doc(m.identity_callable) - == "identity_callable(arg0: Callable[[typing.Union[float, int]], float]) -> Callable[[typing.Union[float, int]], float]" + == "identity_callable(arg0: collections.abc.Callable[[typing.Union[float, int]], float]) -> collections.abc.Callable[[typing.Union[float, int]], float]" ) # Callable identity assert ( doc(m.identity_callable_ellipsis) - == "identity_callable_ellipsis(arg0: Callable[..., float]) -> Callable[..., float]" + == "identity_callable_ellipsis(arg0: collections.abc.Callable[..., float]) -> collections.abc.Callable[..., float]" ) # Nested Callable identity assert ( doc(m.identity_nested_callable) - == "identity_nested_callable(arg0: Callable[[Callable[[typing.Union[float, int]], float]], Callabletyping.Union[float, int]], float]]) -> Callable[[Callable[[typing.Union[float, int]], float]], Callable[[typing.Union[float, int]], float]]" + == "identity_nested_callable(arg0: collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], Callabletyping.Union[float, int]], float]]) -> collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], collections.abc.Callable[[typing.Union[float, int]], float]]" ) # Callable assert ( doc(m.apply_callable) - == "apply_callable(arg0: typing.Union[float, int], arg1: Callable[[typing.Union[float, int]], float]) -> float" + == "apply_callable(arg0: typing.Union[float, int], arg1: collections.abc.Callable[[typing.Union[float, int]], float]) -> float" ) # Callable assert ( doc(m.apply_callable_ellipsis) - == "apply_callable_ellipsis(arg0: typing.Union[float, int], arg1: Callable[..., float]) -> float" + == "apply_callable_ellipsis(arg0: typing.Union[float, int], arg1: collections.abc.Callable[..., float]) -> float" ) # Union assert ( From 4faee40dbac7424179f3a168f3c31a0bc2cca870 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:08:36 +0100 Subject: [PATCH 10/24] Re-add accidentally deleted brackets --- tests/test_pytypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 6ae69d6e0e..4bc3aa7ed7 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1310,7 +1310,7 @@ def test_arg_return_type_hints(doc): # Nested Callable identity assert ( doc(m.identity_nested_callable) - == "identity_nested_callable(arg0: collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], Callabletyping.Union[float, int]], float]]) -> collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], collections.abc.Callable[[typing.Union[float, int]], float]]" + == "identity_nested_callable(arg0: collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], Callable[[typing.Union[float, int]], float]]) -> collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], collections.abc.Callable[[typing.Union[float, int]], float]]" ) # Callable assert ( From 64345df46fd3b31f5e7b53e0f604e128b52f1f23 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:10:50 +0100 Subject: [PATCH 11/24] Add module prefix to Optional --- include/pybind11/stl.h | 2 +- tests/test_pytypes.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 41657148e1..5643a0d901 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -557,7 +557,7 @@ struct optional_caster { return true; } - PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]")); + PYBIND11_TYPE_CASTER(Type, const_name("typing.Optional[") + value_conv::name + const_name("]")); }; #if defined(PYBIND11_HAS_OPTIONAL) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 4bc3aa7ed7..1ccabe885c 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1010,7 +1010,7 @@ def test_union_object_annotations(doc): def test_optional_annotations(doc): assert ( doc(m.annotate_optional) - == "annotate_optional(arg0: list) -> list[Optional[str]]" + == "annotate_optional(arg0: list) -> list[typing.Optional[str]]" ) @@ -1036,7 +1036,7 @@ def test_never_annotation(doc): def test_optional_object_annotations(doc): assert ( doc(m.annotate_optional_to_object) - == "annotate_optional_to_object(arg0: Optional[typing.SupportsInt]) -> object" + == "annotate_optional_to_object(arg0: typing.Optional[typing.SupportsInt]) -> object" ) @@ -1330,7 +1330,7 @@ def test_arg_return_type_hints(doc): # Optional assert ( doc(m.identity_optional) - == "identity_optional(arg0: Optional[typing.Union[float, int]]) -> Optional[float]" + == "identity_optional(arg0: typing.Optional[typing.Union[float, int]]) -> typing.Optional[float]" ) # TypeGuard assert ( From 22f9cebcebd93b1954a5718ab43c3cb682bde093 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:12:02 +0100 Subject: [PATCH 12/24] Add module prefix to Final --- tests/test_pytypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 1ccabe885c..1d8d14f5f5 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1235,7 +1235,7 @@ def test_redeclaration_attr_with_type_hint() -> None: ) def test_final_annotation() -> None: module_annotations = get_annotations_helper(m) - assert module_annotations["CONST_INT"] == "Final[int]" + assert module_annotations["CONST_INT"] == "typing.Final[int]" def test_arg_return_type_hints(doc): From 345f974ec38e46c41cd24a8f63d8331f330b713c Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:12:42 +0100 Subject: [PATCH 13/24] Add module prefix to ClassVar --- tests/test_pytypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 1d8d14f5f5..1532b3955f 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1191,9 +1191,9 @@ def test_class_attribute_types() -> None: instance_annotations = get_annotations_helper(m.Instance) assert empty_annotations is None - assert static_annotations["x"] == "ClassVar[typing.SupportsFloat]" + assert static_annotations["x"] == "typing.ClassVar[typing.SupportsFloat]" assert ( - static_annotations["dict_str_int"] == "ClassVar[dict[str, typing.SupportsInt]]" + static_annotations["dict_str_int"] == "typing.ClassVar[dict[str, typing.SupportsInt]]" ) assert m.Static.x == 1.0 From 7915d6e798fbd44a416097b3d40f769b1fac3bae Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:13:34 +0100 Subject: [PATCH 14/24] Add module prefix to TypeGuard --- tests/test_pytypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 1532b3955f..2d3309827b 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1017,7 +1017,7 @@ def test_optional_annotations(doc): def test_type_guard_annotations(doc): assert ( doc(m.annotate_type_guard) - == "annotate_type_guard(arg0: object) -> TypeGuard[str]" + == "annotate_type_guard(arg0: object) -> typing.TypeGuard[str]" ) @@ -1335,7 +1335,7 @@ def test_arg_return_type_hints(doc): # TypeGuard assert ( doc(m.check_type_guard) - == "check_type_guard(arg0: list[object]) -> TypeGuard[list[float]]" + == "check_type_guard(arg0: list[object]) -> typing.TypeGuard[list[float]]" ) # TypeIs assert doc(m.check_type_is) == "check_type_is(arg0: object) -> TypeIs[float]" From 299a65a6ff5228fd017785454625b057d665855b Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:14:22 +0100 Subject: [PATCH 15/24] Add module prefix to TypeIs --- tests/test_pytypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 2d3309827b..223d8929aa 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1022,7 +1022,7 @@ def test_type_guard_annotations(doc): def test_type_is_annotations(doc): - assert doc(m.annotate_type_is) == "annotate_type_is(arg0: object) -> TypeIs[str]" + assert doc(m.annotate_type_is) == "annotate_type_is(arg0: object) -> typing.TypeIs[str]" def test_no_return_annotation(doc): @@ -1338,4 +1338,4 @@ def test_arg_return_type_hints(doc): == "check_type_guard(arg0: list[object]) -> typing.TypeGuard[list[float]]" ) # TypeIs - assert doc(m.check_type_is) == "check_type_is(arg0: object) -> TypeIs[float]" + assert doc(m.check_type_is) == "check_type_is(arg0: object) -> typing.TypeIs[float]" From 88d408b20afb9d0a3728b8e8069df9d8b2d95faa Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:14:55 +0100 Subject: [PATCH 16/24] Add module prefix to NoReturn --- tests/test_pytypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 223d8929aa..8c5f2bcd70 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1026,7 +1026,7 @@ def test_type_is_annotations(doc): def test_no_return_annotation(doc): - assert doc(m.annotate_no_return) == "annotate_no_return() -> NoReturn" + assert doc(m.annotate_no_return) == "annotate_no_return() -> typing.NoReturn" def test_never_annotation(doc): From f125949bdd1a5258626736e666168f16f5838bf6 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:15:30 +0100 Subject: [PATCH 17/24] Add module prefix to Never --- tests/test_pytypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 8c5f2bcd70..c5dc60d4ba 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1030,7 +1030,7 @@ def test_no_return_annotation(doc): def test_never_annotation(doc): - assert doc(m.annotate_never) == "annotate_never() -> Never" + assert doc(m.annotate_never) == "annotate_never() -> typing.Never" def test_optional_object_annotations(doc): From 6a013af0e37cb355da470134828bb4d57b82c040 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:17:18 +0100 Subject: [PATCH 18/24] Add module prefix to Literal --- tests/test_pytypes.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index c5dc60d4ba..96acadc07d 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1047,40 +1047,40 @@ def test_optional_object_annotations(doc): def test_literal(doc): assert ( doc(m.annotate_literal) - == 'annotate_literal(arg0: Literal[26, 0x1A, "hello world", b"hello world", u"hello world", True, Color.RED, None]) -> object' + == 'annotate_literal(arg0: typing.Literal[26, 0x1A, "hello world", b"hello world", u"hello world", True, Color.RED, None]) -> object' ) # The characters !, @, %, {, } and -> are used in the signature parser as special characters, but Literal should escape those for the parser to work. assert ( doc(m.identity_literal_exclamation) - == 'identity_literal_exclamation(arg0: Literal["!"]) -> Literal["!"]' + == 'identity_literal_exclamation(arg0: typing.Literal["!"]) -> typing.Literal["!"]' ) assert ( doc(m.identity_literal_at) - == 'identity_literal_at(arg0: Literal["@"]) -> Literal["@"]' + == 'identity_literal_at(arg0: typing.Literal["@"]) -> typing.Literal["@"]' ) assert ( doc(m.identity_literal_percent) - == 'identity_literal_percent(arg0: Literal["%"]) -> Literal["%"]' + == 'identity_literal_percent(arg0: typing.Literal["%"]) -> typing.Literal["%"]' ) assert ( doc(m.identity_literal_curly_open) - == 'identity_literal_curly_open(arg0: Literal["{"]) -> Literal["{"]' + == 'identity_literal_curly_open(arg0: typing.Literal["{"]) -> typing.Literal["{"]' ) assert ( doc(m.identity_literal_curly_close) - == 'identity_literal_curly_close(arg0: Literal["}"]) -> Literal["}"]' + == 'identity_literal_curly_close(arg0: typing.Literal["}"]) -> typing.Literal["}"]' ) assert ( doc(m.identity_literal_arrow_with_io_name) - == 'identity_literal_arrow_with_io_name(arg0: Literal["->"], arg1: typing.Union[float, int]) -> Literal["->"]' + == 'identity_literal_arrow_with_io_name(arg0: typing.Literal["->"], arg1: typing.Union[float, int]) -> typing.Literal["->"]' ) assert ( doc(m.identity_literal_arrow_with_callable) - == 'identity_literal_arrow_with_callable(arg0: Callable[[Literal["->"], typing.Union[float, int]], float]) -> Callable[[Literal["->"], typing.Union[float, int]], float]' + == 'identity_literal_arrow_with_callable(arg0: Callable[[typing.Literal["->"], typing.Union[float, int]], float]) -> Callable[[typing.Literal["->"], typing.Union[float, int]], float]' ) assert ( doc(m.identity_literal_all_special_chars) - == 'identity_literal_all_special_chars(arg0: Literal["!@!!->{%}"]) -> Literal["!@!!->{%}"]' + == 'identity_literal_all_special_chars(arg0: typing.Literal["!@!!->{%}"]) -> typing.Literal["!@!!->{%}"]' ) From 99700bbd17d259f20b4b021acf4d8f55e88dbaaf Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:18:52 +0100 Subject: [PATCH 19/24] Add module prefix to Callable --- tests/test_pytypes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 96acadc07d..abde6d279d 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -969,14 +969,14 @@ def test_iterator_annotations(doc): def test_fn_annotations(doc): assert ( doc(m.annotate_fn) - == "annotate_fn(arg0: Callable[[list[str], str], int]) -> None" + == "annotate_fn(arg0: collections.abc.Callable[[list[str], str], int]) -> None" ) def test_fn_return_only(doc): assert ( doc(m.annotate_fn_only_return) - == "annotate_fn_only_return(arg0: Callable[..., int]) -> None" + == "annotate_fn_only_return(arg0: collections.abc.Callable[..., int]) -> None" ) @@ -1076,7 +1076,7 @@ def test_literal(doc): ) assert ( doc(m.identity_literal_arrow_with_callable) - == 'identity_literal_arrow_with_callable(arg0: Callable[[typing.Literal["->"], typing.Union[float, int]], float]) -> Callable[[typing.Literal["->"], typing.Union[float, int]], float]' + == 'identity_literal_arrow_with_callable(arg0: collections.abc.Callable[[typing.Literal["->"], typing.Union[float, int]], float]) -> collections.abc.Callable[[typing.Literal["->"], typing.Union[float, int]], float]' ) assert ( doc(m.identity_literal_all_special_chars) @@ -1310,7 +1310,7 @@ def test_arg_return_type_hints(doc): # Nested Callable identity assert ( doc(m.identity_nested_callable) - == "identity_nested_callable(arg0: collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], Callable[[typing.Union[float, int]], float]]) -> collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], collections.abc.Callable[[typing.Union[float, int]], float]]" + == "identity_nested_callable(arg0: collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], collections.abc.Callable[[typing.Union[float, int]], float]]) -> collections.abc.Callable[[collections.abc.Callable[[typing.Union[float, int]], float]], collections.abc.Callable[[typing.Union[float, int]], float]]" ) # Callable assert ( From 9b93079d6de49b2d341d055e3b79fe44e8429f05 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:26:24 +0100 Subject: [PATCH 20/24] Add module prefix to Sequence --- tests/test_docs_advanced_cast_custom.cpp | 4 ++-- tests/test_docs_advanced_cast_custom.py | 2 +- tests/test_sequences_and_iterators.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_docs_advanced_cast_custom.cpp b/tests/test_docs_advanced_cast_custom.cpp index 0ec1b17ac6..92014d09c5 100644 --- a/tests/test_docs_advanced_cast_custom.cpp +++ b/tests/test_docs_advanced_cast_custom.cpp @@ -23,8 +23,8 @@ struct type_caster { // This macro inserts a lot of boilerplate code and sets the type hint. // `io_name` is used to specify different type hints for arguments and return values. // The signature of our negate function would then look like: - // `negate(Sequence[float]) -> tuple[float, float]` - PYBIND11_TYPE_CASTER(user_space::Point2D, io_name("Sequence[float]", "tuple[float, float]")); + // `negate(collections.abc.Sequence[float]) -> tuple[float, float]` + PYBIND11_TYPE_CASTER(user_space::Point2D, io_name("collections.abc.Sequence[float]", "tuple[float, float]")); // C++ -> Python: convert `Point2D` to `tuple[float, float]`. The second and third arguments // are used to indicate the return value policy and parent object (for diff --git a/tests/test_docs_advanced_cast_custom.py b/tests/test_docs_advanced_cast_custom.py index 8018b8f576..516e5588a0 100644 --- a/tests/test_docs_advanced_cast_custom.py +++ b/tests/test_docs_advanced_cast_custom.py @@ -21,7 +21,7 @@ def assert_negate_function( def test_negate(doc: SanitizedString) -> None: - assert doc(m.negate) == "negate(arg0: Sequence[float]) -> tuple[float, float]" + assert doc(m.negate) == "negate(arg0: collections.abc.Sequence[float]) -> tuple[float, float]" assert_negate_function([1.0, -1.0], (-1.0, 1.0)) assert_negate_function((1.0, -1.0), (-1.0, 1.0)) assert_negate_function([1, -1], (-1.0, 1.0)) diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 27183284ad..29849bf3c9 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -194,7 +194,7 @@ def __len__(self): def test_sequence_doc(): - assert m.sequence_length.__doc__.strip() == "sequence_length(arg0: Sequence) -> int" + assert m.sequence_length.__doc__.strip() == "sequence_length(arg0: collections.abc.Sequence) -> int" def test_map_iterator(): From 21663a39573bb1e356c971c3cb708fa041af3223 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 15 May 2025 09:26:34 +0100 Subject: [PATCH 21/24] Add module prefix to Iterator --- tests/test_pytypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index abde6d279d..f22eb12a06 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1295,7 +1295,7 @@ def test_arg_return_type_hints(doc): # Iterator assert ( doc(m.identity_iterator) - == "identity_iterator(arg0: collections.abc.Iterator[typing.Union[float, int]]) -> Iterator[float]" + == "identity_iterator(arg0: collections.abc.Iterator[typing.Union[float, int]]) -> collections.abc.Iterator[float]" ) # Callable identity assert ( From 5751384a0d9a61f90bf7762f2809e333e6e29611 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 15 May 2025 08:30:19 +0000 Subject: [PATCH 22/24] style: pre-commit fixes --- include/pybind11/detail/common.h | 2 +- include/pybind11/stl.h | 3 ++- tests/test_docs_advanced_cast_custom.cpp | 3 ++- tests/test_docs_advanced_cast_custom.py | 5 ++++- tests/test_pytypes.py | 18 ++++++++++++++---- tests/test_sequences_and_iterators.py | 21 ++++++++++++++++----- 6 files changed, 39 insertions(+), 13 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 6b724a6c76..d4d90faefc 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -247,7 +247,7 @@ #endif // Python 3.9+ Compatibility #5663 -//#if 0x03090000 <= PY_VERSION_HEX +// #if 0x03090000 <= PY_VERSION_HEX #define PYBIND11_TYPE_HINT_ITERABLE "collections.abc.Iterable" #define PYBIND11_TYPE_HINT_ITERATOR "collections.abc.Iterator" #define PYBIND11_TYPE_HINT_CALLABLE "collections.abc.Callable" diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 5643a0d901..ace5ed13b7 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -557,7 +557,8 @@ struct optional_caster { return true; } - PYBIND11_TYPE_CASTER(Type, const_name("typing.Optional[") + value_conv::name + const_name("]")); + PYBIND11_TYPE_CASTER(Type, + const_name("typing.Optional[") + value_conv::name + const_name("]")); }; #if defined(PYBIND11_HAS_OPTIONAL) diff --git a/tests/test_docs_advanced_cast_custom.cpp b/tests/test_docs_advanced_cast_custom.cpp index 92014d09c5..d4114ad906 100644 --- a/tests/test_docs_advanced_cast_custom.cpp +++ b/tests/test_docs_advanced_cast_custom.cpp @@ -24,7 +24,8 @@ struct type_caster { // `io_name` is used to specify different type hints for arguments and return values. // The signature of our negate function would then look like: // `negate(collections.abc.Sequence[float]) -> tuple[float, float]` - PYBIND11_TYPE_CASTER(user_space::Point2D, io_name("collections.abc.Sequence[float]", "tuple[float, float]")); + PYBIND11_TYPE_CASTER(user_space::Point2D, + io_name("collections.abc.Sequence[float]", "tuple[float, float]")); // C++ -> Python: convert `Point2D` to `tuple[float, float]`. The second and third arguments // are used to indicate the return value policy and parent object (for diff --git a/tests/test_docs_advanced_cast_custom.py b/tests/test_docs_advanced_cast_custom.py index 516e5588a0..5374cbceb2 100644 --- a/tests/test_docs_advanced_cast_custom.py +++ b/tests/test_docs_advanced_cast_custom.py @@ -21,7 +21,10 @@ def assert_negate_function( def test_negate(doc: SanitizedString) -> None: - assert doc(m.negate) == "negate(arg0: collections.abc.Sequence[float]) -> tuple[float, float]" + assert ( + doc(m.negate) + == "negate(arg0: collections.abc.Sequence[float]) -> tuple[float, float]" + ) assert_negate_function([1.0, -1.0], (-1.0, 1.0)) assert_negate_function((1.0, -1.0), (-1.0, 1.0)) assert_negate_function([1, -1], (-1.0, 1.0)) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index f22eb12a06..a073cfd640 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -130,7 +130,10 @@ def test_set(capture, doc): assert m.anyset_contains({"foo"}, "foo") assert doc(m.get_set) == "get_set() -> set" - assert doc(m.print_anyset) == "print_anyset(arg0: typing.Union[set, frozenset]) -> None" + assert ( + doc(m.print_anyset) + == "print_anyset(arg0: typing.Union[set, frozenset]) -> None" + ) def test_frozenset(capture, doc): @@ -1022,7 +1025,10 @@ def test_type_guard_annotations(doc): def test_type_is_annotations(doc): - assert doc(m.annotate_type_is) == "annotate_type_is(arg0: object) -> typing.TypeIs[str]" + assert ( + doc(m.annotate_type_is) + == "annotate_type_is(arg0: object) -> typing.TypeIs[str]" + ) def test_no_return_annotation(doc): @@ -1193,7 +1199,8 @@ def test_class_attribute_types() -> None: assert empty_annotations is None assert static_annotations["x"] == "typing.ClassVar[typing.SupportsFloat]" assert ( - static_annotations["dict_str_int"] == "typing.ClassVar[dict[str, typing.SupportsInt]]" + static_annotations["dict_str_int"] + == "typing.ClassVar[dict[str, typing.SupportsInt]]" ) assert m.Static.x == 1.0 @@ -1239,7 +1246,10 @@ def test_final_annotation() -> None: def test_arg_return_type_hints(doc): - assert doc(m.half_of_number) == "half_of_number(arg0: typing.Union[float, int]) -> float" + assert ( + doc(m.half_of_number) + == "half_of_number(arg0: typing.Union[float, int]) -> float" + ) assert ( doc(m.half_of_number_convert) == "half_of_number_convert(x: typing.Union[float, int]) -> float" diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 29849bf3c9..41c80e98d4 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -64,12 +64,20 @@ def test_generalized_iterators_simple(): def test_iterator_doc_annotations(): - assert m.IntPairs.nonref.__doc__.endswith("-> collections.abc.Iterator[tuple[int, int]]\n") + assert m.IntPairs.nonref.__doc__.endswith( + "-> collections.abc.Iterator[tuple[int, int]]\n" + ) assert m.IntPairs.nonref_keys.__doc__.endswith("-> collections.abc.Iterator[int]\n") - assert m.IntPairs.nonref_values.__doc__.endswith("-> collections.abc.Iterator[int]\n") - assert m.IntPairs.simple_iterator.__doc__.endswith("-> collections.abc.Iterator[tuple[int, int]]\n") + assert m.IntPairs.nonref_values.__doc__.endswith( + "-> collections.abc.Iterator[int]\n" + ) + assert m.IntPairs.simple_iterator.__doc__.endswith( + "-> collections.abc.Iterator[tuple[int, int]]\n" + ) assert m.IntPairs.simple_keys.__doc__.endswith("-> collections.abc.Iterator[int]\n") - assert m.IntPairs.simple_values.__doc__.endswith("-> collections.abc.Iterator[int]\n") + assert m.IntPairs.simple_values.__doc__.endswith( + "-> collections.abc.Iterator[int]\n" + ) def test_iterator_referencing(): @@ -194,7 +202,10 @@ def __len__(self): def test_sequence_doc(): - assert m.sequence_length.__doc__.strip() == "sequence_length(arg0: collections.abc.Sequence) -> int" + assert ( + m.sequence_length.__doc__.strip() + == "sequence_length(arg0: collections.abc.Sequence) -> int" + ) def test_map_iterator(): From edb4ba3cfe1a02b2f1875b0e38807923ba8d73b6 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Fri, 16 May 2025 08:48:48 +0100 Subject: [PATCH 23/24] Remove type hint macros --- include/pybind11/cast.h | 8 ++++---- include/pybind11/detail/common.h | 21 --------------------- include/pybind11/functional.h | 2 +- include/pybind11/pybind11.h | 2 +- include/pybind11/typing.h | 20 ++++++++++---------- 5 files changed, 16 insertions(+), 37 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 66a77b40a1..0cc63b0b4a 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1395,11 +1395,11 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_ITERABLE); + static constexpr auto name = const_name("collections.abc.Iterable"); }; template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_ITERATOR); + static constexpr auto name = const_name("collections.abc.Iterator"); }; template <> struct handle_type_name { @@ -1407,7 +1407,7 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_CALLABLE); + static constexpr auto name = const_name("collections.abc.Callable"); }; template <> struct handle_type_name { @@ -1419,7 +1419,7 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_SEQUENCE); + static constexpr auto name = const_name("collections.abc.Sequence"); }; template <> struct handle_type_name { diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index d4d90faefc..e3df32df3e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -246,27 +246,6 @@ # define PYBIND11_BUFFER_TYPE_HINT "typing_extensions.Buffer" #endif -// Python 3.9+ Compatibility #5663 -// #if 0x03090000 <= PY_VERSION_HEX -#define PYBIND11_TYPE_HINT_ITERABLE "collections.abc.Iterable" -#define PYBIND11_TYPE_HINT_ITERATOR "collections.abc.Iterator" -#define PYBIND11_TYPE_HINT_CALLABLE "collections.abc.Callable" -#define PYBIND11_TYPE_HINT_SEQUENCE "collections.abc.Sequence" -#define PYBIND11_TYPE_HINT_TUPLE "tuple" -#define PYBIND11_TYPE_HINT_DICT "dict" -#define PYBIND11_TYPE_HINT_LIST "list" -#define PYBIND11_TYPE_HINT_SET "set" -// #else -// # define PYBIND11_TYPE_HINT_ITERABLE "typing.Iterable" -// # define PYBIND11_TYPE_HINT_ITERATOR "typing.Iterator" -// # define PYBIND11_TYPE_HINT_CALLABLE "typing.Callable" -// # define PYBIND11_TYPE_HINT_SEQUENCE "typing.Sequence" -// # define PYBIND11_TYPE_HINT_TUPLE "typing.Tuple" -// # define PYBIND11_TYPE_HINT_DICT "typing.Dict" -// # define PYBIND11_TYPE_HINT_LIST "typing.List" -// # define PYBIND11_TYPE_HINT_SET "typing.Set" -// #endif - // #define PYBIND11_STR_LEGACY_PERMISSIVE // If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject // (probably surprising and never documented, but this was the diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index c0dcbc52e4..9327a1364d 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -137,7 +137,7 @@ struct type_caster> { PYBIND11_TYPE_CASTER( type, - const_name(PYBIND11_TYPE_HINT_CALLABLE) + const_name("[[") + const_name("collections.abc.Callable[[") + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]")); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 8088b4fb50..25c783f3f9 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1252,7 +1252,7 @@ PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods) template <> struct handle_type_name { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_CALLABLE); + static constexpr auto name = const_name("collections.abc.Callable"); }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 79583fef8b..60e9aca755 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -146,7 +146,7 @@ PYBIND11_NAMESPACE_BEGIN(detail) template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_TUPLE) + const_name("[") + static constexpr auto name = const_name("tuple[") + ::pybind11::detail::concat(make_caster::name...) + const_name("]"); }; @@ -154,44 +154,44 @@ struct handle_type_name> { template <> struct handle_type_name> { // PEP 484 specifies this syntax for an empty tuple - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_TUPLE) + const_name("[()]"); + static constexpr auto name = const_name("tuple[()]"); }; template struct handle_type_name> { // PEP 484 specifies this syntax for a variable-length tuple - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_TUPLE) + const_name("[") + static constexpr auto name = const_name("tuple[") + make_caster::name + const_name(", ...]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_DICT) + const_name("[") + static constexpr auto name = const_name("dict[") + make_caster::name + const_name(", ") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_LIST) + const_name("[") + static constexpr auto name = const_name("list[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_SET) + const_name("[") + static constexpr auto name = const_name("set[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_ITERABLE) + const_name("[") + static constexpr auto name = const_name("collections.abc.Iterable[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_ITERATOR) + const_name("[") + static constexpr auto name = const_name("collections.abc.Iterator[") + make_caster::name + const_name("]"); }; @@ -199,7 +199,7 @@ template struct handle_type_name> { using retval_type = conditional_t::value, void_type, Return>; static constexpr auto name - = const_name(PYBIND11_TYPE_HINT_CALLABLE) + const_name("[[") + = const_name("collections.abc.Callable[[") + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); @@ -209,7 +209,7 @@ template struct handle_type_name> { // PEP 484 specifies this syntax for defining only return types of callables using retval_type = conditional_t::value, void_type, Return>; - static constexpr auto name = const_name(PYBIND11_TYPE_HINT_CALLABLE) + const_name("[..., ") + static constexpr auto name = const_name("collections.abc.Callable[..., ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); }; From e684a4d39c6df07ea4cb573e0d63191b30bdfcfe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 16 May 2025 07:52:54 +0000 Subject: [PATCH 24/24] style: pre-commit fixes --- include/pybind11/typing.h | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 60e9aca755..57b2c087d1 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -160,39 +160,36 @@ struct handle_type_name> { template struct handle_type_name> { // PEP 484 specifies this syntax for a variable-length tuple - static constexpr auto name = const_name("tuple[") - + make_caster::name + const_name(", ...]"); + static constexpr auto name + = const_name("tuple[") + make_caster::name + const_name(", ...]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("dict[") - + make_caster::name + const_name(", ") + make_caster::name - + const_name("]"); + static constexpr auto name = const_name("dict[") + make_caster::name + const_name(", ") + + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("list[") - + make_caster::name + const_name("]"); + static constexpr auto name = const_name("list[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("set[") - + make_caster::name + const_name("]"); + static constexpr auto name = const_name("set[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("collections.abc.Iterable[") - + make_caster::name + const_name("]"); + static constexpr auto name + = const_name("collections.abc.Iterable[") + make_caster::name + const_name("]"); }; template struct handle_type_name> { - static constexpr auto name = const_name("collections.abc.Iterator[") - + make_caster::name + const_name("]"); + static constexpr auto name + = const_name("collections.abc.Iterator[") + make_caster::name + const_name("]"); }; template