pybind / pybind11

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

feat(types): adds support for TypeGuard and TypeIs #5194

Closed InvincibleRMC closed 3 days ago

InvincibleRMC commented 3 days ago

Description

Allows annotating functions for type narrowing using TypeGuard or TypeIs.

For additional information relating to the specific differences between TypeGuard and TypeIs can be found here.

def is_str_type_guard(x: object) -> TypeGuard[str]:
    return isinstance(x, str)

def is_str_type_is(x: object) -> TypeIs[str]:
    return isinstance(x, str)

Suggested changelog entry:

   Adds support for `typing.TypeGuard` and `typing.TypeIs`.