-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix crash with forward reference in TypedDict #3560
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -813,3 +813,43 @@ p = TaggedPoint(type='2d', x=42, y=1337) | |
p.get('x', 1 + 'y') # E: Unsupported operand types for + ("int" and "str") | ||
[builtins fixtures/dict.pyi] | ||
[typing fixtures/typing-full.pyi] | ||
|
||
|
||
-- Special cases | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just have few comments:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good comments! My responses:
|
||
|
||
[case testForwardReferenceInTypedDict] | ||
from typing import Mapping | ||
from mypy_extensions import TypedDict | ||
X = TypedDict('X', {'b': 'B', 'c': 'C'}) | ||
class B: pass | ||
class C(B): pass | ||
x: X | ||
reveal_type(x) # E: Revealed type is 'TypedDict(b=__main__.B, c=__main__.C, _fallback=__main__.X)' | ||
m1: Mapping[str, B] = x | ||
m2: Mapping[str, C] = x # E: Incompatible types in assignment (expression has type "X", variable has type Mapping[str, C]) | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testForwardReferenceInClassTypedDict] | ||
from typing import Mapping | ||
from mypy_extensions import TypedDict | ||
class X(TypedDict): | ||
b: 'B' | ||
c: 'C' | ||
class B: pass | ||
class C(B): pass | ||
x: X | ||
reveal_type(x) # E: Revealed type is 'TypedDict(b=__main__.B, c=__main__.C, _fallback=__main__.X)' | ||
m1: Mapping[str, B] = x | ||
m2: Mapping[str, C] = x # E: Incompatible types in assignment (expression has type "X", variable has type Mapping[str, C]) | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testForwardReferenceToTypedDictInTypedDict] | ||
from typing import Mapping | ||
from mypy_extensions import TypedDict | ||
# Forward references don't quite work yet | ||
X = TypedDict('X', {'a': 'A'}) # E: Invalid type "__main__.A" | ||
A = TypedDict('A', {'b': int}) | ||
x: X | ||
reveal_type(x) # E: Revealed type is 'TypedDict(a=TypedDict(b=builtins.int, _fallback=__main__.A), _fallback=__main__.X)' | ||
reveal_type(x['a']['b']) # E: Revealed type is 'builtins.int' | ||
[builtins fixtures/dict.pyi] |
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.
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.
Just curious whether it would ever make a difference whether this was MutableMapping?
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.
Probably the most precise fallback would be just
Dict
, but probably we want to have some flexibility becauseMapping
is covariant in value type (unlikeMutableMapping
andDict
that are invariant).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.
Hm, now you mention variance, why shouldn't this be invariant?
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.
My previous comment was just my interpretation of the status quo. As far as I am concerned, I would prefer fallback to be just
Dict
(this would be most precise and most similar to how tuples and named tuples work).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.
The reason why we don't use a mutable type as a fallback is that it would compromise type safety. For example, if the fallback would be
Dict[str, (join of value types)]
, then for{'a': 1, 'b': 'x'}
the fallback type would beDict[str, object]
. Through the fallback type we could do things likedel d['a']
ord['a'] = [1]
which would break safety.Mapping
only supports getting values, so it's safe. Also, sinceMapping
only provides read operations, it can be covariant in the value type.Making a typed dict compatible with
Dict[str, Any]
without making this the fallback might be a reasonable thing to do, but it's unclear if that would result in ambiguity or other problems.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.
Maybe I am missing something, but
TypedDict
is special-cased incheker.check_indexed_assignment
, howeverdel d['a']
indeed goes through just checking for__delitem__
and the latter is looked up on the fallback. Maybe we can just special-case this too?TypedDict
s at runtime are justdict
s and have methods likeupdate
that can't be accessed onMapping
. AlsoTypedDict
withtotal=False
can support item deletion.Anyway, it looks like both
Mapping
andDict
fallbacks require some special-casing, and it is not something where I have a strong opinion.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.
Yeah, we could special case additional methods, but it wouldn't still be safe: a TypedDict is a subtype of the fallback type, and if the fallback type is
Dict
, we could upcast a TypedDict to theDict
type and do things that the TypedDict wouldn't support, such as setting items with incompatible value types, or deleting required keys.