Skip to content

Make Mapping.get(default) more constrained - #14360

Merged
AlexWaygood merged 18 commits into
python:mainfrom
ndmitchell:main
Jul 2, 2025
Merged

AlexWaygood merged 18 commits into
python:mainfrom
ndmitchell:main

Conversation

@ndmitchell

Copy link
Copy Markdown
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).

@github-actions

github-actions Bot commented Jul 2, 2025

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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.

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

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!

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

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
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