Skip to content

Conversation

@randolf-scholz
Copy link
Contributor

@randolf-scholz randolf-scholz commented Jan 16, 2026

Fixes #15271

new overloads:

class dict[K, V]:
    @overload
    def pop(self, key: object, /) -> V: ...
    @overload
    def pop[T](self, key: object, default: T, /) -> V | T: ...

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

@github-actions

This comment has been minimized.

@github-actions
Copy link
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]

@randolf-scholz randolf-scholz changed the title Allow arbitrary types dict pop Allow arbitrary types in dict.pop (2 overloads) Jan 16, 2026
@randolf-scholz
Copy link
Contributor Author

randolf-scholz commented Jan 16, 2026

It seems the 3-overload variant hides some dubious code: take the misc/python/materialize/mzbuild.py:459 case:

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

@randolf-scholz
Copy link
Contributor Author

Another dubious case: rotkehlchen/api/rest.py:457:

# 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']

@randolf-scholz randolf-scholz marked this pull request as ready for review January 16, 2026 23:26
@randolf-scholz
Copy link
Contributor Author

cloudinit/config/cc_ansible.py errors are also obvious false negatives on main:

def run_ansible_pull(pull: AnsiblePull, cfg: dict):
    playbook_name: str = cfg.pop("playbook_name", None)           # PR: error ✅️ main: no error ❌️
    playbook_names: List[str] = cfg.pop("playbook_names", None)   # PR: error ✅️ main: no error ❌️

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.

Several methods on built-in types are too strict.

1 participant