mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Overriding methods with partially generic signatures leads to mypy errors

Open micha91 opened this issue 1 year ago • 0 comments

Bug Report

If I define an abstract method in a base class with generic parameters and one specific parameter, I cannot have a subclass implementing this method with specific parameters without mypy complaining about incompatible signatures. If I don't specify any specific parameters in the superclass I don't get an error.

To Reproduce func has generic parameters only and I can override it with a method with a specific signature func2 has generic parameters, but also defines a specific parameter super_arg. If I override it with a method having specific parameters only, mypy complains about incompatible signatures, although I also have super_arg in my method signature.

import abc
import typing as t


class A(abc.ABC):
    @abc.abstractmethod
    def func(self, *args: t.Any, **kwargs: t.Any) -> str: ...

    @abc.abstractmethod
    def func2(self, *args: t.Any, super_arg: int, **kwargs) -> str: ...

class B(A):
    def func(self, a: str, b: str, c: int = 0) -> str:
        return a + b + str(c)

    def func2(self, a: str, b: str, super_arg: int, c: int = 0) -> str:
        return a + b + str(super_arg) + str(c)

Expected Behavior

I would expect that mypy doesn't fail on this.

Actual Behavior

mypy raises an error because of incompatible signatures:

test.py:16: error: Signature of "func2" incompatible with supertype "A"  [override]
test.py:16: note:      Superclass:
test.py:16: note:          def func2(self, *args: Any, super_arg: int, **kwargs: Any) -> str
test.py:16: note:      Subclass:
test.py:16: note:          def func2(self, a: str, b: str, super_arg: int, c: int = ...) -> str

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.11.3

micha91 avatar Jun 11 '24 14:06 micha91