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

TYPE_CHECKING doesn't work if imported with alias #12928

Open
DetachHead opened this issue Jun 2, 2022 · 3 comments
Open

TYPE_CHECKING doesn't work if imported with alias #12928

DetachHead opened this issue Jun 2, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@DetachHead
Copy link
Contributor

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING as amogus

if not TYPE_CHECKING:
    1 + "" # no error (unreachable)
    
if not amogus:
    1 + "" # error

playground

@DetachHead DetachHead added the bug mypy got something wrong label Jun 2, 2022
@DetachHead
Copy link
Contributor Author

it also doesn't care if i define a fake TYPE_CHECKING variable

TYPE_CHECKING = False

if not TYPE_CHECKING:
    1 + "" # no error even though this is reachable at runtime

@KotlinIsland

This comment was marked as off-topic.

@pranavrajpal
Copy link
Contributor

it also doesn't care if i define a fake TYPE_CHECKING variable

I think that's probably a remnant of when you couldn't import TYPE_CHECKING (or MYPY, which was an earlier version of the same thing) from typing so defining a False constant with the right name was the recommended usage (see the first example in #308).

That behavior could be considered a feature if you wanted to be able to add types to Python 2 code without having to install the typing backport, but, judging from #12237, we're probably fine making breaking changes for Python 2 users.

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

3 participants