-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add |=
and |
operators support for TypedDict
#16249
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 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
339b232
Add `|=` and `|` operators support for `TypedDict`
sobolevn 29cc9b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ac3de1c
Fix tests
sobolevn 353c3ee
Merge branch 'issue-16244' of https://github.com/python/mypy into iss…
sobolevn a3c904b
Address review
sobolevn aa06b44
Fix typo
sobolevn d1f4b78
Support `__ror__` and improve tests
sobolevn 6b1d4be
Simplify tests more
sobolevn 6eb5728
Fix tests
sobolevn 6c1ae4a
Check dict subtype
sobolevn 73015a2
Address review
sobolevn f8f4d62
Fix mypyc
sobolevn 1bcdf93
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 37f6c67
Fix CI
sobolevn f4d05ce
Address review
sobolevn 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Test stub for typing module that includes TypedDict `|` operator. | ||
# It only covers `__or__`, `__ror__`, and `__ior__`. | ||
# | ||
# We cannot define these methods in `typing-typeddict.pyi`, | ||
# because they need `dict` with two type args, | ||
# and not all tests using `[typing typing-typeddict.pyi]` have the proper | ||
# `dict` stub. | ||
# | ||
# Keep in sync with `typeshed`'s definition. | ||
from abc import ABCMeta | ||
|
||
cast = 0 | ||
assert_type = 0 | ||
overload = 0 | ||
Any = 0 | ||
Union = 0 | ||
Optional = 0 | ||
TypeVar = 0 | ||
Generic = 0 | ||
Protocol = 0 | ||
Tuple = 0 | ||
Callable = 0 | ||
NamedTuple = 0 | ||
Final = 0 | ||
Literal = 0 | ||
TypedDict = 0 | ||
NoReturn = 0 | ||
Required = 0 | ||
NotRequired = 0 | ||
Self = 0 | ||
|
||
T = TypeVar('T') | ||
T_co = TypeVar('T_co', covariant=True) | ||
V = TypeVar('V') | ||
|
||
# Note: definitions below are different from typeshed, variances are declared | ||
# to silence the protocol variance checks. Maybe it is better to use type: ignore? | ||
|
||
class Sized(Protocol): | ||
def __len__(self) -> int: pass | ||
|
||
class Iterable(Protocol[T_co]): | ||
def __iter__(self) -> 'Iterator[T_co]': pass | ||
|
||
class Iterator(Iterable[T_co], Protocol): | ||
def __next__(self) -> T_co: pass | ||
|
||
class Sequence(Iterable[T_co]): | ||
# misc is for explicit Any. | ||
def __getitem__(self, n: Any) -> T_co: pass # type: ignore[misc] | ||
|
||
class Mapping(Iterable[T], Generic[T, T_co], metaclass=ABCMeta): | ||
pass | ||
|
||
# Fallback type for all typed dicts (does not exist at runtime). | ||
class _TypedDict(Mapping[str, object]): | ||
@overload | ||
def __or__(self, __value: Self) -> Self: ... | ||
@overload | ||
def __or__(self, __value: dict[str, object]) -> dict[str, object]: ... | ||
@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__ | ||
def __ior__(self, __value: Self) -> Self: ... # type: ignore[misc] |
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.
Uh oh!
There was an error while loading. Please reload this page.