Skip to content

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

Open

Description

@BergLucas

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions