-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Conversation
Summary: Test Plan:
Summary: Test Plan:
Summary: Test Plan:
Summary: Test Plan:
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]
|
Looking at home-assistant/core and encode/starlette these are both approximately:
The error is |
There was a problem hiding this 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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Summary: Fixes mapping.get as per python/typeshed#14360. Reviewed By: yangdanny97 Differential Revision: D77657698 fbshipit-source-id: 02a17faf5a608a800eb8913a39b28293e452aa36
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).