-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-117516: Implement typing.TypeIs #117517
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
Conversation
Doc/library/typing.rst
Outdated
|
||
``TypeGuard`` also works with type variables. See :pep:`647` for more details. | ||
``TypeIs`` also works with type variables. For more information, see | ||
PEP 742 (Narrowing types with ``TypeIs``). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question here as below -- should we link to typing spec instead of PEP?
If we do link to PEP, should probably make it a real link using :pep:
role
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the link for now, we should figure out how to set up intersphinx to enable linking to the spec.
Doc/library/typing.rst
Outdated
are incompatible (e.g., ``list[object]`` to ``list[int]``) and when the | ||
function does not return ``True`` for all instances of the narrowed type. | ||
|
||
Using ``-> TypeIs`` tells the static type checker that for a given |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be clearer to use TypeIs[...]
or TypeIs[SomeType]
here? The latter would also give you a name that might be clearer/shorter than "the type inside TypeIs" below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, done. Also changed the docstrings of TypeGuard and TypeIs accordingly.
Co-authored-by: Bénédikt Tran <[email protected]>
See PEP 742. Co-authored-by: Carl Meyer <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
This adds
typing.TypeIs
, as proposed in PEP-742.The runtime implementation is trivial but the documentation is important here. As suggested in the PEP (https://peps.python.org/pep-0742/#how-to-teach-this), I made the documentation emphasize TypeIs over TypeGuard, and the TypeGuard docs now focus on how it is different from TypeIs.
📚 Documentation preview 📚: https://cpython-previews--117517.org.readthedocs.build/