Closed as duplicate of#1013
Description
Bug Report
When overriding methods in subclasses, it's important to keep the same argument names since any argument in Python can be given as a keyword argument. If an override in a subclass changes the name of a parameter, it breaks the polymorphism for any user of the class using keyword arguments
To Reproduce
from abc import ABC, abstractmethod
class Adder(ABC):
@abstractmethod
def add(self, a: int, b: int) -> int:
pass
class MyAdder(Adder):
def add(self, a: int, b: int) -> int:
return a + b
class OtherAdder(Adder):
def add(self, a: int, new_b: int) -> int:
return a + new_b
def use_adder(adder: Adder) -> int:
return adder.add(a=1, b=2)
print(f"1 + 2 = {use_adder(MyAdder())}") # works
print(f"1 + 2 = {use_adder(OtherAdder())}") # broken, but type checks in mypy
Expected Behavior
The code example above should not type check, as OtherAdder
is not a valid subclass of Adder
. If I type check this code in pyright
, it correctly identifies this issue:
$ pyright classes.py
/path/to/classes.py
/path/to/classes.py:16:9 - error: Method "add" overrides class "Adder" in an incompatible manner
Parameter 3 name mismatch: base parameter is named "b", override parameter is named "new_b" (reportIncompatibleMethodOverride)
1 error, 0 warnings, 0 informations
Actual Behavior
Mypy lets this error through:
$ mypy classes.py
Success: no issues found in 1 source file
Your Environment
- Mypy version used:
mypy 1.15.0 (compiled: yes)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files): None - Python version used:
Python 3.13.3