Skip to content

Add typing and collections.abc module prefix #5663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fc436dd
Fix Python 3.8 type hints and add module prefix
gentlegiantJGC May 14, 2025
7f59a8b
style: pre-commit fixes
pre-commit-ci[bot] May 14, 2025
cf1610c
Add module prefix to Union
gentlegiantJGC May 15, 2025
78b2f91
Rename macros
gentlegiantJGC May 15, 2025
ac9a3fd
Improve comment
gentlegiantJGC May 15, 2025
aaff285
Comment out 3.8 type hint macros
gentlegiantJGC May 15, 2025
f27cc9e
Add Iterable module prefix
gentlegiantJGC May 15, 2025
1d143be
Add module prefix to Iterator
gentlegiantJGC May 15, 2025
6806f11
Add module prefix to Callable
gentlegiantJGC May 15, 2025
4faee40
Re-add accidentally deleted brackets
gentlegiantJGC May 15, 2025
64345df
Add module prefix to Optional
gentlegiantJGC May 15, 2025
22f9ceb
Add module prefix to Final
gentlegiantJGC May 15, 2025
345f974
Add module prefix to ClassVar
gentlegiantJGC May 15, 2025
7915d6e
Add module prefix to TypeGuard
gentlegiantJGC May 15, 2025
299a65a
Add module prefix to TypeIs
gentlegiantJGC May 15, 2025
88d408b
Add module prefix to NoReturn
gentlegiantJGC May 15, 2025
f125949
Add module prefix to Never
gentlegiantJGC May 15, 2025
6a013af
Add module prefix to Literal
gentlegiantJGC May 15, 2025
99700bb
Add module prefix to Callable
gentlegiantJGC May 15, 2025
9b93079
Add module prefix to Sequence
gentlegiantJGC May 15, 2025
21663a3
Add module prefix to Iterator
gentlegiantJGC May 15, 2025
edc52b5
Merge branch 'master' into fix-type-hints-3-9
gentlegiantJGC May 15, 2025
5751384
style: pre-commit fixes
pre-commit-ci[bot] May 15, 2025
edb4ba3
Remove type hint macros
gentlegiantJGC May 16, 2025
e684a4d
style: pre-commit fixes
pre-commit-ci[bot] May 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ struct handle_type_name<dict> {
};
template <>
struct handle_type_name<anyset> {
static constexpr auto name = const_name("Union[set, frozenset]");
static constexpr auto name = const_name("typing.Union[set, frozenset]");
};
template <>
struct handle_type_name<set> {
Expand Down Expand Up @@ -1395,19 +1395,19 @@ struct handle_type_name<int_> {
};
template <>
struct handle_type_name<iterable> {
static constexpr auto name = const_name("Iterable");
static constexpr auto name = const_name("collections.abc.Iterable");
};
template <>
struct handle_type_name<iterator> {
static constexpr auto name = const_name("Iterator");
static constexpr auto name = const_name("collections.abc.Iterator");
};
template <>
struct handle_type_name<float_> {
static constexpr auto name = io_name("typing.SupportsFloat", "float");
};
template <>
struct handle_type_name<function> {
static constexpr auto name = const_name("Callable");
static constexpr auto name = const_name("collections.abc.Callable");
};
template <>
struct handle_type_name<handle> {
Expand All @@ -1419,7 +1419,7 @@ struct handle_type_name<none> {
};
template <>
struct handle_type_name<sequence> {
static constexpr auto name = const_name("Sequence");
static constexpr auto name = const_name("collections.abc.Sequence");
};
template <>
struct handle_type_name<bytearray> {
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct type_caster<std::function<Return(Args...)>> {

PYBIND11_TYPE_CASTER(
type,
const_name("Callable[[")
const_name("collections.abc.Callable[[")
+ ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster<Args>::name)...)
+ const_name("], ") + ::pybind11::detail::return_descr(make_caster<retval_type>::name)
+ const_name("]"));
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods)

template <>
struct handle_type_name<cpp_function> {
static constexpr auto name = const_name("Callable");
static constexpr auto name = const_name("collections.abc.Callable");
};

PYBIND11_NAMESPACE_END(detail)
Expand Down
5 changes: 3 additions & 2 deletions include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -642,7 +643,7 @@ struct variant_caster<V<Ts...>> {

using Type = V<Ts...>;
PYBIND11_TYPE_CASTER(Type,
const_name("Union[")
const_name("typing.Union[")
+ ::pybind11::detail::concat(make_caster<Ts>::name...)
+ const_name("]"));
};
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/stl/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 19 additions & 13 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,21 @@ struct handle_type_name<typing::Set<T>> {

template <typename T>
struct handle_type_name<typing::Iterable<T>> {
static constexpr auto name = const_name("Iterable[") + make_caster<T>::name + const_name("]");
static constexpr auto name
= const_name("collections.abc.Iterable[") + make_caster<T>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::Iterator<T>> {
static constexpr auto name = const_name("Iterator[") + make_caster<T>::name + const_name("]");
static constexpr auto name
= const_name("collections.abc.Iterator[") + make_caster<T>::name + const_name("]");
};

template <typename Return, typename... Args>
struct handle_type_name<typing::Callable<Return(Args...)>> {
using retval_type = conditional_t<std::is_same<Return, void>::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<Args>::name)...)
+ const_name("], ") + ::pybind11::detail::return_descr(make_caster<retval_type>::name)
+ const_name("]");
Expand All @@ -204,7 +206,7 @@ template <typename Return>
struct handle_type_name<typing::Callable<Return(ellipsis)>> {
// PEP 484 specifies this syntax for defining only return types of callables
using retval_type = conditional_t<std::is_same<Return, void>::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<retval_type>::name)
+ const_name("]");
};
Expand All @@ -216,46 +218,50 @@ struct handle_type_name<typing::Type<T>> {

template <typename... Types>
struct handle_type_name<typing::Union<Types...>> {
static constexpr auto name = const_name("Union[")
static constexpr auto name = const_name("typing.Union[")
+ ::pybind11::detail::concat(make_caster<Types>::name...)
+ const_name("]");
};

template <typename T>
struct handle_type_name<typing::Optional<T>> {
static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
static constexpr auto name
= const_name("typing.Optional[") + make_caster<T>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::Final<T>> {
static constexpr auto name = const_name("Final[")
static constexpr auto name = const_name("typing.Final[")
+ ::pybind11::detail::return_descr(make_caster<T>::name)
+ const_name("]");
};

template <typename T>
struct handle_type_name<typing::ClassVar<T>> {
static constexpr auto name = const_name("ClassVar[") + make_caster<T>::name + const_name("]");
static constexpr auto name
= const_name("typing.ClassVar[") + make_caster<T>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::TypeGuard<T>> {
static constexpr auto name = const_name("TypeGuard[") + make_caster<T>::name + const_name("]");
static constexpr auto name
= const_name("typing.TypeGuard[") + make_caster<T>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::TypeIs<T>> {
static constexpr auto name = const_name("TypeIs[") + make_caster<T>::name + const_name("]");
static constexpr auto name
= const_name("typing.TypeIs[") + make_caster<T>::name + const_name("]");
};

template <>
struct handle_type_name<typing::NoReturn> {
static constexpr auto name = const_name("NoReturn");
static constexpr auto name = const_name("typing.NoReturn");
};

template <>
struct handle_type_name<typing::Never> {
static constexpr auto name = const_name("Never");
static constexpr auto name = const_name("typing.Never");
};

#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
Expand All @@ -281,7 +287,7 @@ consteval auto sanitize_string_literal() {
template <typing::StringLiteral... Literals>
struct handle_type_name<typing::Literal<Literals...>> {
static constexpr auto name
= const_name("Literal[")
= const_name("typing.Literal[")
+ pybind11::detail::concat(const_name(sanitize_string_literal<Literals>().name)...)
+ const_name("]");
};
Expand Down
6 changes: 3 additions & 3 deletions tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
)


Expand Down Expand Up @@ -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"
)


Expand Down
5 changes: 3 additions & 2 deletions tests/test_docs_advanced_cast_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ struct type_caster<user_space::Point2D> {
// 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
Expand Down
5 changes: 4 additions & 1 deletion tests/test_docs_advanced_cast_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace detail {

template <>
struct type_caster<RealNumber> {
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();
Expand Down
Loading
Loading