-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Allow arbitrary types in dict.pop (2 overloads)
#15296
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
Open
randolf-scholz
wants to merge
7
commits into
python:main
Choose a base branch
from
randolf-scholz:allow_arbitrary_types_dict_pop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Allow arbitrary types in dict.pop (2 overloads)
#15296
randolf-scholz
wants to merge
7
commits into
python:main
from
randolf-scholz:allow_arbitrary_types_dict_pop
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This comment has been minimized.
This comment has been minimized.
7 tasks
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: pandera (https://github.com/pandera-dev/pandera)
+ pandera/schema_statistics/pandas.py:81: error: Need type annotation for "options" [var-annotated]
spark (https://github.com/apache/spark)
+ python/pyspark/sql/plot/plotly.py:192: error: Argument "bw_method" to "compute_kde_col" of "PySparkKdePlotBase" has incompatible type "Any | None"; expected "int | float" [arg-type]
mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ bson/son.py:126: error: Signature of "pop" incompatible with supertype "builtins.dict" [override]
+ bson/son.py:126: note: Superclass:
+ bson/son.py:126: note: @overload
+ bson/son.py:126: note: def pop(self, object, /) -> _Value
+ bson/son.py:126: note: @overload
+ bson/son.py:126: note: def [_T] pop(self, object, _T, /) -> _Value | _T
+ bson/son.py:126: note: Subclass:
+ bson/son.py:126: note: def [_T] pop(self, key: _Key, *args: _Value | _T) -> _Value | _T
setuptools (https://github.com/pypa/setuptools)
+ setuptools/config/_apply_pyprojecttoml.py:285: error: Need type annotation for "given" [var-annotated]
pylox (https://github.com/sco1/pylox)
- pylox/containers/array.py:83: note: def pop(self, Any, /) -> Any
+ pylox/containers/array.py:83: note: def pop(self, object, /) -> Any
- pylox/containers/array.py:83: note: def pop(self, Any, Any, /) -> Any
- pylox/containers/array.py:83: note: def [_T] pop(self, Any, _T, /) -> Any | _T
+ pylox/containers/array.py:83: note: def [_T] pop(self, object, _T, /) -> Any | _T
alerta (https://github.com/alerta/alerta)
+ alerta/webhooks/prometheus.py:85: error: Argument "resource" to "Alert" has incompatible type "Any | None"; expected "str" [arg-type]
rotki (https://github.com/rotki/rotki)
+ rotkehlchen/chain/aggregator.py:727: error: Unused "type: ignore" comment [unused-ignore]
+ rotkehlchen/api/rest.py:457: error: Value of type "Any | None" is not indexable [index]
+ rotkehlchen/api/rest.py:459: error: Value of type "Any | None" is not indexable [index]
+ rotkehlchen/api/rest.py:460: error: Item "None" of "Any | None" has no attribute "get" [union-attr]
sympy (https://github.com/sympy/sympy)
+ sympy/plotting/plot.py:412: error: Need type annotation for "labels" [var-annotated]
+ sympy/plotting/plot.py:607: error: Need type annotation for "labels" [var-annotated]
+ sympy/plotting/plot.py:738: error: Need type annotation for "labels" [var-annotated]
+ sympy/plotting/plot.py:1037: error: Need type annotation for "labels" [var-annotated]
xarray (https://github.com/pydata/xarray)
+ xarray/plot/facetgrid.py: note: In member "map_plot1d" of class "FacetGrid":
+ xarray/plot/facetgrid.py:450: error: Need type annotation for "cbar_kwargs" [var-annotated]
+ xarray/plot/facetgrid.py: note: At top level:
+ xarray/plot/dataarray_plot.py: note: In function "_plot1d":
+ xarray/plot/dataarray_plot.py:983: error: Need type annotation for "cmap_params_subset" [var-annotated]
+ xarray/plot/dataarray_plot.py:984: error: Need type annotation for "cbar_kwargs" [var-annotated]
+ xarray/indexes/nd_point_index.py: note: In member "from_variables" of class "NDPointIndex":
+ xarray/indexes/nd_point_index.py:269: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "type[T_TreeAdapter]" [arg-type]
materialize (https://github.com/MaterializeInc/materialize)
+ misc/python/materialize/mzbuild.py:459: error: Unsupported operand types for / ("Path" and "None") [operator]
+ misc/python/materialize/mzbuild.py:459: note: Right operand is of type "Any | None"
+ misc/python/materialize/mzbuild.py:461: error: Unsupported operand types for / ("Path" and "None") [operator]
+ misc/python/materialize/mzbuild.py:461: note: Right operand is of type "Any | None"
+ misc/python/materialize/mzbuild.py:464: error: Unsupported operand types for / ("Path" and "None") [operator]
+ misc/python/materialize/mzbuild.py:464: note: Right operand is of type "Any | None"
+ misc/python/materialize/mzbuild.py:508: error: Need type annotation for "bin" [var-annotated]
+ misc/python/materialize/mzbuild.py:510: error: Need type annotation for "example" [var-annotated]
+ misc/python/materialize/mzbuild.py:513: error: Need type annotation for "extract" [var-annotated]
core (https://github.com/home-assistant/core)
+ homeassistant/helpers/check_config.py:160: error: Need type annotation for "core_config" [var-annotated]
+ homeassistant/components/light/__init__.py:330: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "int" [arg-type]
+ homeassistant/components/light/__init__.py:340: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "int" [arg-type]
+ homeassistant/helpers/config_entry_oauth2_flow.py:717: error: Need type annotation for "headers" [var-annotated]
+ homeassistant/components/netatmo/config_flow.py:119: error: Need type annotation for "areas" [var-annotated]
+ homeassistant/components/template/config_flow.py:861: error: Need type annotation for "advanced_options" [var-annotated]
isort (https://github.com/pycqa/isort)
+ isort/output.py:333: error: Argument 1 to "add_to_line" has incompatible type "Any | tuple[()]"; expected "list[str] | None" [arg-type]
+ isort/output.py:471: error: Need type annotation for "use_comments" (hint: "use_comments: list[<type>] = ...") [var-annotated]
+ isort/output.py:473: error: Incompatible types in assignment (expression has type "Any | tuple[()]", variable has type "list[Any]") [assignment]
+ isort/output.py:473: error: Incompatible types in assignment (expression has type "Any | tuple[()] | None", variable has type "list[Any]") [assignment]
+ isort/output.py:500: error: Argument 1 to "add_to_line" has incompatible type "Any | tuple[()]"; expected "list[str] | None" [arg-type]
+ isort/output.py:500: error: Argument 1 to "add_to_line" has incompatible type "Any | tuple[()] | None"; expected "list[str] | None" [arg-type]
+ isort/output.py:534: error: Argument "comments" to "import_statement" has incompatible type "Any | tuple[()] | None"; expected "Sequence[str]" [arg-type]
+ isort/output.py:544: error: Argument "comments" to "import_statement" has incompatible type "Any | tuple[()] | None"; expected "Sequence[str]" [arg-type]
+ isort/output.py:552: error: Argument "comments" to "import_statement" has incompatible type "Any | tuple[()] | None"; expected "Sequence[str]" [arg-type]
+ isort/main.py:1082: error: Need type annotation for "file_names" [var-annotated]
+ isort/main.py:1282: error: Argument 1 to "join" of "str" has incompatible type "Any | Literal[True]"; expected "Iterable[str]" [arg-type]
+ isort/main.py:1288: error: Argument 1 to "join" of "str" has incompatible type "Any | Literal[True]"; expected "Iterable[str]" [arg-type]
django-stubs (https://github.com/typeddjango/django-stubs)
+ django-stubs/http/request.pyi:202: error: Signature of "pop" incompatible with supertype "builtins.dict" [override]
+ django-stubs/http/request.pyi:202: note: Superclass:
+ django-stubs/http/request.pyi:202: note: @overload
+ django-stubs/http/request.pyi:202: note: def pop(self, object, /) -> str
+ django-stubs/http/request.pyi:202: note: @overload
+ django-stubs/http/request.pyi:202: note: def [_T] pop(self, object, _T, /) -> str | _T
+ django-stubs/http/request.pyi:202: note: Subclass:
+ django-stubs/http/request.pyi:202: note: @overload
+ django-stubs/http/request.pyi:202: note: def pop(self, str | bytes, /) -> Never
+ django-stubs/http/request.pyi:202: note: @overload
+ django-stubs/http/request.pyi:202: note: def [_Z] pop(self, str | bytes, str | _Z = ..., /) -> Never
zulip (https://github.com/zulip/zulip)
+ scripts/lib/zulip_tools.py:555: error: Need type annotation for "sudo_args" [var-annotated]
+ zproject/backends.py:1999: error: Need type annotation for "all_received_group_names" [var-annotated]
+ zerver/views/auth.py:956: error: Need type annotation for "extra_context" [var-annotated]
pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/indexers/objects.py:565: error: Argument 2 to "pop" of "dict" has incompatible type "int | BaseIndexer"; expected "int" [arg-type]
ibis (https://github.com/ibis-project/ibis)
+ ibis/backends/snowflake/__init__.py:262: error: Need type annotation for "session_parameters" [var-annotated]
+ ibis/backends/clickhouse/__init__.py:347: error: Need type annotation for "settings" [var-annotated]
prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/server/models/block_schemas.py:510: error: Need type annotation for "child_definitions" [var-annotated]
+ src/prefect/blocks/notifications.py:977: error: Need type annotation for "cookies" [var-annotated]
+ src/prefect/blocks/notifications.py:990: error: Need type annotation for "cookies" [var-annotated]
+ src/prefect/cli/deploy/_core.py:310: error: Argument 2 to "_gather_deployment_trigger_definitions" has incompatible type "Any | None"; expected "list[dict[str, Any]]" [arg-type]
scrapy (https://github.com/scrapy/scrapy)
+ scrapy/utils/datatypes.py:98: error: Signature of "pop" incompatible with supertype "builtins.dict" [override]
+ scrapy/utils/datatypes.py:98: note: Superclass:
+ scrapy/utils/datatypes.py:98: note: @overload
+ scrapy/utils/datatypes.py:98: note: def pop(self, object, /) -> Any
+ scrapy/utils/datatypes.py:98: note: @overload
+ scrapy/utils/datatypes.py:98: note: def [_T] pop(self, object, _T, /) -> Any | _T
+ scrapy/utils/datatypes.py:98: note: Subclass:
+ scrapy/utils/datatypes.py:98: note: def [AnyStr: (str, bytes)] pop(self, key: AnyStr, *args: Any) -> Any
pylint (https://github.com/pycqa/pylint)
+ pylint/config/arguments_provider.py:54: error: Incompatible types in "yield" (actual type "tuple[None, list[tuple[str, dict[str, str | bool | int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None], Any]]]", expected type "tuple[str, list[tuple[str, dict[str, str | bool | int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None], Any]]] | tuple[None, dict[str, list[tuple[str, dict[str, str | bool | int | Pattern[str] | Iterable[str | int | Pattern[str]] | type[_CallbackAction] | Callable[[Any], Any] | Callable[[Any, Any, Any, Any], Any] | None], Any]]]]") [misc]
+ pylint/config/arguments_provider.py:54: note: Error code "misc" not covered by "type: ignore" comment
+ pylint/config/arguments_provider.py:54: error: Unused "type: ignore" comment [unused-ignore]
strawberry (https://github.com/strawberry-graphql/strawberry)
+ strawberry/schema/schema.py:201: error: Argument 13 to "_OperationContextAwareGraphQLResolveInfo" has incompatible type "Any | None"; expected "dict[str, Any]" [arg-type]
pydantic (https://github.com/pydantic/pydantic)
+ pydantic/v1/schema.py:1069: error: Argument "unique_items" to "conlist" has incompatible type "Any | None"; expected "bool" [arg-type]
+ pydantic/v1/decorator.py:181: error: Need type annotation for "var_kwargs" [var-annotated]
+ pydantic/deprecated/decorator.py:202: error: Need type annotation for "var_kwargs" [var-annotated]
cloud-init (https://github.com/canonical/cloud-init)
+ cloudinit/config/cc_ansible.py:277: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "str" [arg-type]
+ cloudinit/config/cc_ansible.py:278: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "list[str]" [arg-type]
meson (https://github.com/mesonbuild/meson)
+ docs/refman/loaderyaml.py:185:19: error: Need type annotation for "posargs" [var-annotated]
+ docs/refman/loaderyaml.py:186:19: error: Need type annotation for "optargs" [var-annotated]
+ docs/refman/loaderyaml.py:188:18: error: Need type annotation for "kwargs" [var-annotated]
+ docs/refman/loaderyaml.py:265:19: error: Need type annotation for "methods" [var-annotated]
discord.py (https://github.com/Rapptz/discord.py)
- ...typeshed_to_test/stdlib/typing.pyi:1046: note: "update" of "TypedDict" defined here
+ ...typeshed_to_test/stdlib/typing.pyi:1044: note: "update" of "TypedDict" defined here
+ discord/ui/view.py:932: error: Unused "type: ignore" comment [unused-ignore]
steam.py (https://github.com/Gobot1234/steam.py)
- steam/ext/commands/utils.py:52: note: def pop(self, str, /) -> _VT
+ steam/ext/commands/utils.py:52: note: def pop(self, object, /) -> _VT
- steam/ext/commands/utils.py:52: note: def pop(self, str, _VT, /) -> _VT
- steam/ext/commands/utils.py:52: note: @overload
- steam/ext/commands/utils.py:52: note: def [_T] pop(self, str, _T, /) -> _VT | _T
+ steam/ext/commands/utils.py:52: note: def [_T] pop(self, object, _T, /) -> _VT | _T
- steam/ext/commands/utils.py:52: note: def pop(self, str, /) -> _VT
+ steam/ext/commands/utils.py:52: note: def pop(self, Any, /) -> _VT
- steam/ext/commands/utils.py:52: note: def pop(self, str, _VT, /) -> _VT
- steam/ext/commands/utils.py:52: note: @overload
- steam/ext/commands/utils.py:52: note: def [_T] pop(self, str, _T, /) -> _VT | _T
+ steam/ext/commands/utils.py:52: note: def [_T] pop(self, Any, _T, /) -> _VT | _T
+ steam/state.py:454: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "CMsgClientFriendsListFriend" [arg-type]
altair (https://github.com/vega/altair)
+ tools/schemapi/codegen.py:273: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "str" [arg-type]
operator (https://github.com/canonical/operator)
- ops/_private/harness.py:2645: error: No overload variant of "pop" of "dict" matches argument types "str", "None" [call-overload]
- ops/_private/harness.py:2645: note: Possible overload variants:
- ops/_private/harness.py:2645: note: def pop(self, int, /) -> dict[str, Any]
- ops/_private/harness.py:2645: note: def pop(self, int, dict[str, Any], /) -> dict[str, Any]
- ops/_private/harness.py:2645: note: def [_T] pop(self, int, _T, /) -> dict[str, Any] | _T
cki-lib (https://gitlab.com/cki-project/cki-lib)
+ cki_lib/yaml.py:146: error: Argument 2 to "validate" has incompatible type "Any | None"; expected "dict[Any, Any] | list[Any] | str" [arg-type]
|
dict.pop (2 overloads)
Contributor
Author
|
It seems the 3-overload variant hides some dubious code: take the class Copy(PreImage):
def __init__(self, rd: RepositoryDetails, path: Path, config: dict[str, Any]):
super().__init__(rd, path)
self.source = config.pop("source", None)
if self.source is None:
raise ValueError("mzbuild config is missing 'source' argument")
self.destination = config.pop("destination", None)
if self.destination is None:
raise ValueError("mzbuild config is missing 'destination' argument")
self.matching = config.pop("matching", "*")
def run(self, prep: Any) -> None:
super().run(prep)
for src in self.inputs():
dst = self.path / self.destination / src
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(self.rd.root / self.source / src, dst)This should probably be written something like this instead: if (dest := config.pop("destination", None)) is None:
raise ValueError("mzbuild config is missing 'destination' argument")
else:
assert isinstance(dest, str | Path)
self.destination = dest |
Contributor
Author
|
Another dubious case: # task_results: : dict[int, Any]
if task_id in self.task_results:
# Task has completed and we just got the outcome
function_response = self.task_results.pop(int(task_id), None)
# The result of the original request
result = function_response['result']master reports no error here, which seems like a sure false negative. This should be rewritten as if (function_response := self.task_results.pop(int(task_id), None)) is not None:
result = function_response['result'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #15271
new overloads:
Note: I remove the redundant looking overload. was this necessary for some mypy quirk? It doesn't seem to make any difference without context: https://mypy-play.net/?mypy=latest&python=3.12&gist=1db2e079cf94bb56b590ea1f385259fd