Skip to content

fix(types): 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

Merged
merged 25 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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(PYBIND11_ITERABLE_TYPE_HINT);
};
template <>
struct handle_type_name<iterator> {
static constexpr auto name = const_name("Iterator");
static constexpr auto name = const_name(PYBIND11_ITERATOR_TYPE_HINT);
};
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(PYBIND11_CALLABLE_TYPE_HINT);
};
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(PYBIND11_SEQUENCE_TYPE_HINT);
};
template <>
struct handle_type_name<bytearray> {
Expand Down
21 changes: 21 additions & 0 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,27 @@
# define PYBIND11_SUBINTERPRETER_SUPPORT
#endif

// 3.9 Compatibility
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Python 3.9+ Compatibility

?
(The + is the most important aspect of my suggestion.)

#if 0x03090000 <= PY_VERSION_HEX
# define PYBIND11_ITERABLE_TYPE_HINT "collections.abc.Iterable"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to make these

PYBIND11_TYPE_HINT_...

so they line up nicely and are easier to pin-point.

# 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
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(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[[")
+ ::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(PYBIND11_CALLABLE_TYPE_HINT);
};

PYBIND11_NAMESPACE_END(detail)
Expand Down
51 changes: 30 additions & 21 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,55 +146,60 @@ PYBIND11_NAMESPACE_BEGIN(detail)

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

template <>
struct handle_type_name<typing::Tuple<>> {
// 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 <typename T>
struct handle_type_name<typing::Tuple<T, ellipsis>> {
// PEP 484 specifies this syntax for a variable-length tuple
static constexpr auto name
= const_name("tuple[") + make_caster<T>::name + const_name(", ...]");
static constexpr auto name = const_name(PYBIND11_TUPLE_TYPE_HINT) + const_name("[")
+ make_caster<T>::name + const_name(", ...]");
};

template <typename K, typename V>
struct handle_type_name<typing::Dict<K, V>> {
static constexpr auto name = const_name("dict[") + make_caster<K>::name + const_name(", ")
+ make_caster<V>::name + const_name("]");
static constexpr auto name = const_name(PYBIND11_DICT_TYPE_HINT) + const_name("[")
+ make_caster<K>::name + const_name(", ") + make_caster<V>::name
+ const_name("]");
};

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

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

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(PYBIND11_ITERABLE_TYPE_HINT) + const_name("[")
+ 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(PYBIND11_ITERATOR_TYPE_HINT) + const_name("[")
+ 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(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[[")
+ ::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 +209,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(PYBIND11_CALLABLE_TYPE_HINT) + const_name("[..., ")
+ ::pybind11::detail::return_descr(make_caster<retval_type>::name)
+ const_name("]");
};
Expand All @@ -216,46 +221,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 +290,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
Loading