-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Special case TypedDict.get #19639
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
Draft
randolf-scholz
wants to merge
5
commits into
python:master
Choose a base branch
from
randolf-scholz:special_case_typeddict_get
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Special case TypedDict.get #19639
randolf-scholz
wants to merge
5
commits into
python:master
from
randolf-scholz:special_case_typeddict_get
Conversation
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
randolf-scholz
commented
Aug 11, 2025
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: operator (https://github.com/canonical/operator)
- ops/model.py:1149: error: Incompatible types in assignment (expression has type "str | None", variable has type "str") [assignment]
- ops/model.py:1223: error: Incompatible types in assignment (expression has type "str | None", variable has type "str") [assignment]
- ops/_private/harness.py:2280: error: Value expression in dictionary comprehension has incompatible type "str | int | float | None"; expected type "str | int | float" [misc]
paasta (https://github.com/yelp/paasta)
+ paasta_tools/kubernetes_tools.py:1801: error: Need type annotation for "crypto_keys" [var-annotated]
core (https://github.com/home-assistant/core)
+ homeassistant/data_entry_flow.py:645: error: Unused "type: ignore" comment [unused-ignore]
+ homeassistant/data_entry_flow.py:650: error: Unused "type: ignore" comment [unused-ignore]
- homeassistant/components/evohome/storage.py:98: error: Argument 1 to "fromisoformat" of "date" has incompatible type "object"; expected "str" [arg-type]
+ homeassistant/components/fritz/coordinator.py:397: error: Statement is unreachable [unreachable]
+ homeassistant/components/fritz/coordinator.py:414: error: Incompatible types in assignment (expression has type "bool | None", variable has type "bool") [assignment]
steam.py (https://github.com/Gobot1234/steam.py)
- steam/http.py:62: error: Incompatible types in assignment (expression has type "int", variable has type "Language") [assignment]
+ steam/http.py:62: error: Incompatible types in assignment (expression has type "object", variable has type "Language") [assignment]
- steam/http.py:349: error: Value of "last_assetid" has incompatible type "str | int"; expected "str" [typeddict-item]
- steam/user.py:125: error: Argument "game_name" to "CMsgClientPersonaStateFriend" has incompatible type "str | int"; expected "str" [arg-type]
- steam/state.py:206: error: Incompatible types in assignment (expression has type "int", variable has type "PersonaState") [assignment]
+ steam/state.py:206: error: Incompatible types in assignment (expression has type "object", variable has type "PersonaState") [assignment]
- steam/state.py:207: error: Incompatible types in assignment (expression has type "int", variable has type "UIMode") [assignment]
+ steam/state.py:207: error: Incompatible types in assignment (expression has type "object", variable has type "UIMode") [assignment]
- steam/state.py:208: error: Incompatible types in assignment (expression has type "int", variable has type "PersonaStateFlag") [assignment]
+ steam/state.py:208: error: Incompatible types in assignment (expression has type "object", variable has type "PersonaStateFlag") [assignment]
discord.py (https://github.com/Rapptz/discord.py)
+ discord/automod.py:176: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ discord/automod.py:176: note: Possible overload variants:
+ discord/automod.py:176: note: def get(self, Never, None = ..., /) -> None
+ discord/automod.py:176: note: def get(self, Never, Never, /) -> Never
+ discord/automod.py:176: note: def [_T] get(self, Never, _T, /) -> _T
- discord/scheduled_event.py:139: error: Incompatible types in assignment (expression has type "object", variable has type "str | None") [assignment]
- discord/scheduled_event.py:145: error: Incompatible types in assignment (expression has type "object", variable has type "str | None") [assignment]
- discord/scheduled_event.py:146: error: Incompatible types in assignment (expression has type "object", variable has type "int") [assignment]
- discord/scheduled_event.py:150: error: Argument 1 to "store_user" of "ConnectionState" has incompatible type "object"; expected "User | PartialUser" [arg-type]
- discord/scheduled_event.py:155: error: No overload variant of "parse_time" matches argument type "object" [call-overload]
- discord/scheduled_event.py:155: note: Possible overload variants:
- discord/scheduled_event.py:155: note: def parse_time(timestamp: None) -> None
- discord/scheduled_event.py:155: note: def parse_time(timestamp: str) -> datetime
- discord/scheduled_event.py:155: note: def parse_time(timestamp: str | None) -> datetime | None
- discord/scheduled_event.py:159: error: Argument 1 to "_unroll_metadata" of "ScheduledEvent" has incompatible type "object"; expected "EntityMetadata | None" [arg-type]
- discord/poll.py:449: error: Item "None" of "PollMedia | None" has no attribute "get" [union-attr]
- discord/poll.py:459: error: Argument "question" to "Poll" has incompatible type "str | Any | None"; expected "PollMedia | str" [arg-type]
- discord/app_commands/models.py:224: error: No overload variant of "int" matches argument type "object" [call-overload]
- discord/app_commands/models.py:224: note: Possible overload variants:
- discord/app_commands/models.py:224: note: def __new__(cls, str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc = ..., /) -> int
- discord/app_commands/models.py:224: note: def __new__(cls, str | bytes | bytearray, /, base: SupportsIndex) -> int
- discord/app_commands/models.py:231: error: Incompatible types in assignment (expression has type "object", variable has type "bool") [assignment]
- discord/app_commands/models.py:237: error: Argument 1 to "_from_value" of "ArrayFlags" has incompatible type "object"; expected "Sequence[int]" [arg-type]
- discord/app_commands/models.py:243: error: Argument 1 to "_from_value" of "ArrayFlags" has incompatible type "object"; expected "Sequence[int]" [arg-type]
- discord/app_commands/models.py:245: error: Incompatible types in assignment (expression has type "object", variable has type "bool") [assignment]
- discord/app_commands/models.py:246: error: Argument 1 to "_to_locale_dict" has incompatible type "object"; expected "dict[str, str]" [arg-type]
- discord/app_commands/models.py:247: error: Argument 1 to "_to_locale_dict" has incompatible type "object"; expected "dict[str, str]" [arg-type]
- discord/app_commands/models.py:1059: error: Argument 1 to "_to_locale_dict" has incompatible type "object"; expected "dict[str, str]" [arg-type]
- discord/app_commands/models.py:1060: error: Argument 1 to "_to_locale_dict" has incompatible type "object"; expected "dict[str, str]" [arg-type]
- discord/app_commands/models.py:1158: error: Argument 1 to "_to_locale_dict" has incompatible type "object"; expected "dict[str, str]" [arg-type]
- discord/app_commands/models.py:1159: error: Argument 1 to "_to_locale_dict" has incompatible type "object"; expected "dict[str, str]" [arg-type]
- discord/message.py:869: error: "object" has no attribute "items" [attr-defined]
- discord/interactions.py:247: error: Need type annotation for "raw_channel" [var-annotated]
- discord/guild.py:244: error: Incompatible types in assignment (expression has type "int | None", variable has type "int") [assignment]
- discord/guild.py:245: error: Incompatible types in assignment (expression has type "int | None", variable has type "int") [assignment]
archinstall (https://github.com/archlinux/archinstall)
+ archinstall/lib/models/users.py:204: error: Left operand of "or" is always false [redundant-expr]
+ archinstall/lib/models/authentication.py:48: error: Statement is unreachable [unreachable]
|
For the last one, possibly it would be better to infer |
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.
Makes
TypedDict.get
smarter by producing a list of overloads corresponding to the key-value pairs.On 1.17.1:
With this PR:
I think in principle we should be able to combine the last two
y
overloads like pyright does it, but this likely needs some changes in generic callable inference.Moreover, we now get the non-optional type when doing
TypedDict.get(required_key)
:We get slightly different behavior with
List
/Set
/Dict
expressions:master: https://mypy-play.net/?mypy=master&python=3.12&gist=b3cac3b9355a53f8a5a1ad3fcdd8bbba
1.17.1: https://mypy-play.net/?mypy=latest&python=3.12&gist=dfb9f9df42e01a143f8f14d644347cc6
This PR:
Since in the example above,
C
is total, so an empty dict is not compatible withC
. Therefore, the behavior on both master and 1.17.1 is incorrect in this example.