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.
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
Add
|=
and|
operators support forTypedDict
#16249Add
|=
and|
operators support forTypedDict
#16249Changes from 7 commits
339b232
29cc9b9
ac3de1c
353c3ee
a3c904b
aa06b44
d1f4b78
6b1d4be
6eb5728
6c1ae4a
73015a2
f8f4d62
1bcdf93
37f6c67
f4d05ce
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
I don't understand this comment: see what I wrote in python/typeshed#10678 (comment). What do you think is broken in
_TypedDict.__(r)or__
?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.
Here's my understanding of this problem:
dict.__or__
does not allowTypedDict
, because it only allowsdict
(andTypedDict
does not match it):__or__
operator, so even forfoo | {'key': 1}
it raises (without this PR):So, my problem definition is:
dict.__or__
is not compatible withTypedDict
anddict.__ror__
is also not compatible withTypedDict
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.
Here is the snippet the OP was complaining about in #16244:
mypy reports:
https://mypy-play.net/?mypy=latest&python=3.11&gist=9927e1732f6fce4595d29a19493ae603
Note: mypy does not report that
|=
cannot be used betweenfoo
and{"key: 3}
. It understands that the|=
operator can be used between the two objects. However, it is inferring that the type of thefoo
variable after the|
-ing isdict[str, object]
rather thanFoo
, which would be an incompatible reassignment of thefoo
variable, since thefoo
variable has been declared to always have typeFoo
.That indicates that mypy is using the second overload of
_TypedDict.__or__
here to infer the type offoo
after the|=
operation, rather than_TypedDict.__ior__
or the first overload of_TypedDict.__or__
. If it used the_TypedDict.__ior__
annotations, or the first overload of_TypedDict.__or__
, it would be able to correctly infer that the type offoo
after the|=
operation is stillFoo
, which is a reassignment that's compatible with the declared type of thefoo
variable:mypy/mypy/typeshed/stdlib/typing.pyi
Lines 910 to 919 in 2e52e98
I think the issue is that in this snippet, mypy is not able currently to infer using type context (bidirectional type inference) that the object on the right-hand-side of the
|=
operator can be considered to be an instance ofFoo
. If it did so, then it would select_TypedDict.__ior__
or the first overload of_TypedDict.__or__
to infer the type offoo
after the|=
operation, and would correctly understand that the type of thefoo
variable does not change as a result of the operation.