Skip to content

Mypy doesn't catch renaming arguments in overrides #19132

Closed as duplicate
Closed as duplicate

Description

@mathialo

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

Activity

  1. sterliakov commented on May 22, 2025

    @sterliakov
    Collaborator

    Given #1013, seems like mypy intentionally does not want to do that globally. Personally I'd prefer this implemented as a separate strictness flag - some projects may prefer to opt out, but IMO most of the projects should be enforcing same argument names consistently. This is an immediate dupe of #8314 which was closed as a dupe of #1013, so let's keep tracking this feature there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions