@@ -147,14 +147,15 @@ def _populate_type_annotations(
147147 if isinstance (usage_node , nodes .AssignName ) and isinstance (
148148 usage_node .parent , (nodes .AnnAssign , nodes .Assign )
149149 ):
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 )
158159
159160 if isinstance (usage_node , nodes .FunctionDef ):
160161 self ._populate_type_annotations_function (
@@ -194,24 +195,25 @@ def _populate_type_annotations_annotation(
194195 """Handles the possibility of an annotation either being a Name, i.e. just type,
195196 or a Subscript e.g. `Optional[type]` or an Attribute, e.g. `pylint.lint.linter`.
196197 """
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 () if node .name not in all_used_type_annotations :
200+ all_used_type_annotations [node .name ] = True
201+ return node .name # type: ignore[no-any-return]
202+ case nodes .Subscript (): # e.g. Optional[List[str]]
203+ # slice is the next nested type
204+ self ._populate_type_annotations_annotation (
205+ node .slice , all_used_type_annotations
206+ )
207+ # value is the current type name: could be a Name or Attribute
208+ return self ._populate_type_annotations_annotation (
209+ node .value , all_used_type_annotations
210+ )
211+ case nodes .Attribute ():
212+ # An attribute is a type like `pylint.lint.pylinter`. node.expr is the next level
213+ # up, could be another attribute
214+ return self ._populate_type_annotations_annotation (
215+ node .expr , all_used_type_annotations
216+ )
215217 return None
216218
217219 @staticmethod
0 commit comments