pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.08k stars 2.05k forks source link

feat(types): add type[T] support to typing.h #5166

Closed InvincibleRMC closed 1 week ago

InvincibleRMC commented 2 weeks ago

Description

Similar to #5165 but, adds support for type[] (see https://docs.python.org/3.10/library/typing.html#typing.Type).

The benefit of adding support for type[] allows type checkers to differentiate between different types.

By default foo could take any valid python type E.g. float or str

def foo(a: type) -> None: ...

But with allowing specific the type it can narrow valid input properly and str would no long be valid.

def foo(a: type[int]) -> None: ...

Suggested changelog entry:

Support for ``type[T]`` was added to pybind11/typing.h.
rwgk commented 2 weeks ago

I'm not a typing expert. For people like myself, could you please explain in the PR description why adding type[] is useful?

rwgk commented 2 weeks ago

Hi @InvincibleRMC, could you help by resolving the merge conflicts (I just merged your #5165)?