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