@@ -147,14 +147,15 @@ def _populate_type_annotations(
147
147
if isinstance (usage_node , nodes .AssignName ) and isinstance (
148
148
usage_node .parent , (nodes .AnnAssign , nodes .Assign )
149
149
):
150
- assign_parent = usage_node .parent
151
- if isinstance (assign_parent , nodes .AnnAssign ):
152
- name_assignments .append (assign_parent )
153
- private_name = self ._populate_type_annotations_annotation (
154
- usage_node .parent .annotation , all_used_type_annotations
155
- )
156
- elif isinstance (assign_parent , nodes .Assign ):
157
- name_assignments .append (assign_parent )
150
+ match assign_parent := usage_node .parent :
151
+ case nodes .AnnAssign ():
152
+ name_assignments .append (assign_parent )
153
+ private_name = self ._populate_type_annotations_annotation (
154
+ assign_parent .annotation ,
155
+ all_used_type_annotations ,
156
+ )
157
+ case nodes .Assign ():
158
+ name_assignments .append (assign_parent )
158
159
159
160
if isinstance (usage_node , nodes .FunctionDef ):
160
161
self ._populate_type_annotations_function (
@@ -194,24 +195,23 @@ def _populate_type_annotations_annotation(
194
195
"""Handles the possibility of an annotation either being a Name, i.e. just type,
195
196
or a Subscript e.g. `Optional[type]` or an Attribute, e.g. `pylint.lint.linter`.
196
197
"""
197
- if isinstance (node , nodes .Name ) and node .name not in all_used_type_annotations :
198
- all_used_type_annotations [node .name ] = True
199
- return node .name # type: ignore[no-any-return]
200
- if isinstance (node , nodes .Subscript ): # e.g. Optional[List[str]]
201
- # slice is the next nested type
202
- self ._populate_type_annotations_annotation (
203
- node .slice , all_used_type_annotations
204
- )
205
- # value is the current type name: could be a Name or Attribute
206
- return self ._populate_type_annotations_annotation (
207
- node .value , all_used_type_annotations
208
- )
209
- if isinstance (node , nodes .Attribute ):
210
- # An attribute is a type like `pylint.lint.pylinter`. node.expr is the next level
211
- # up, could be another attribute
212
- return self ._populate_type_annotations_annotation (
213
- node .expr , all_used_type_annotations
214
- )
198
+ match node :
199
+ case nodes .Name (name = n ) if n not in all_used_type_annotations :
200
+ all_used_type_annotations [n ] = True
201
+ return n # type: ignore[no-any-return]
202
+ case nodes .Subscript (slice = s , value = v ):
203
+ # slice is the next nested type
204
+ self ._populate_type_annotations_annotation (s , all_used_type_annotations )
205
+ # value is the current type name: could be a Name or Attribute
206
+ return self ._populate_type_annotations_annotation (
207
+ v , all_used_type_annotations
208
+ )
209
+ case nodes .Attribute (expr = e ):
210
+ # An attribute is a type like `pylint.lint.pylinter`. node.expr is the next level
211
+ # up, could be another attribute
212
+ return self ._populate_type_annotations_annotation (
213
+ e , all_used_type_annotations
214
+ )
215
215
return None
216
216
217
217
@staticmethod
0 commit comments