-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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_CHECKS_GUARDS fooled by import alias #7524
Comments
Work-around for pylint-dev/pylint#7524
@cdce8p I think this was intentional for better performance, right ? |
I didn't see anything in the #2834 close discussion about it being intentional, but that might have come up in TYPE_CHECKS_GUARDS original intention. (apologies: it's PyCharm's type system that can't do |
Aha in #6787 a code fix for |
You're quite right; this diff fixes it: diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 38a3a7cc2..efe4aedae 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -1230,8 +1230,7 @@ class VariablesChecker(BaseChecker):
# Do not take in account redefined names for the purpose
# of type checking.:
if any(
- isinstance(definition.parent, nodes.If)
- and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS
+ in_type_checking_block(definition)
for definition in globs[name]
):
continue
|
Yeah. I didn't want to do inference for each name. The question quickly becomes where do you stop? What about this for example? from typing import TYPE_CHECKING as TC You also don't have to use For the initial implementation I though that supporting only If you've found a solution which isn't too costly and works, that would be fine. |
Bug description
Configuration
Default
Command used
Pylint output
Expected behavior
Whether TYPE_CHECKING is imported from typing or called as typing.TYPE_CHECKING or imported as an alias should not matter to whether TYPE_CHECKING is called.
I looked at the code and realize that this is a difficult bug to fix -- it probably would involve putting a Mock around typing.TYPE_CHECKING before the module is imported so that the ast tree can report whether it's in a TYPE_CHECKING context or not. This might be a bug left open for a while, but I do think it's an actual issue that might not be resolved until Python has a syntax similar to TypeScript's "from collections import type defaultdict"
I've noticed the same issue with @t.overload on mypy not properly recognizing it as a overload decorator, so the solution was something I could see.
Because it can be a pain to constantly update a
from typing import ....
statement at the top of a module when adding types, my project,music21
uses a standard of importing typing as t at the top of each module so it's easy to know thatt.Union[t.Dict[t.Optional[str]]
will always be available. (When our min Py version goes to 3.9/3.10, there'll be a lot less of this).Thanks for an amazing tool!
Pylint version
OS / Environment
MacOS 12.6
Additional dependencies
None
The text was updated successfully, but these errors were encountered: