Skip to content

Fix used-before-assignment for functions/classes defined in type checking guard #7370

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

Merged
merged 1 commit into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7368.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix ``used-before-assignment`` for functions/classes defined in type checking guard.

Closes #7368
10 changes: 7 additions & 3 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,6 @@ def _is_variable_violation(
base_scope_type,
is_recursive_klass,
) -> tuple[bool, bool, bool]:
# pylint: disable=too-many-nested-blocks
maybe_before_assign = True
annotation_return = False
use_outer_definition = False
Expand Down Expand Up @@ -2014,8 +2013,13 @@ def _is_variable_violation(
for target in definition.targets
if isinstance(target, nodes.AssignName)
)
if defined_in_or_else:
break
elif isinstance(
definition, (nodes.ClassDef, nodes.FunctionDef)
):
defined_in_or_else = definition.name == node.name

if defined_in_or_else:
break

if not used_in_branch and not defined_in_or_else:
maybe_before_assign = True
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/u/undefined/undefined_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,24 @@ def func_should_fail(_dt: datetime): # [used-before-assignment]
if TYPE_CHECKING:
from collections import Counter
from collections import OrderedDict
from collections import defaultdict
from collections import UserDict
else:
Counter = object
OrderedDict = object
def defaultdict():
return {}
class UserDict(dict):
pass


def tick(counter: Counter, name: str, dictionary: OrderedDict) -> OrderedDict:
counter[name] += 1
return dictionary

defaultdict()

UserDict()

# pylint: disable=unused-argument
def not_using_loop_variable_accordingly(iterator):
Expand Down
18 changes: 9 additions & 9 deletions tests/functional/u/undefined/undefined_variable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ undefined-variable:171:4:171:13::Undefined variable 'unicode_3':UNDEFINED
undefined-variable:226:25:226:37:LambdaClass4.<lambda>:Undefined variable 'LambdaClass4':UNDEFINED
undefined-variable:234:25:234:37:LambdaClass5.<lambda>:Undefined variable 'LambdaClass5':UNDEFINED
used-before-assignment:255:26:255:34:func_should_fail:Using variable 'datetime' before assignment:HIGH
undefined-variable:282:18:282:24:not_using_loop_variable_accordingly:Undefined variable 'iteree':UNDEFINED
undefined-variable:299:27:299:28:undefined_annotation:Undefined variable 'x':UNDEFINED
used-before-assignment:300:7:300:8:undefined_annotation:Using variable 'x' before assignment:HIGH
undefined-variable:330:11:330:12:decorated3:Undefined variable 'x':UNDEFINED
undefined-variable:335:19:335:20:decorated4:Undefined variable 'y':UNDEFINED
undefined-variable:356:10:356:20:global_var_mixed_assignment:Undefined variable 'GLOBAL_VAR':HIGH
undefined-variable:368:19:368:44:RepeatedReturnAnnotations.x:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
undefined-variable:370:19:370:44:RepeatedReturnAnnotations.y:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
undefined-variable:372:19:372:44:RepeatedReturnAnnotations.z:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
undefined-variable:291:18:291:24:not_using_loop_variable_accordingly:Undefined variable 'iteree':UNDEFINED
undefined-variable:308:27:308:28:undefined_annotation:Undefined variable 'x':UNDEFINED
used-before-assignment:309:7:309:8:undefined_annotation:Using variable 'x' before assignment:HIGH
undefined-variable:339:11:339:12:decorated3:Undefined variable 'x':UNDEFINED
undefined-variable:344:19:344:20:decorated4:Undefined variable 'y':UNDEFINED
undefined-variable:365:10:365:20:global_var_mixed_assignment:Undefined variable 'GLOBAL_VAR':HIGH
undefined-variable:377:19:377:44:RepeatedReturnAnnotations.x:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
undefined-variable:379:19:379:44:RepeatedReturnAnnotations.y:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED
undefined-variable:381:19:381:44:RepeatedReturnAnnotations.z:Undefined variable 'RepeatedReturnAnnotations':UNDEFINED