diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 44f8e1f837..0cc63b0b4a 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("collections.abc.Iterable"); }; template <> struct handle_type_name { - static constexpr auto name = const_name("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("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("Sequence"); + static constexpr auto name = const_name("collections.abc.Sequence"); }; template <> struct handle_type_name { diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 85f9b7907d..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("Callable[[") + 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 7108343f6f..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("Callable"); + static constexpr auto name = const_name("collections.abc.Callable"); }; PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 4258f1584c..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("Optional[") + value_conv::name + const_name("]")); + PYBIND11_TYPE_CASTER(Type, + const_name("typing.Optional[") + value_conv::name + const_name("]")); }; #if defined(PYBIND11_HAS_OPTIONAL) @@ -642,7 +643,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/include/pybind11/typing.h b/include/pybind11/typing.h index 09ff5d801c..57b2c087d1 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -182,19 +182,21 @@ struct handle_type_name> { template struct handle_type_name> { - static constexpr auto name = const_name("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("Iterator[") + make_caster::name + const_name("]"); + static constexpr auto name + = const_name("collections.abc.Iterator[") + 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("collections.abc.Callable[[") + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); @@ -204,7 +206,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("collections.abc.Callable[..., ") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); }; @@ -216,46 +218,50 @@ 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 +287,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("]"); }; 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_docs_advanced_cast_custom.cpp b/tests/test_docs_advanced_cast_custom.cpp index 0ec1b17ac6..d4114ad906 100644 --- a/tests/test_docs_advanced_cast_custom.cpp +++ b/tests/test_docs_advanced_cast_custom.cpp @@ -23,8 +23,9 @@ 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..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: 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.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..a073cfd640 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( @@ -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 @@ -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: Union[set, frozenset]) -> None" + assert ( + doc(m.print_anyset) + == "print_anyset(arg0: typing.Union[set, frozenset]) -> None" + ) def test_frozenset(capture, doc): @@ -955,28 +958,28 @@ 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" ) 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" ) 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" ) @@ -989,54 +992,57 @@ 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" ) 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]]" ) 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]" ) 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): - 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): - assert doc(m.annotate_never) == "annotate_never() -> Never" + assert doc(m.annotate_never) == "annotate_never() -> typing.Never" 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" ) @@ -1047,40 +1053,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: 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["->"], Union[float, int]], float]) -> Callable[[Literal["->"], 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) - == 'identity_literal_all_special_chars(arg0: Literal["!@!!->{%}"]) -> Literal["!@!!->{%}"]' + == 'identity_literal_all_special_chars(arg0: typing.Literal["!@!!->{%}"]) -> typing.Literal["!@!!->{%}"]' ) @@ -1160,7 +1166,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]" ) @@ -1191,9 +1197,10 @@ 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 @@ -1235,14 +1242,17 @@ 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): - 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,87 +1265,87 @@ 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: collections.abc.Iterable[typing.Union[float, int]]) -> collections.abc.Iterable[float]" ) # Iterator assert ( doc(m.identity_iterator) - == "identity_iterator(arg0: Iterator[Union[float, int]]) -> Iterator[float]" + == "identity_iterator(arg0: collections.abc.Iterator[typing.Union[float, int]]) -> collections.abc.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: 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[[Union[float, int]], float]], Callable[[Union[float, int]], float]]) -> Callable[[Callable[[Union[float, int]], float]], Callable[[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 ( 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: collections.abc.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: collections.abc.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: typing.Optional[typing.Union[float, int]]) -> typing.Optional[float]" ) # 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]" + assert doc(m.check_type_is) == "check_type_is(arg0: object) -> typing.TypeIs[float]" diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 6fba6fba34..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("-> 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(): @@ -194,7 +202,10 @@ 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(): 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" )