[Python] Type Checker Method Overwrites

Discussão em 'Python' iniciado por Stack, Agosto 11, 2025.

  1. Stack

    Stack Membro Participativo

    When I try to overwrite a method in a subclass with different return and additional arguments, Pylance raises these two exceptions (Strict type checking):

    Method "my_method" overrides class "foo" in an incompatible manner
    Positional parameter count mismatch; base method has 3, but override has 4 Pylance (reportIncompatibleMethodOverride)


    Return type mismatch: base method returns type "tuple[Any, Any]", override returns type "tuple[Any, Any, Any]"
    "tuple[Any, Any, Any]" is not assignable to "tuple[Any, Any]"
    Tuple size mismatch; expected 2 but received 3 Pylance (reportIncompatibleMethodOverride)


    from typing import Any

    class foo:
    def my_method(self, arg1: Any, arg2: Any) -> tuple[Any, Any]:
    return arg1, arg2

    class bar(foo):
    def my_method(self, arg1: Any, arg2: Any, arg3: Any) -> tuple[Any, Any, Any]: # Pylance: Method "my_method" overrides class "foo" in an incompatible manner. Positional parameter count mismatch; base method has 3, but override has 4. Return type mismatch: base method returns type "tuple[Any, Any]", override returns type "tuple[Any, Any, Any]". "tuple[Any, Any, Any]" is not assignable to "tuple[Any, Any]". Tuple size mismatch; expected 2 but received 3.
    return arg1, arg2, arg3


    Of course, The obvious solution is to modify the parent classes my_method to fit the return type and parameters. Or just use a different name.

    class foo:
    def my_method(self, arg1: Any, arg2: Any, arg3: Any) -> tuple[Any, Any, Any]:
    return arg1, arg2, None

    class bar(foo):
    def my_method(self, arg1: Any, arg2: Any, arg3: Any) -> tuple[Any, Any, Any]:
    return arg1, arg2, arg3


    The problem now is the solution itself. Type checking is supposed to make your code more readable and easier to maintain. Having a extra return value and additional argument for no real reason gets confusing quickly. As for changing the name, In the real script where the problem arose, One method builds on top the other, using the name local variables and namespace. Changing to a different name for the subclassed method would break this functionality. Programming like this, In hindsight was probably a bad idea, but I'm not interested in a complete rewrite.

    Does anyone have a better solution?

    Continue reading...

Compartilhe esta Página