From c9e54946ec9e0074c52b65c18e6fcbac9830c080 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 25 Sep 2019 18:04:31 +0200 Subject: [PATCH] Continue looking for undefined variables in type checking guards. Close #3127 --- pylint/checkers/variables.py | 3 ++- tests/functional/u/undefined_variable.py | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index dcbf4ec0c5..f31df0d38a 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1357,7 +1357,8 @@ def _is_variable_violation( defined_in_or_else = any( target.name == name for target in definition.targets ) - break + if defined_in_or_else: + break if not used_in_branch and not defined_in_or_else: maybee0601 = True diff --git a/tests/functional/u/undefined_variable.py b/tests/functional/u/undefined_variable.py index 909898f16d..0657d7b2b9 100644 --- a/tests/functional/u/undefined_variable.py +++ b/tests/functional/u/undefined_variable.py @@ -270,9 +270,12 @@ def func_should_fail(_dt: datetime): # [used-before-assignment] if TYPE_CHECKING: from collections import Counter + from collections import OrderedDict else: Counter = object + OrderedDict = object -def tick(counter: Counter, name: str) -> None: +def tick(counter: Counter, name: str, dictionary: OrderedDict) -> None: counter[name] += 1 + return dictionary