diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2019-09-25 09:27:44 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-09-25 09:27:44 +0200 |
commit | 2fa5d435df32ff363596de6e0285c13264e4ba30 (patch) | |
tree | 1d62623309e1c37bd8d854c088550e97e2a4f4e3 | |
parent | 26008155931784eb933a5cd3a09f109cf105fd1b (diff) | |
download | pylint-git-2fa5d435df32ff363596de6e0285c13264e4ba30.tar.gz |
Exempt type checking definitions inside the type check guard
In a7f236528bb3758886b97285a56f3f9ce5b13a99 we added basic support
for emitting `used-before-assignment` if a variable was only defined
inside a type checking guard (using `TYPE_CHECKING` variable from `typing`)
Unfortunately that missed the case of using those type checking imports
inside the guard itself, which triggered spurious used-before-assignment errors.
Close #3119
-rw-r--r-- | pylint/checkers/variables.py | 4 | ||||
-rw-r--r-- | tests/functional/u/undefined_variable.py | 8 |
2 files changed, 11 insertions, 1 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 548f910c0..e4be053cd 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1340,7 +1340,9 @@ class VariablesChecker(BaseChecker): maybee0601 = False if isinstance(defstmt, (astroid.Import, astroid.ImportFrom)): defstmt_parent = defstmt.parent - if isinstance(defstmt_parent, astroid.If): + if isinstance( + defstmt_parent, astroid.If + ) and not defstmt_parent.parent_of(node): if defstmt_parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS: maybee0601 = True diff --git a/tests/functional/u/undefined_variable.py b/tests/functional/u/undefined_variable.py index 0967f7a30..71df92960 100644 --- a/tests/functional/u/undefined_variable.py +++ b/tests/functional/u/undefined_variable.py @@ -256,3 +256,11 @@ if TYPE_CHECKING: def func_should_fail(_dt: datetime): # [used-before-assignment] pass + + +# The following should not emit an error. +# pylint: disable=invalid-name +if TYPE_CHECKING: + from typing_extensions import Literal + + AllowedValues = Literal['hello', 'world'] |