Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible override when overriding non-overload with overload #12379

Open
JelleZijlstra opened this issue Mar 19, 2022 · 2 comments
Open

Incompatible override when overriding non-overload with overload #12379

JelleZijlstra opened this issue Mar 19, 2022 · 2 comments
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides topic-overloads

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Mar 19, 2022

from typing import overload, Union

class A:
    def f(self, x: Union[int, str]) -> None: ...
    
class B(A):
    @overload
    def f(self, x: int) -> None: ...
    @overload
    def f(self, x: str) -> None: ...
    def f(self, x: Union[int, str]) -> None: ...

https://mypy-play.net/?mypy=latest&python=3.10&gist=37c2291afd7ddd170576b1de208ba77c

produces:

main.py:7: error: Signature of "f" incompatible with supertype "A"
main.py:7: note:      Superclass:
main.py:7: note:          def f(self, x: Union[int, str]) -> None
main.py:7: note:      Subclass:
main.py:7: note:          @overload
main.py:7: note:          def f(self, x: int) -> None
main.py:7: note:          @overload
main.py:7: note:          def f(self, x: str) -> None
Found 1 error in 1 file (checked 1 source file)

But as far as I can see this is safe. Mypy accepts a union as an argument to B.f:

arg: Union[int, str]
B().f(arg)

Of course, the overloads are useless in this toy example, but the error also comes up if you vary the return type in the overloads. This came up in python/typeshed#7510.

@JelleZijlstra JelleZijlstra added bug mypy got something wrong topic-overloads labels Mar 19, 2022
@JelleZijlstra
Copy link
Member Author

Possibly a duplicate of #495

@AlexWaygood AlexWaygood added the topic-inheritance Inheritance and incompatible overrides label Mar 24, 2022
@DevilXD
Copy link

DevilXD commented Mar 26, 2022

This can also go the other way around - something I encountered while working with Tkinter and subclassing widgets:

from __future__ import annotations

from typing import Any, overload

# defined within the tkinter itself (roughly)
class BaseWidget:
    @overload
    def configure(self, arg: int) -> int:
        ...

    @overload
    def configure(self, arg: str) -> str:
        ...

    def configure(self, arg: int | str) -> int | str:
        ...

    config = configure  # alias for the 'configure' method

# my subclass
class SubWidget(BaseWidget):
    # overwrite 'configure' with a pass-through function that runs some extra code first
    # NOTE: 'Any' usage to not have to replicate the overloads, this type-checks just fine
    def configure(self, *args: Any, **kwargs: Any) -> Any:
        ...  # some extra code
        super().configure(*args, **kwargs)

    # ..., but the alias complains
    config = configure  # error: Incompatible types in assignment (expression has type "Callable[[SubWidget, VarArg(Any), KwArg(Any)], Any]", base class "BaseWidget" defined the type as overloaded function)

Current workaround:

    def config(self, *args: Any, **kwargs: Any) -> Any:
        self.configure(*args, **kwargs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-inheritance Inheritance and incompatible overrides topic-overloads
Projects
None yet
Development

No branches or pull requests

3 participants