|
25 | 25 | import ast |
26 | 26 | import os |
27 | 27 | from collections import defaultdict |
28 | | -from typing import List, Dict, Any, Iterator |
| 28 | +from typing import List, Dict, Any |
29 | 29 |
|
30 | 30 | from . import utils |
31 | 31 |
|
@@ -65,7 +65,7 @@ def _get_type_str(self, node: ast.AST | None) -> str | None: |
65 | 65 | if isinstance(curr, ast.Name): |
66 | 66 | parts.append(curr.id) |
67 | 67 | return ".".join(reversed(parts)) |
68 | | - # Handles subscripted types like 'list[str]', 'Optional[...]' |
| 68 | + # Handles subscripted types like 'list[str]', 'Optional[...]' |
69 | 69 | if isinstance(node, ast.Subscript): |
70 | 70 | value_str = self._get_type_str(node.value) |
71 | 71 | slice_str = self._get_type_str(node.slice) |
@@ -191,16 +191,20 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> None: |
191 | 191 | self._is_in_method = False |
192 | 192 |
|
193 | 193 | def _add_attribute(self, attr_name: str, attr_type: str | None = None): |
194 | | - """Adds a unique attribute to the current class context.""" |
195 | | - if self._current_class_info: |
196 | | - # Create a list of attribute names for easy lookup |
197 | | - attr_names = [ |
198 | | - attr.get("name") for attr in self._current_class_info["attributes"] |
199 | | - ] |
200 | | - if attr_name not in attr_names: |
201 | | - self._current_class_info["attributes"].append( |
202 | | - {"name": attr_name, "type": attr_type} |
203 | | - ) |
| 194 | + """Adds a unique attribute to the current class context. |
| 195 | +
|
| 196 | + Assumes self._current_class_info is not None, as this method |
| 197 | + is only called from within visit_Assign and visit_AnnAssign |
| 198 | + after checking for an active class context. |
| 199 | + """ |
| 200 | + # Create a list of attribute names for easy lookup |
| 201 | + attr_names = [ |
| 202 | + attr.get("name") for attr in self._current_class_info["attributes"] |
| 203 | + ] |
| 204 | + if attr_name not in attr_names: |
| 205 | + self._current_class_info["attributes"].append( |
| 206 | + {"name": attr_name, "type": attr_type} |
| 207 | + ) |
204 | 208 |
|
205 | 209 | def visit_Assign(self, node: ast.Assign) -> None: |
206 | 210 | """Handles attribute assignments: `x = ...` and `self.x = ...`.""" |
|
0 commit comments