Closed
Description
Bug Report
mypy's "override" check is erroneously flagging the type hints for the pop()
method in the following code:
from collections import UserDict
from enum import Enum
import typing as t
KT = t.TypeVar('KT')
VT = t.TypeVar('VT')
class MissingT(Enum):
"""Sentinel used to represent none/missing when None itself can't be used."""
MISSING = 'MISSING'
MISSING = MissingT.MISSING
DT = t.TypeVar('DT') #: for default arguments
ODT = t.Union[DT, MissingT]
class mydict(UserDict[KT, VT], t.MutableMapping[KT, VT]):
@t.overload
def pop(self, __key: KT) -> VT: ...
@t.overload
def pop(self, __key: KT, __default: t.Union[VT, DT]) -> t.Union[VT, DT]: ...
def pop(self, key: KT, default: ODT[DT] = MISSING) -> t.Union[VT, DT]:
...
See https://mypy-play.net/?mypy=latest&python=3.10&gist=eb527d76a58888350b4038002816923f for a live demo.
Other type checkers, such as Pyright, flag no error here. But mypy fails with the following:
main.py:23: error: Signature of "pop" incompatible with supertype "MutableMapping"
main.py:23: note: Superclass:
main.py:23: note: @overload
main.py:23: note: def pop(self, KT) -> VT
main.py:23: note: @overload
main.py:23: note: def [_T] pop(self, KT, Union[VT, _T] = ...) -> Union[VT, _T]
main.py:23: note: Subclass:
main.py:23: note: @overload
main.py:23: note: def pop(self, KT) -> VT
main.py:23: note: @overload
main.py:23: note: def [DT] pop(self, KT, Union[VT, DT]) -> Union[VT, DT]
Found 1 error in 1 file (checked 1 source file)
Comparing the superclass to the subclass, mypy apparently thinks that
def [_T] pop(self, KT, Union[VT, _T] = ...) -> Union[VT, _T]
is incompatible with
def [DT] pop(self, KT, Union[VT, DT]) -> Union[VT, DT]
even though these should be compatible.
Your Environment
- Mypy version used: 0.941 (latest)
- Mypy command-line flags: none
- Python version used: 3.10 (latest)