python / python/mypy

Signature incompatible with mapping when key is a union of 2 types

Open
#15,663 1 comment 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.