-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 for TypedDict
#16249
Changes from 6 commits
339b232
29cc9b9
ac3de1c
353c3ee
a3c904b
aa06b44
d1f4b78
6b1d4be
6eb5728
6c1ae4a
73015a2
f8f4d62
1bcdf93
37f6c67
f4d05ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -71,3 +71,18 @@ class _TypedDict(Mapping[str, object]): | |||||||||||||||||||||
def pop(self, k: NoReturn, default: T = ...) -> object: ... | ||||||||||||||||||||||
def update(self: T, __m: T) -> None: ... | ||||||||||||||||||||||
def __delitem__(self, k: NoReturn) -> None: ... | ||||||||||||||||||||||
# It is a bit of a lie: | ||||||||||||||||||||||
# 1. it is only supported since 3.9 in runtime | ||||||||||||||||||||||
# 2. `__or__` uses `dict[str, Any]`, not `Mapping[str, object]` | ||||||||||||||||||||||
@overload | ||||||||||||||||||||||
def __or__(self, __value: Self) -> Self: ... | ||||||||||||||||||||||
@overload | ||||||||||||||||||||||
def __or__(self, __value: Mapping[str, object]) -> Mapping[str, object]: ... | ||||||||||||||||||||||
# TODO: re-enable after `__ror__` definition is fixed | ||||||||||||||||||||||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
# https://github.com/python/typeshed/issues/10678 | ||||||||||||||||||||||
# @overload | ||||||||||||||||||||||
# def __ror__(self, __value: Self) -> Self: ... | ||||||||||||||||||||||
# @overload | ||||||||||||||||||||||
# def __ror__(self, __value: dict[str, Any]) -> dict[str, object]: ... | ||||||||||||||||||||||
# supposedly incompatible definitions of __or__ and __ior__ | ||||||||||||||||||||||
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. I don't understand this comment: see what I wrote in python/typeshed#10678 (comment). What do you think is broken in 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. Here's my understanding of this problem:
So, my problem definition is: 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. Here is the snippet the OP was complaining about in #16244: from typing import TypedDict
class Foo(TypedDict):
key: int
foo: Foo = {"key": 1}
foo |= {"key": 3} mypy reports:
https://mypy-play.net/?mypy=latest&python=3.11&gist=9927e1732f6fce4595d29a19493ae603 Note: mypy does not report that That indicates that mypy is using the second overload of 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 |
||||||||||||||||||||||
def __ior__(self, __value: Self) -> Self: ... # type: ignore[misc] |
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.
These tests both pass with the
master
branch checked outThere 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.
Yes, this is just the extra test that was missing. Please, see #16244 for the bug the user had.