Skip to content

Argument 2 to pop of MutableMapping has incompatible type #10152

Closed
@NiklasRosenstein

Description

@NiklasRosenstein

Bug Report

Using MutableMapping.pop(key, default) inconsistently results in an error for the type of the default argument.

To Reproduce

import typing as t
T = t.TypeVar('T')
V = t.TypeVar('V')
def test(key: T, value: V):
  values: t.Dict[T, V] = {}
  print(values.pop(key, None))  # ok
  values.pop(key, value)        # ok
  values.pop(key, None)         # error: Argument 2 to "pop" of "MutableMapping" has incompatible type "None"; expected "V"
  values.pop(key, 42)           # error: Argument 2 to "pop" of "MutableMapping" has incompatible type "int"; expected "V"

Expected Behavior

I expect no errors when checking the above code with Mypy. The definition of MutableMapping.pop() allows using a different type for the default argument value (in which case a union of the map's value type and the default argument's type is returned).

class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
    # ....
    @overload
    def pop(self, key: _KT, default: Union[_VT, _T] = ...) -> Union[_VT, _T]: ...

What is curious is that the line with print() does not result in an error, even though the .pop() call is the same as two lines below.

Your Environment

  • Mypy version used: 0.782, 0.812
  • Mypy command-line flags: N/a
  • Mypy configuration options from mypy.ini (and other config files): N/a
  • Python version used: CPython 3.7.3
  • Operating system and version: WSL Debian 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions