python / python/mypy

Reassigned method raises "signature incompatible with supertype"

Open
#12,594 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

A rather simple class with an aliased method raises "signature incompatible with supertype" - which seems to happen due to the method name (i can't really find something else that's wrong).

To Reproduce

class A:
    def hello(self, a: str, b: str):
        pass

    world = hello

class B(A):
    # Also tried without ":A" on the self argument
    def world(self: A, a: str, b: str):
        # Do something slightly different when child-class is the instance
        pass

Expected Behavior

Mypy should pass - as the function signature is correct except for the method name.
It should obviously flag if an argument is missing (or named wrongly).
The only workaround to the above error is to actually implement world in the parent class - which is obviously what i'd like to avoid as that'll be unnecessary code, and one unnecessary additional function call and nesting level.

# type: ignore[override] on the "world" method does not circumvent this problem

Actual Behavior

mypy_decoroator.py:12: error: Signature of "world" incompatible with supertype "A"
mypy_decoroator.py:12: note:      Superclass:
mypy_decoroator.py:12: note:          def hello(self: A, a: str, b: str) -> Any
mypy_decoroator.py:12: note:      Subclass:
mypy_decoroator.py:12: note:          def world(self, a: str, b: str) -> Any

(Write what happened.)

Your Environment

  • Mypy version used:0.942
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): n/A
  • Python version used: 3.9.7
  • Operating system and version: linux

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 minimal class A/B reproducer in the issue and run mypy against it to confirm the reported diagnostic. Trace how the aliased method is compared with the superclass method; done when the compatible alias is accepted while missing or incorrectly named arguments still produce an override error.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.