gh-117516: Implement typing.TypeIs#117517
Merged
JelleZijlstra merged 13 commits intopython:mainfrom Apr 9, 2024
Merged
Conversation
Closed
carljm
reviewed
Apr 3, 2024
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``). |
Member
There was a problem hiding this comment.
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
Member
Author
There was a problem hiding this comment.
Adding the link for now, we should figure out how to set up intersphinx to enable linking to the spec.
carljm
approved these changes
Apr 5, 2024
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 |
Member
There was a problem hiding this comment.
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.
Member
Author
There was a problem hiding this comment.
Good idea, done. Also changed the docstrings of TypeGuard and TypeIs accordingly.
Co-authored-by: Carl Meyer <carl@oddbird.net>
picnixz
reviewed
Apr 6, 2024
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
diegorusso
pushed a commit
to diegorusso/cpython
that referenced
this pull request
Apr 17, 2024
See PEP 742. Co-authored-by: Carl Meyer <carl@oddbird.net> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/