Signature incompatible with mapping when key is a union of 2 types
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
I am getting this unexpected error:
overload_mypy.py:15: error: Signature of "__getitem__" incompatible with supertype "dict"
overload_mypy.py:15: note: Superclass:
overload_mypy.py:15: note: def __getitem__(self, Union[KeyEnum, ADDITIONAL_PROPERTY]) -> Union[int, str]
overload_mypy.py:15: note: Subclass:
overload_mypy.py:15: note: @overload
overload_mypy.py:15: note: def __getitem__(self, Literal[KeyEnum.PROP_A]) -> int
overload_mypy.py:15: note: @overload
overload_mypy.py:15: note: def __getitem__(self, ADDITIONAL_PROPERTY) -> str
overload_mypy.py:15: error: Signature of "__getitem__" incompatible with supertype "Mapping"
overload_mypy.py:15: note: def __getitem__(self, Union[KeyEnum, ADDITIONAL_PROPERTY]) -> Union[int, str]
overload_mypy.py:15: note: @overload
overload_mypy.py:15: note: def __getitem__(self, Literal[KeyEnum.PROP_A]) -> int
overload_mypy.py:15: note: @overload
overload_mypy.py:15: note: def __getitem__(self, ADDITIONAL_PROPERTY) -> str
Found 2 errors in 1 file (checked 1 source file)
To Reproduce
run mypy on this code
import enum
import typing
import typing_extensions
class ADDITIONAL_PROPERTY(str):
pass
class KeyEnum(str, enum.Enum):
PROP_A = "prop_a"
OUTPUT_BASE_TYPES = typing.Union[int, str]
class SomeDict(typing.Dict[typing.Union[KeyEnum, ADDITIONAL_PROPERTY], OUTPUT_BASE_TYPES]):
@typing.overload
def __getitem__(self, name: typing_extensions.Literal[KeyEnum.PROP_A]) -> int:
...
@typing.overload
def __getitem__(self, name: ADDITIONAL_PROPERTY) -> str:
...
def __getitem__(
self,
name
):
# dict_instance[name] accessor
return super().__getitem__(name)
Expected Behavior
code should pass a mypy check because keys are constrained to strings or an enum
overloads are exhaustive
Actual Behavior
my reports errors
Your Environment
- Mypy version used: mypy 0.971 (compiled: yes)
- Mypy command-line flags: n/a
- Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: Python 3.9.17 (main, Jun 6 2023, 14:44:03) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running mypy 0.971 on the reproduced overload_mypy.py example and inspect the error at line 15. Trace the overload compatibility check for the Union[KeyEnum, ADDITIONAL_PROPERTY] mapping key and the two getitem overloads. Done means the example passes mypy without the reported superclass and Mapping signature errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100