Skip to content

Commit 646b503

Browse files
committed
Handle type subscript
1 parent 4b5b939 commit 646b503

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

pylint/checkers/variables.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,11 +2542,12 @@ def _is_never_evaluated(
25422542

25432543
@staticmethod
25442544
def _is_variable_annotation_in_function(node: nodes.Name) -> bool:
2545+
ann_assign = utils.get_node_first_ancestor_of_type(node, nodes.AnnAssign)
25452546
return (
2546-
isinstance(node.parent, nodes.AnnAssign)
2547-
and node is node.parent.annotation
2547+
ann_assign
2548+
and (node is ann_assign.annotation or ann_assign.annotation.parent_of(node))
25482549
and utils.get_node_first_ancestor_of_type( # type: ignore[return-value]
2549-
node.parent, nodes.FunctionDef
2550+
ann_assign, nodes.FunctionDef
25502551
)
25512552
)
25522553

tests/functional/u/used/used_before_assignment_scoping.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ def func():
1616
first = datetime.now() # [used-before-assignment]
1717
second = datetime.now()
1818
return first, second
19+
20+
21+
def annotated_declaration_with_type_subscript():
22+
dt_list: list[datetime]
23+
dt_list = [datetime.now(), datetime.now()] # [used-before-assignment]
24+
25+
return dt_list
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
used-before-assignment:10:13:10:21:func_two:Using variable 'datetime' before assignment:INFERENCE
22
used-before-assignment:16:12:16:20:func:Using variable 'datetime' before assignment:INFERENCE
3+
used-before-assignment:23:15:23:23:annotated_declaration_with_type_subscript:Using variable 'datetime' before assignment:INFERENCE

0 commit comments

Comments
 (0)