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

Does override need to be overloaded explicitly? #109

Closed
bersbersbers opened this issue Oct 17, 2022 · 3 comments
Closed

Does override need to be overloaded explicitly? #109

bersbersbers opened this issue Oct 17, 2022 · 3 comments

Comments

@bersbersbers
Copy link

mypy treats overrides and override differently.

(Note that I reported no-redef separately as python/mypy#13914. This issue is about the misc mypy message.)

""""Bug."""
from typing import Callable, ParamSpec, TypeVar
P = ParamSpec("P")
T = TypeVar("T")

try:
    from overrides import override
except ModuleNotFoundError:
    def override(func: Callable[P, T]) -> Callable[P, T]:  # type: ignore[no-redef]
        return func

print(override)

Incompatible redefinition (redefinition with type "Callable[[Callable[P, T]], Callable[P, T]]", original type overloaded function) [misc]

By contrast, this is fine:

""""No bug."""
from typing import Callable, ParamSpec, TypeVar
P = ParamSpec("P")
T = TypeVar("T")

try:
    from overrides import overrides
except ModuleNotFoundError:
    def overrides(func: Callable[P, T]) -> Callable[P, T]:  # type: ignore[no-redef]
        return func

print(overrides)

I wonder if this is a mypy issue or whether override needs to be overloaded explicitly.

@bersbersbers
Copy link
Author

bersbersbers commented Oct 17, 2022

Not sure why, but

F = TypeVar("F", bound=Callable)
override: Callable[[F], F]

solves both issues:

"""Workaround."""
from typing import Callable, TypeVar

F = TypeVar("F", bound=Callable)
override: Callable[[F], F]
try:
    from overrides import override
except ModuleNotFoundError:
    def override(func: F) -> F:
        return func

print(override)

as well as

"""Workaround."""
from typing import Callable, TypeVar

F = TypeVar("F", bound=Callable)
overrides: Callable[[F], F]
try:
    from overrides import overrides
except ModuleNotFoundError:
    def overrides(func: F) -> F:
        return func

print(overrides)

@mkorpela
Copy link
Owner

Thanks @bersbersbers !

@mkorpela
Copy link
Owner

mkorpela commented Oct 17, 2022

Fixed in 7.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants