Thanks to visit codestin.com
Credit goes to github.com

Skip to content

@override warnings are silenced by accidentally type-annotating override #15293

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

Closed
bersbersbers opened this issue May 24, 2023 · 4 comments
Closed
Labels
bug mypy got something wrong

Comments

@bersbersbers
Copy link

Bug Report

I am thinking about using override on Python < 3.12 without having to have typing_extensions as a forced run-time dependency. I am using a conditional import for this, which I had previously also done for overrides.override, see #13914. However, when I leave this type annotation in place for typing_extensions.override, all @override warnings are gone.

To Reproduce

from typing import Any, Callable, TypeVar

F = TypeVar("F", bound=Callable[..., Any])

# this silences @override even if typing_extensions is installed.
override: Callable[[F], F]

try:
    from typing_extensions import override
except ModuleNotFoundError:

    def override(method: F, /) -> F:
        return method

class ClassA:
    ...

class ClassB(ClassA):
    @override
    def this_should_error(self) -> None:
        pass

https://gist.github.com/mypy-play/74cf551a06f0cc5b56ddc8cc96708ab8

Expected Behavior

Mypy error about this_should_error

Actual Behavior

No error

Your Environment

  • Mypy version used: master on playground
@bersbersbers bersbersbers added the bug mypy got something wrong label May 24, 2023
@ikonst
Copy link
Contributor

ikonst commented May 24, 2023

Shorter repro:
https://mypy-play.net/?mypy=master&python=3.12&gist=985cd92890a29ba55c4a37bf818e526a

The trouble is that override is created as a local reference. If you change

-override: Callable[[F], F]
+override: Callable[[F, int], F]

then you'd get

error: Incompatible import of "override" (imported name has type "Callable[[_F], _F]", local name has type "Callable[[F, int], F]")  [assignment]

so the right way to think about it is

-(1) remote reference
+(1) local declaration
+(2) assignment

and since we don't track the value throughout assignments, the override local ref cannot be later "blessed" with being a typing.override.

Does it make sense to either

  • warn on the import?
  • have the import mutate the node's fullname?

@tmke8
Copy link
Contributor

tmke8 commented May 24, 2023

Doesn't it work as expected without the override: Callable[[F], F]?

I would write it like this:

from typing import Any, Callable, TypeVar, TYPE_CHECKING

F = TypeVar("F", bound=Callable[..., Any])

if TYPE_CHECKING:
    from typing_extensions import override
else:

    def override(method: F, /) -> F:
        return method

class ClassA:
    ...

class ClassB(ClassA):
    @override
    def this_should_error(self) -> None:
        pass
    
reveal_type(ClassB.this_should_error)

https://mypy-play.net/?mypy=master&python=3.11&gist=f8cef5ea618b97668d941510ad211550

...which seems to work?

@bersbersbers
Copy link
Author

Doesn't it work as expected without the override: Callable[[F], F]?

Yes, it does - that's why I wrote "accidental" in the title. The override: Callable[[F], F] is/was a leftover from my previous code using the overrides packages, which needs this kind of gymnastics for reasons that are not clear to me.

The point of this issue not to improve my code, but to make you aware that by typing override, one can silently render it useless.

@hauntsaninja
Copy link
Collaborator

Yeah, I think this is a won't fix. As ikonst says, this looks like something that could get rebound. pyright's behaviour is similar, except that it issues a no-redef kind of warning that you were looking to avoid in #13914

tmke8's suggestion of if TYPE_CHECKING is a good way to go for this kind of thing. Type checkers are special cased to always know about typing_extensions even if it's not installed, so everything should just work.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants