-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
TYPE_CHECKING only imports should trigger errors on non-annotation usage #16587
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
Comments
I don't think we should warn here. It's also easy for simpler linters to catch this sort of thing. |
My understanding is that this was primarily added to help avoid recursive imports just to add annotations It would be nice if it would hint at.missuse of those when type-checking |
I belive this could work if you use postponed evaluation of annotations, but it requires some changes in the code: from __future__ import annotations
from typing import TYPE_CHECKING, cast
if TYPE_CHECKING:
from types import FunctionType
def testit(func: FunctionType) -> None:
from types import FunctionType # <<--- Without this, you won't have `FunctionType` available at runtime
assert isinstance(func, FunctionType)
def something() -> None:
pass
# ------
# As it is, this won't work.
# You must import it beforehand, but the import is conditional to a static type-checking scenario.
testit(cast(FunctionType, something)
# An option is to enclose it in a function instead, and import your circular type inside the implementation,
# just like what we did before.
def execute_testit() -> None:
from types import FunctionType
testit(cast(FunctionType, something) Not pretty, I know. Also, I'm not entirely sure if you can use An alternative to |
@flisboac what i want is a warning when the specific import for runtime use is missing mypy wont error if a import is turned from normal to type checking only and there are actual usages instead of just annotation usages |
Well, then I think @JelleZijlstra 's reasoning is sound.
|
Type checking, in general, was made to reduce errors to code. If it's possible to flag potential errors arising from incomplete types, it seems more reasonable to report those errors. I don't buy the argument that using TYPE_CHECKING is lying to the type checker. Forward types in other languages (C++) are well established and in no way are they "lying to the [compiler]". Also, I think this should produce an error during strict type checking, not a warning. |
Closing as a duplicate of #6104 Jelle is correct that mypy's behaviour matches the spec, but I agree that it would be nice to do something non-standard to issue diagnostics to users if mypy could be sure the code will fail at runtime. |
Bug Report
this should warn for all non-annotation usages of FunctionType
Expected Behavior
warn that FunctionType can only be used in annotations
The text was updated successfully, but these errors were encountered: