-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Make Mapping.get(default) less constrained #14347
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
base: main
Are you sure you want to change the base?
Conversation
At the moment we say that `default` can either be the same as the value type, or anything it wants to be. Given: ``` def f(mapping: Mapping[str, str]): reveal_type(mapping.get("x", "y")) ``` The `_T` is entirely unconstrained, and thus `str | int` is a perfectly valid type. This change makes it obviously `str | str` which collapses to `str` correctly. This fix makes Pyrefly happier, although Pyright already seems to deal with it, but this doesn't harm Pyright.
Happy to iterate on the stubtest failures, but would appreciate broad feedback if this seems like the right thing to do first. |
Diff from mypy_primer, showing the effect of this PR on open source code: operator (https://github.com/canonical/operator)
- ops/model.py:1021: note: def [_T] get(self, str, /, default: Binding | _T) -> Binding | _T
+ ops/model.py:1021: note: def [_T] get(self, str, /, default: _T) -> Binding | _T
pydantic (https://github.com/pydantic/pydantic)
+ pydantic/_internal/_generate_schema.py:661: error: Need type annotation for "metadata" [var-annotated]
starlette (https://github.com/encode/starlette)
+ starlette/responses.py:262: error: No overload variant of "get" of "dict" matches argument types "str", "str" [call-overload]
+ starlette/responses.py:262: note: Possible overload variants:
+ starlette/responses.py:262: note: def get(self, Never, None = ..., /) -> None
+ starlette/responses.py:262: note: def get(self, Never, Never, /) -> Never
+ starlette/responses.py:262: note: def [_T] get(self, Never, _T, /) -> _T
colour (https://github.com/colour-science/colour)
+ colour/geometry/primitives.py:142: error: Incompatible types in assignment (expression has type "Any | str", variable has type "Literal['-x', '+x', '-y', '+y', '-z', '+z', 'xy', 'xz', 'yz', 'yx', 'zx', 'zy']") [assignment]
cibuildwheel (https://github.com/pypa/cibuildwheel)
+ cibuildwheel/__main__.py:268: error: Argument 2 to "get" of "Mapping" has incompatible type "Literal['']"; expected "Literal['auto', 'linux', 'macos', 'windows'] | None" [arg-type]
typeshed-stats (https://github.com/AlexWaygood/typeshed-stats)
+ src/typeshed_stats/gather.py:424: error: No overload variant of "get" of "dict" matches argument types "str", "dict[Never, Never]" [call-overload]
+ src/typeshed_stats/gather.py:424: note: Possible overload variants:
+ src/typeshed_stats/gather.py:424: note: def get(self, Never, None = ..., /) -> None
+ src/typeshed_stats/gather.py:424: note: def get(self, Never, Never, /) -> Never
+ src/typeshed_stats/gather.py:424: note: def [_T] get(self, Never, _T, /) -> _T
pyodide (https://github.com/pyodide/pyodide)
- 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
freqtrade (https://github.com/freqtrade/freqtrade)
+ freqtrade/exchange/exchange.py:2692: error: Incompatible types in assignment (expression has type "dict[tuple[str, str, CandleType], DataFrame]", variable has type "dict[tuple[str, str, CandleType], Any | None]") [assignment]
+ freqtrade/exchange/exchange.py:2692: note: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ freqtrade/exchange/exchange.py:2692: note: Consider using "Mapping" instead, which is covariant in the value type
+ freqtrade/exchange/exchange.py:2695: error: Incompatible return value type (got "dict[tuple[str, str, CandleType], Any | None]", expected "dict[tuple[str, str, CandleType], DataFrame]") [return-value]
discord.py (https://github.com/Rapptz/discord.py)
+ discord/embeds.py:230: error: Argument 2 to "get" of "Mapping" has incompatible type "None"; expected "Literal['rich', 'image', 'video', 'gifv', 'article', 'link', 'poll_result']" [arg-type]
zulip (https://github.com/zulip/zulip)
+ zerver/lib/markdown/api_arguments_table_generator.py:222: error: Need type annotation for "object_values" [var-annotated]
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
+ src/scikit_build_core/builder/get_requires.py:83: error: Argument 2 to "get" of "Mapping" has incompatible type "str"; expected "bool" [arg-type]
mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ mitmproxy/http.py:735: error: Argument 2 to "get" of "Mapping" has incompatible type "None"; expected "str" [arg-type]
+ mitmproxy/addons/savehar.py:206: error: Unsupported target for indexed assignment ("object | Any") [index]
+ mitmproxy/addons/savehar.py:207: error: Unsupported target for indexed assignment ("object | Any") [index]
+ mitmproxy/addons/savehar.py:211: error: Unsupported target for indexed assignment ("object | Any") [index]
+ mitmproxy/addons/savehar.py:213: error: Unsupported target for indexed assignment ("object | Any") [index]
kopf (https://github.com/nolar/kopf)
+ kopf/_cogs/structs/bodies.py:239: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_cogs/structs/bodies.py:239: note: Possible overload variants:
+ kopf/_cogs/structs/bodies.py:239: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/bodies.py:239: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/bodies.py:239: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/structs/bodies.py:240: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_cogs/structs/bodies.py:240: note: Possible overload variants:
+ kopf/_cogs/structs/bodies.py:240: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/bodies.py:240: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/bodies.py:240: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/structs/bodies.py:241: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_cogs/structs/bodies.py:241: note: Possible overload variants:
+ kopf/_cogs/structs/bodies.py:241: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/bodies.py:241: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/bodies.py:241: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/structs/bodies.py:266: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_cogs/structs/bodies.py:266: note: Possible overload variants:
+ kopf/_cogs/structs/bodies.py:266: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/bodies.py:266: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/bodies.py:266: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/structs/bodies.py:267: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_cogs/structs/bodies.py:267: note: Possible overload variants:
+ kopf/_cogs/structs/bodies.py:267: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/bodies.py:267: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/bodies.py:267: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/structs/finalizers.py:14: error: No overload variant of "get" of "dict" matches argument types "str", "None" [call-overload]
+ kopf/_cogs/structs/finalizers.py:14: note: Possible overload variants:
+ kopf/_cogs/structs/finalizers.py:14: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/finalizers.py:14: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/finalizers.py:14: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/structs/finalizers.py:21: error: No overload variant of "get" of "dict" matches argument types "str", "list[Never]" [call-overload]
+ kopf/_cogs/structs/finalizers.py:21: note: Possible overload variants:
+ kopf/_cogs/structs/finalizers.py:21: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/finalizers.py:21: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/finalizers.py:21: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/structs/finalizers.py:32: error: No overload variant of "get" of "dict" matches argument types "str", "list[Never]" [call-overload]
+ kopf/_cogs/structs/finalizers.py:32: note: Possible overload variants:
+ kopf/_cogs/structs/finalizers.py:32: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/finalizers.py:32: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/finalizers.py:32: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/structs/finalizers.py:44: error: No overload variant of "get" of "dict" matches argument types "str", "list[Never]" [call-overload]
+ kopf/_cogs/structs/finalizers.py:44: note: Possible overload variants:
+ kopf/_cogs/structs/finalizers.py:44: note: def get(self, Never, None = ..., /) -> None
+ kopf/_cogs/structs/finalizers.py:44: note: def get(self, Never, Never, /) -> Never
+ kopf/_cogs/structs/finalizers.py:44: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_cogs/configs/conventions.py:70: error: Need type annotation for "owners" [var-annotated]
+ kopf/_core/actions/loggers.py:132: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_core/actions/loggers.py:132: note: Possible overload variants:
+ kopf/_core/actions/loggers.py:132: note: def get(self, Never, None = ..., /) -> None
+ kopf/_core/actions/loggers.py:132: note: def get(self, Never, Never, /) -> Never
+ kopf/_core/actions/loggers.py:132: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_core/actions/loggers.py:133: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_core/actions/loggers.py:133: note: Possible overload variants:
+ kopf/_core/actions/loggers.py:133: note: def get(self, Never, None = ..., /) -> None
+ kopf/_core/actions/loggers.py:133: note: def get(self, Never, Never, /) -> Never
+ kopf/_core/actions/loggers.py:133: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_core/actions/loggers.py:134: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_core/actions/loggers.py:134: note: Possible overload variants:
+ kopf/_core/actions/loggers.py:134: note: def get(self, Never, None = ..., /) -> None
+ kopf/_core/actions/loggers.py:134: note: def get(self, Never, Never, /) -> Never
+ kopf/_core/actions/loggers.py:134: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_core/actions/loggers.py:145: error: No overload variant of "__ror__" of "dict" matches argument type "Mapping[str, object]" [operator]
+ kopf/_core/actions/loggers.py:145: note: Possible overload variants:
+ kopf/_core/actions/loggers.py:145: note: def __ror__(self, dict[Never, Never], /) -> dict[Never, Never]
+ kopf/_core/actions/loggers.py:145: note: def [_T1, _T2] __ror__(self, dict[_T1, _T2], /) -> dict[_T1, _T2]
+ kopf/_core/actions/loggers.py:145: note: Right operand is of type "Any | dict[Never, Never]"
+ kopf/_kits/hierarchies.py:114: error: No overload variant of "get" of "dict" matches argument types "str", "dict[Never, Never]" [call-overload]
+ kopf/_kits/hierarchies.py:114: note: Possible overload variants:
+ kopf/_kits/hierarchies.py:114: note: def get(self, Never, None = ..., /) -> None
+ kopf/_kits/hierarchies.py:114: note: def get(self, Never, Never, /) -> Never
+ kopf/_kits/hierarchies.py:114: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_kits/hierarchies.py:164: error: No overload variant of "get" of "dict" matches argument types "str", "None" [call-overload]
+ kopf/_kits/hierarchies.py:164: note: Possible overload variants:
+ kopf/_kits/hierarchies.py:164: note: def get(self, Never, None = ..., /) -> None
+ kopf/_kits/hierarchies.py:164: note: def get(self, Never, Never, /) -> Never
+ kopf/_kits/hierarchies.py:164: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_kits/hierarchies.py:219: error: No overload variant of "get" of "dict" matches argument types "str", "None" [call-overload]
+ kopf/_kits/hierarchies.py:219: note: Possible overload variants:
+ kopf/_kits/hierarchies.py:219: note: def get(self, Never, None = ..., /) -> None
+ kopf/_kits/hierarchies.py:219: note: def get(self, Never, Never, /) -> Never
+ kopf/_kits/hierarchies.py:219: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_kits/hierarchies.py:227: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ kopf/_kits/hierarchies.py:227: note: Possible overload variants:
+ kopf/_kits/hierarchies.py:227: note: def get(self, Never, None = ..., /) -> None
+ kopf/_kits/hierarchies.py:227: note: def get(self, Never, Never, /) -> Never
+ kopf/_kits/hierarchies.py:227: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_kits/hierarchies.py:250: error: No overload variant of "get" of "dict" matches argument types "str", "None" [call-overload]
+ kopf/_kits/hierarchies.py:250: note: Possible overload variants:
+ kopf/_kits/hierarchies.py:250: note: def get(self, Never, None = ..., /) -> None
+ kopf/_kits/hierarchies.py:250: note: def get(self, Never, Never, /) -> Never
+ kopf/_kits/hierarchies.py:250: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_kits/hierarchies.py:251: error: No overload variant of "get" of "dict" matches argument types "str", "None" [call-overload]
+ kopf/_kits/hierarchies.py:251: note: Possible overload variants:
+ kopf/_kits/hierarchies.py:251: note: def get(self, Never, None = ..., /) -> None
+ kopf/_kits/hierarchies.py:251: note: def get(self, Never, Never, /) -> Never
+ kopf/_kits/hierarchies.py:251: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_kits/hierarchies.py:252: error: No overload variant of "get" of "dict" matches argument types "str", "dict[Never, Never]" [call-overload]
+ kopf/_kits/hierarchies.py:252: note: Possible overload variants:
+ kopf/_kits/hierarchies.py:252: note: def get(self, Never, None = ..., /) -> None
+ kopf/_kits/hierarchies.py:252: note: def get(self, Never, Never, /) -> Never
+ kopf/_kits/hierarchies.py:252: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_core/intents/registries.py:440: error: No overload variant of "get" of "dict" matches argument types "str", "dict[Never, Never]" [call-overload]
+ kopf/_core/intents/registries.py:440: note: Possible overload variants:
+ kopf/_core/intents/registries.py:440: note: def get(self, Never, None = ..., /) -> None
+ kopf/_core/intents/registries.py:440: note: def get(self, Never, Never, /) -> Never
+ kopf/_core/intents/registries.py:440: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_core/intents/registries.py:451: error: No overload variant of "get" of "dict" matches argument types "str", "dict[Never, Never]" [call-overload]
+ kopf/_core/intents/registries.py:451: note: Possible overload variants:
+ kopf/_core/intents/registries.py:451: note: def get(self, Never, None = ..., /) -> None
+ kopf/_core/intents/registries.py:451: note: def get(self, Never, Never, /) -> Never
+ kopf/_core/intents/registries.py:451: note: def [_T] get(self, Never, _T, /) -> _T
+ kopf/_core/engines/indexing.py:233: error: Need type annotation for "meta" [var-annotated]
mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ pymongo/errors.py:302: error: Need type annotation for "wces" [var-annotated]
+ pymongo/errors.py:306: error: Need type annotation for "werrs" [var-annotated]
+ pymongo/errors.py:350: error: Argument 2 to "get" of "Mapping" has incompatible type "dict[Never, Never]"; expected "list[WriteError] | None" [arg-type]
+ pymongo/auth_shared.py:114: error: Need type annotation for "properties" [var-annotated]
+ pymongo/hello.py:183: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ pymongo/hello.py:183: note: Possible overload variants:
+ pymongo/hello.py:183: note: def get(self, Never, None = ..., /) -> None
+ pymongo/hello.py:183: note: def get(self, Never, Never, /) -> Never
+ pymongo/hello.py:183: note: def [_T] get(self, Never, _T, /) -> _T
+ pymongo/pool_options.py:226: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ pymongo/pool_options.py:226: note: Possible overload variants:
+ pymongo/pool_options.py:226: note: def get(self, Never, None = ..., /) -> None
+ pymongo/pool_options.py:226: note: def get(self, Never, Never, /) -> Never
+ pymongo/pool_options.py:226: note: def [_T] get(self, Never, _T, /) -> _T
+ pymongo/pool_options.py:232: error: No overload variant of "get" of "dict" matches argument type "str" [call-overload]
+ pymongo/pool_options.py:232: note: Possible overload variants:
+ pymongo/pool_options.py:232: note: def get(self, Never, None = ..., /) -> None
+ pymongo/pool_options.py:232: note: def get(self, Never, Never, /) -> Never
+ pymongo/pool_options.py:232: note: def [_T] get(self, Never, _T, /) -> _T
+ pymongo/pool_options.py:256: error: Need type annotation for "driver" [var-annotated]
+ pymongo/pool_options.py:259: error: Value of type "Any | None" is not indexable [index]
+ pymongo/pool_options.py:269: error: Value of type "Any | None" is not indexable [index]
+ pymongo/client_options.py:56: error: Argument 1 to "_build_credentials_tuple" has incompatible type "Any | str | None"; expected "str" [arg-type]
+ pymongo/synchronous/collection.py:2855: error: Need type annotation for "options" [var-annotated]
+ pymongo/asynchronous/collection.py:2862: error: Need type annotation for "options" [var-annotated]
core (https://github.com/home-assistant/core)
+ homeassistant/components/hassio/backup.py:666: error: Need type annotation for "restore_errors" [var-annotated]
+ homeassistant/components/hyperion/config_flow.py:206: error: No overload variant of "get" of "dict" matches argument types "str", "Any" [call-overload]
+ homeassistant/components/hyperion/config_flow.py:206: note: Possible overload variants:
+ homeassistant/components/hyperion/config_flow.py:206: note: def get(self, Never, None = ..., /) -> None
+ homeassistant/components/hyperion/config_flow.py:206: note: def get(self, Never, Never, /) -> Never
+ homeassistant/components/hyperion/config_flow.py:206: note: def [_T] get(self, Never, _T, /) -> _T
+ homeassistant/components/onewire/sensor.py:66: error: No overload variant of "get" of "dict" matches argument types "str", "dict[Never, Never]" [call-overload]
+ homeassistant/components/onewire/sensor.py:66: note: Possible overload variants:
+ homeassistant/components/onewire/sensor.py:66: note: def get(self, Never, None = ..., /) -> None
+ homeassistant/components/onewire/sensor.py:66: note: def get(self, Never, Never, /) -> Never
+ homeassistant/components/onewire/sensor.py:66: note: def [_T] get(self, Never, _T, /) -> _T
+ homeassistant/components/denonavr/config_flow.py:273: error: Argument 2 to "get" of "Mapping" has incompatible type "str | None"; expected "str" [arg-type]
+ homeassistant/components/fritz/coordinator.py:519: error: Argument 2 to "get" of "Mapping" has incompatible type "bool"; expected "Mapping[str, Any] | None" [arg-type]
+ homeassistant/components/mobile_app/logbook.py:60: error: Incompatible return value type (got "dict[str, str | Any | None]", expected "dict[str, str]") [return-value]
steam.py (https://github.com/Gobot1234/steam.py)
- 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
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+ src/hydra_zen/wrapper/_implementations.py:1735: error: Argument 2 to "get" of "Mapping" has incompatible type "str | None"; expected "str" [arg-type]
spack (https://github.com/spack/spack)
+ lib/spack/spack/solver/asp.py:4747: error: Need type annotation for "default_include" [var-annotated]
+ lib/spack/spack/solver/asp.py:4748: error: Need type annotation for "default_exclude" [var-annotated]
+ lib/spack/spack/solver/asp.py:4764: error: Argument "include" to "from_environment_included_concrete" of "SpecFilter" has incompatible type "Any | str | list[Any]"; expected "list[str]" [arg-type]
+ lib/spack/spack/solver/asp.py:4765: error: Argument "exclude" to "from_environment_included_concrete" of "SpecFilter" has incompatible type "Any | str | list[Any]"; expected "list[str]" [arg-type]
black (https://github.com/psf/black)
+ src/black/__init__.py:1135:16: error: No overload variant of "get" of "dict" matches argument types "str", "dict[Never, Never]" [call-overload]
+ src/black/__init__.py:1135:16: note: Possible overload variants:
+ src/black/__init__.py:1135:16: note: def get(self, Never, None = ..., /) -> None
+ src/black/__init__.py:1135:16: note: def get(self, Never, Never, /) -> Never
+ src/black/__init__.py:1135:16: note: def [_T] get(self, Never, _T, /) -> _T
xarray (https://github.com/pydata/xarray)
+ xarray/core/indexing.py: note: In function "group_indexers_by_index":
+ xarray/core/indexing.py:141: error: Argument 2 to "get" of "Mapping" has incompatible type "None"; expected "Index" [arg-type]
+ xarray/computation/rolling.py: note: In member "_mapping_to_list" of class "Rolling":
+ xarray/computation/rolling.py:245: error: Argument 2 to "get" of "Mapping" has incompatible type "_T | None"; expected "_T" [arg-type]
|
Could you try a three-overload solution, like we do for Lines 1183 to 1188 in 0c155fe
|
Summary: I think python/typeshed#14347 is the fix to the stdlib, have sent that upstream. Reviewed By: migeed-z Differential Revision: D77434542 fbshipit-source-id: a1f4dadebb2fdaa359577ed885d7cdd9bf7e4686
Summary: I think python/typeshed#14347 is the fix to the stdlib, have sent that upstream. Reviewed By: migeed-z Differential Revision: D77434542 fbshipit-source-id: a1f4dadebb2fdaa359577ed885d7cdd9bf7e4686
At the moment we say that
default
can either be the same as the value type, or anything it wants to be. Given:The
_T
is entirely unconstrained, and thusstr | int
is a perfectly valid type. This change makes it obviouslystr | str
which collapses tostr
correctly.This fix makes Pyrefly happier, although Pyright already seems to deal with it, but this doesn't harm Pyright.