10000 Make Mapping.get(default) more constrained by ndmitchell · Pull Request #14360 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Make Mapping.get(default) more constrained #14360

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 18 commits into from
Jul 2, 2025
Merged

Conversation

ndmitchell
Copy link
Contributor

Follow up to #14347, with all the tests now passing. Does the same thing as #10294 but using the pattern from #10501 everywhere. Means that the type of Mapping.get now has a type that very clearly returns the default, rather than potentially returning a fresh type that has never been seen before. Makes Pyrefly give more precise types for os.environ.get("X", "Y").

Assuming these commits are squash merged, but can squash in advance if required (the commit history involved a lot of experimenting with CI).

Copy link
Contributor
github-actions bot commented Jul 2, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

operator (https://github.com/canonical/operator)
+ ops/model.py:1025: note:          def get(self, str, /, default: Binding) -> Binding
+ ops/model.py:1025: note:          @overload
- ops/model.py:1025: note:          def [_T] get(self, str, /, default: Binding | _T) -> Binding | _T
+ ops/model.py:1025: note:          def [_T] get(self, str, /, default: _T) -> Binding | _T

starlette (https://github.com/encode/starlette)
+ starlette/requests.py:137: error: Returning Any from function declared to return "dict[str, Any]"  [no-any-return]

django-stubs (https://github.com/typeddjango/django-stubs)
+ django-stubs/utils/datastructures.pyi:65: error: Unused "type: ignore" comment  [unused-ignore]

pyodide (https://github.com/pyodide/pyodide)
+ pyodide-build/pyodide_build/xbuildenv_releases.py:220: note:     def get(self, str, /, default: str) -> str
- pyodide-build/pyodide_build/xbuildenv_releases.py:220: note:     def [_T] get(self, str, /, default: str | _T) -> str | _T
+ pyodide-build/pyodide_build/xbuildenv_releases.py:220: note:     def [_T] get(self, str, /, default: _T) -> str | _T

discord.py (https://github.com/Rapptz/discord.py)
- ...typeshed_to_test/stdlib/typing.pyi:1013: note: "update" of "TypedDict" defined here
+ ...typeshed_to_test/stdlib/typing.pyi:1015: note: "update" of "TypedDict" defined here
- ...typeshed_to_test/stdlib/typing.pyi:1013: note: "update" of "TypedDict" defined here
+ ...typeshed_to_test/stdlib/typing.pyi:1015: note: "update" of "TypedDict" defined here

core (https://github.com/home-assistant/core)
+ homeassistant/components/transmission/coordinator.py:63: error: Returning Any from function declared to return "int"  [no-any-return]
+ homeassistant/components/transmission/coordinator.py:68: error: Returning Any from function declared to return "str"  [no-any-return]
+ homeassistant/components/islamic_prayer_times/coordinator.py:57: error: Returning Any from function declared to return "str"  [no-any-return]
+ homeassistant/components/islamic_prayer_times/coordinator.py:71: error: Returning Any from function declared to return "str"  [no-any-return]
+ homeassistant/components/islamic_prayer_times/coordinator.py:76: error: Returning Any from function declared to return "str"  [no-any-return]
+ homeassistant/components/bthome/coordinator.py:48: error: Returning Any from function declared to return "bool"  [no-any-return]
+ homeassistant/components/androidtv_remote/helpers.py:28: error: Returning Any from function declared to return "bool"  [no-any-return]
+ homeassistant/components/xiaomi_ble/coordinator.py:70: error: Returning Any from function declared to return "bool"  [no-any-return]
+ homeassistant/components/unifiprotect/data.py:96: error: Returning Any from function declared to return "bool"  [no-any-return]
+ homeassistant/components/unifiprotect/data.py:101: error: Returning Any from function declared to return "int"  [no-any-return]
+ homeassistant/components/shelly/utils.py:454: error: Returning Any from function declared to return "int"  [no-any-return]
+ homeassistant/components/mikrotik/coordinator.py:86: error: Returning Any from function declared to return "bool"  [no-any-return]
+ homeassistant/components/mikrotik/coordinator.py:91: error: Returning Any from function declared to return "bool"  [no-any-return]
+ homeassistant/components/bthome/device_trigger.py:73: error: Returning Any from function declared to return "list[str]"  [no-any-return]
+ homeassistant/components/squeezebox/media_player.py:292: error: Returning Any from function declared to return "int"  [no-any-return]
+ homeassistant/components/shelly/coordinator.py:166: error: Returning Any from function declared to return "int"  [no-any-return]

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/ext/commands/utils.py:43: note:          def get(self, str, /, default: _VT) -> _VT
+ steam/ext/commands/utils.py:43: note:          @overload
- steam/ext/commands/utils.py:43: note:          def [_T] get(self, str, /, default: _VT | _T) -> _VT | _T
+ steam/ext/commands/utils.py:43: note:          def [_T] get(self, str, /, default: _T) -> _VT | _T

ignite (https://github.com/pytorch/ignite)
+ ignite/handlers/visdom_logger.py:182: error: Redundant cast to "str"  [redundant-cast]

@ndmitchell
Copy link
Contributor Author

Looking at home-assistant/core and encode/starlette these are both approximately:

def f(x: dict[str, Any]) -> int:
    return d.get("field", 0)

The error is no-any-return which seems correct - the return type should be inferred to be Any | int and mypy now complains about that.

Copy link
Collaborator
@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good to me!

Copy link
Member
@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@AlexWaygood AlexWaygood merged commit dbd3ad3 into python:main Jul 2, 2025
67 checks passed
facebook-github-bot pushed a commit to facebook/pyrefly that referenced this pull request Jul 2, 2025
Summary: Fixes mapping.get as per python/typeshed#14360.

Reviewed By: yangdanny97

Differential Revision: D77657698

fbshipit-source-id: 02a17faf5a608a800eb8913a39b28293e452aa36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0