|
19 | 19 | ANNOTATION_PROPERTY, |
20 | 20 | ATTRIBUTE_PROPERTY, |
21 | 21 | ATTRS_DECORATORS, |
22 | | - ATTRS_IMPORTS, |
23 | 22 | BINOP_OPERAND_PROPERTY, |
24 | 23 | MISSING, |
25 | 24 | TC001, |
|
53 | 52 | HasPosition, |
54 | 53 | Import, |
55 | 54 | ImportTypeValue, |
56 | | - Name, |
57 | 55 | SupportsIsTyping, |
58 | 56 | ) |
59 | 57 |
|
@@ -130,53 +128,19 @@ class AttrsMixin: |
130 | 128 | """ |
131 | 129 |
|
132 | 130 | if TYPE_CHECKING: |
133 | | - third_party_imports: dict[str, Import] |
134 | 131 |
|
135 | | - def get_all_attrs_imports(self) -> dict[str | None, str]: |
136 | | - """Return a map of all attrs/attr imports.""" |
137 | | - attrs_imports: dict[str | None, str] = {} # map of alias to full import name |
138 | | - |
139 | | - for node in self.third_party_imports.values(): |
140 | | - module = getattr(node, 'module', '') |
141 | | - names: list[Name] = getattr(node, 'names', []) |
142 | | - |
143 | | - for name in names: |
144 | | - if module in ATTRS_IMPORTS: |
145 | | - alias = name.name if name.asname is None else name.asname |
146 | | - attrs_imports[alias] = f'{module}.{name.name}' |
147 | | - elif name.name.split('.')[0] in ATTRS_IMPORTS: |
148 | | - attrs_imports[name.asname] = name.name |
149 | | - |
150 | | - return attrs_imports |
| 132 | + def lookup_full_name(self, node: ast.AST) -> str | None: # noqa: D102 |
| 133 | + ... |
151 | 134 |
|
152 | 135 | def is_attrs_class(self, class_node: ast.ClassDef) -> bool: |
153 | 136 | """Check whether an ast.ClassDef is an attrs class or not.""" |
154 | | - attrs_imports = self.get_all_attrs_imports() |
155 | | - return any(self.is_attrs_decorator(decorator, attrs_imports) for decorator in class_node.decorator_list) |
| 137 | + return any(self.is_attrs_decorator(decorator) for decorator in class_node.decorator_list) |
156 | 138 |
|
157 | | - def is_attrs_decorator(self, decorator: Any, attrs_imports: dict[str | None, str]) -> bool: |
| 139 | + def is_attrs_decorator(self, decorator: ast.AST) -> bool: |
158 | 140 | """Check whether a class decorator is an attrs decorator or not.""" |
159 | 141 | if isinstance(decorator, ast.Call): |
160 | | - return self.is_attrs_decorator(decorator.func, attrs_imports) |
161 | | - elif isinstance(decorator, ast.Attribute): |
162 | | - return self.is_attrs_attribute(decorator) |
163 | | - elif isinstance(decorator, ast.Name): |
164 | | - return self.is_attrs_str(decorator.id, attrs_imports) |
165 | | - return False |
166 | | - |
167 | | - @staticmethod |
168 | | - def is_attrs_attribute(attribute: ast.Attribute) -> bool: |
169 | | - """Check whether an ast.Attribute is an attrs attribute or not.""" |
170 | | - s1 = f"attr.{getattr(attribute, 'attr', '')}" |
171 | | - s2 = f"attrs.{getattr(attribute, 'attrs', '')}" |
172 | | - actual = [s1, s2] |
173 | | - return any(e for e in actual if e in ATTRS_DECORATORS) |
174 | | - |
175 | | - @staticmethod |
176 | | - def is_attrs_str(attribute: str | ast.expr, attrs_imports: dict[str | None, str]) -> bool: |
177 | | - """Check whether an ast.expr or string is an attrs string or not.""" |
178 | | - actual = attrs_imports.get(str(attribute), '') |
179 | | - return actual in ATTRS_DECORATORS |
| 142 | + decorator = decorator.func |
| 143 | + return self.lookup_full_name(decorator) in ATTRS_DECORATORS |
180 | 144 |
|
181 | 145 |
|
182 | 146 | class DunderAllMixin: |
|
0 commit comments