feat: proper narrowing for TypedDict keys and values#19610
Closed
BobTheBuidler wants to merge 2 commits intopython:masterfrom
Closed
feat: proper narrowing for TypedDict keys and values#19610BobTheBuidler wants to merge 2 commits intopython:masterfrom
BobTheBuidler wants to merge 2 commits intopython:masterfrom
Conversation
| reveal_type(p) # N: Revealed type is "TypedDict('__main__.Point', {'x': builtins.int, 'y': builtins.int})" | ||
| # Use values() to check fallback value type. | ||
| reveal_type(p.values()) # N: Revealed type is "typing.Iterable[builtins.object]" | ||
| reveal_type(p.values()) # N: Revealed type is "typing.ValuesView[builtins.int]" |
Member
There was a problem hiding this comment.
This is incorrect since the TypedDict is not closed, so there may be other values that are not of this type.
Contributor
Author
There was a problem hiding this comment.
Wait why isn't it closed? If you try to set p['not-present'] = 123 you will get an error from mypy
Member
There was a problem hiding this comment.
There can be subclasses. The upcoming PEP 728 addresses this.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: scrapy (https://github.com/scrapy/scrapy)
+ scrapy/utils/request.py:230: error: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes [str-bytes-safe]
yarl (https://github.com/aio-libs/yarl)
+ tests/test_cache.py:18:12: error: Non-overlapping equality check (left operand type: "KeysView[Literal['idna_encode', 'idna_decode', 'ip_address', 'host_validate', 'encode_host']]", right operand type: "set[str]") [comparison-overlap]
+ tests/test_cache.py:18:12: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-comparison-overlap for more info
|
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 PR enhances type narrowing for
TypedDict.keysandTypedDict.values.Keys will now type as a Union of Literals and values will type as a Union of the value types.
I will do
TypedDict.itemsin a separate PR, as I'm having trouble with the typeshed.(Explain how this PR changes mypy.)