Fix TypedDict get literal defaults#3831
Conversation
|
Hi @goutamadwant! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Diff from mypy_primer, showing the effect of this PR on open source code: pyinstrument (https://github.com/joerick/pyinstrument)
- ERROR pyinstrument/context_manager.py:40:34-52: Argument `Literal['disabled', 'enabled', 'strict'] | str` is not assignable to parameter `async_mode` with type `Literal['disabled', 'enabled', 'strict']` in function `pyinstrument.profiler.Profiler.__init__` [bad-argument-type]
meson (https://github.com/mesonbuild/meson)
- ERROR mesonbuild/build.py:2198:32-68: `Literal['bin', 'cdylib', 'dylib', 'lib', 'proc-macro', 'rlib', 'staticlib'] | str` is not assignable to attribute `rust_crate_type` with type `Literal['bin', 'cdylib', 'dylib', 'lib', 'proc-macro', 'rlib', 'staticlib']` [bad-assignment]
- ERROR mesonbuild/build.py:2331:32-69: `Literal['bin', 'cdylib', 'dylib', 'lib', 'proc-macro', 'rlib', 'staticlib'] | str` is not assignable to attribute `rust_crate_type` with type `Literal['bin', 'cdylib', 'dylib', 'lib', 'proc-macro', 'rlib', 'staticlib']` [bad-assignment]
- ERROR mesonbuild/build.py:2551:32-70: `Literal['bin', 'cdylib', 'dylib', 'lib', 'proc-macro', 'rlib', 'staticlib'] | str` is not assignable to attribute `rust_crate_type` with type `Literal['bin', 'cdylib', 'dylib', 'lib', 'proc-macro', 'rlib', 'staticlib']` [bad-assignment]
core (https://github.com/home-assistant/core)
- ERROR homeassistant/components/mqtt/config_flow.py:4648:27-65: No matching overload found for function `homeassistant.components.mqtt.models.MqttDeviceData.update` called with arguments: (dict[tuple[Literal['configuration_url'], str] | tuple[Literal['hw_version'], str] | tuple[Literal['identifiers'], str] | tuple[Literal['model'], str] | tuple[Literal['model_id'], str] | tuple[Literal['mqtt_settings'], DeviceMqttOptions] | tuple[Literal['name'], str] | tuple[Literal['sw_version'], str], @_] | DeviceMqttOptions) [no-matching-overload]
+ ERROR homeassistant/components/mqtt/config_flow.py:4648:27-65: No matching overload found for function `homeassistant.components.mqtt.models.MqttDeviceData.update` called with arguments: (DeviceMqttOptions) [no-matching-overload]
- ERROR homeassistant/loader.py:860:16-60: Returned type `Literal['device', 'entity', 'hardware', 'helper', 'hub', 'service', 'system', 'virtual'] | str` is not assignable to declared return type `Literal['device', 'entity', 'hardware', 'helper', 'hub', 'service', 'system', 'virtual']` [bad-return]
paasta (https://github.com/yelp/paasta)
- ERROR paasta_tools/kubernetes_tools.py:820:35-85: No matching overload found for function `typing.MutableMapping.update` called with arguments: (dict[str, int | list[dict[str, int | str]] | str] | dict[Unknown, Unknown] | None) [no-matching-overload]
+ ERROR paasta_tools/kubernetes_tools.py:820:35-85: No matching overload found for function `typing.MutableMapping.update` called with arguments: (dict[Unknown, Unknown] | None) [no-matching-overload]
discord.py (https://github.com/Rapptz/discord.py)
- ERROR discord/app_commands/tree.py:1221:63-87: Argument `dict[@_, @_] | ResolvedData` is not assignable to parameter `resolved` with type `ResolvedData` in function `discord.app_commands.namespace.Namespace._get_resolved_items` [bad-argument-type]
- ERROR discord/app_commands/tree.py:1277:44-68: Argument `dict[@_, @_] | ResolvedData` is not assignable to parameter `resolved` with type `ResolvedData` in function `discord.app_commands.namespace.Namespace.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `enabled` with type `bool` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `help` with type `str | None` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `brief` with type `str | None` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `usage` with type `str | None` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `rest_is_raw` with type `bool` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `aliases` with type `list[str] | tuple[str, ...]` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `description` with type `str` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `hidden` with type `bool` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `checks` with type `list[(Context[Any]) -> Coroutine[Any, Any, bool] | bool]` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `cooldown` with type `CooldownMapping[Context[Any]]` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `max_concurrency` with type `MaxConcurrency` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `require_var_positional` with type `bool` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `cooldown_after_parsing` with type `bool` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `ignore_extra` with type `bool` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `extras` with type `dict[Any, Any]` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/ext/commands/help.py:402:53-73: Unpacked keyword argument `object` is not assignable to parameter `name` with type `str` in function `_HelpCommandImpl.__init__` [bad-argument-type]
- ERROR discord/interactions.py:353:32-56: Argument `dict[@_, @_] | ResolvedData` is not assignable to parameter `resolved` with type `ResolvedData` in function `discord.app_commands.namespace.Namespace.__init__` [bad-argument-type]
- ERROR discord/state.py:830:81-89: Argument `dict[Unknown, Unknown] | ResolvedData` is not assignable to parameter `resolved` with type `ResolvedData` in function `discord.ui.view.ViewStore.dispatch_modal` [bad-argument-type]
|
Primer Diff Classification✅ 3 improvement(s) | ➖ 1 neutral | ❓ 1 needs review | 5 project(s) total | +2, -12 errors 3 improvement(s) across pyinstrument, meson, discord.py.
Detailed analysis✅ Improvement (3)pyinstrument (-1)
meson (-3)
discord.py (-5)
Error 3 ( All 5 errors appear to be false positives that are resolved by improved TypedDict type inference in the PR.
➖ Neutral (1)paasta (+1, -1)
❓ Needs Review (1)core (+1, -2)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (1 heuristic, 4 LLM) |
Summary
Fixes #3787
Add a field-typed default overload when synthesizing
TypedDict.getandTypedDict.popmethods:This overload is tried before the existing generic default overload, so defaults that are already assignable to the field type keep the field type. For a non-required field typed as
Literal["a", "b"],.get("x", "b")now resolves toLiteral["a", "b"]instead ofLiteral["a", "b"] | str.Defaults outside the field type still use the generic
_Toverload and keep the existingFieldType | Tbehavior.Test Plan