python / python/mypy

Incompatible signature error when there are overloads that split a Literal union in a subclass

Open
#11,762 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-inheritance topic-literal-types topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When I try to overload the method foo() in B, I get an incompatible signature error from mypy and I think it's a bug.

To Reproduce

  1. Write this code in a file
from typing import Literal, overload


class A:
    def foo(self, bar: Literal["X"] | Literal["Y"]) -> int | bool:
        if bar == "X":
            return 1
        else:
            return True


class B(A):
    @overload
    def foo(self, bar: Literal["X"]) -> int:
        ...

    @overload
    def foo(self, bar: Literal["Y"]) -> bool:
        ...

    def foo(self, bar: Literal["X"] | Literal["Y"]) -> int | bool:
        return super().foo(bar)
  1. Run mypy <filename>

Expected Behavior

In a similar case (below), it does not produce an error so I think it's a bug.

class C:
    def foo(self, bar: bool | float) -> int | str:
        if isinstance(bar, bool):
            return 1
        else:
            return "A"


class D(C):
    @overload
    def foo(self, bar: bool) -> int:
        ...

    @overload
    def foo(self, bar: float) -> str:
        ...

    def foo(self, bar: bool | float) -> int | str:
        return super().foo(bar)

Actual Behavior

mypy produces the following error:

d.py:13: error: Signature of "foo" incompatible with supertype "A"
d.py:13: note:      Superclass:
d.py:13: note:          def foo(self, bar: Union[Literal['X'], Literal['Y']]) -> Union[int, bool]
d.py:13: note:      Subclass:
d.py:13: note:          @overload
d.py:13: note:          def foo(self, bar: Literal['X']) -> int
d.py:13: note:          @overload
d.py:13: note:          def foo(self, bar: Literal['Y']) -> bool
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.920
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.0
  • Operating system and version: Windows 10

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

Put the reproducer in d.py and run mypy with the shown example, then compare it with the bool | float case. Trace the subclass overload signature compatibility check; done means the Literal-union example no longer reports an incompatible signature while existing compatibility checks remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.