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

No error when importing typing.type_check_only #11592

Closed
KotlinIsland opened this issue Nov 22, 2021 · 6 comments
Closed

No error when importing typing.type_check_only #11592

KotlinIsland opened this issue Nov 22, 2021 · 6 comments
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

from typing import type_check_only

Success: no issues found in 1 source file

@KotlinIsland KotlinIsland added the bug mypy got something wrong label Nov 22, 2021
@sobolevn
Copy link
Member

Can you please explain why do you think this should be an error?
Do you mean older versions of python that don't have this in typing?

@KotlinIsland
Copy link
Contributor Author

https://docs.python.org/3/library/typing.html#typing.type_check_only

This decorator is itself not available at runtime.

It doesn't actually exist.

> python test.py
Traceback (most recent call last):
  File "/tmp/test.py", line 1, in <module>
    from typing import type_check_only
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: cannot import name 'type_check_only' from 'typing' (/usr/lib/python3.11/typing.py)

@AlexWaygood
Copy link
Member

We want things like this to be legal, though:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from typing import type_check_only

This will not fail at runtime, so a blanket "mypy should raise an error if you ever try to import type_check_only in a non-stub file" rule wouldn't be good enough.

@KotlinIsland
Copy link
Contributor Author

KotlinIsland commented Nov 23, 2021

The docs specify that:

It is mainly intended to mark classes that are defined in type stub files if an implementation returns an instance of a private class

And if currently using it in a "runtime" context is a huge false negative, wouldn't it be better to always error when used in a py file (except for explicit TYPE_CHECKING blocks.) and when you do want to use explicitly put a type ignore on it.

@KotlinIsland
Copy link
Contributor Author

related: #9531

@hauntsaninja
Copy link
Collaborator

Duplicate of #9531

@hauntsaninja hauntsaninja marked this as a duplicate of #9531 Jun 21, 2024
@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2024
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