python / python/mypy

Overriding methods with partially generic signatures leads to mypy errors

Open
#17,363 0 comments 2 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

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

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 with the provided test.py reproduction and run it with mypy 1.10.0 using the shown Python 3.11.3 environment. Trace the override-signature checking path for func2 and compare it with func; done means the reported incompatible-signature error is resolved without weakening the valid override check.

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.