Skip to content

Commit 7d24f79

Browse files
authored
refactor: Simplifies attrs support
1 parent 04d1a05 commit 7d24f79

File tree

3 files changed

+7
-48
lines changed

3 files changed

+7
-48
lines changed

flake8_type_checking/checker.py

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
ANNOTATION_PROPERTY,
2020
ATTRIBUTE_PROPERTY,
2121
ATTRS_DECORATORS,
22-
ATTRS_IMPORTS,
2322
BINOP_OPERAND_PROPERTY,
2423
MISSING,
2524
TC001,
@@ -53,7 +52,6 @@
5352
HasPosition,
5453
Import,
5554
ImportTypeValue,
56-
Name,
5755
SupportsIsTyping,
5856
)
5957

@@ -130,53 +128,19 @@ class AttrsMixin:
130128
"""
131129

132130
if TYPE_CHECKING:
133-
third_party_imports: dict[str, Import]
134131

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+
...
151134

152135
def is_attrs_class(self, class_node: ast.ClassDef) -> bool:
153136
"""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)
156138

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:
158140
"""Check whether a class decorator is an attrs decorator or not."""
159141
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
180144

181145

182146
class DunderAllMixin:

flake8_type_checking/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
'attr.mutable',
1717
'attr.s',
1818
]
19-
ATTRS_IMPORTS = {'attrs', 'attr'}
2019

2120
flake_version_gt_v4 = tuple(int(i) for i in flake8.__version__.split('.')) >= (4, 0, 0)
2221

flake8_type_checking/types.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
if TYPE_CHECKING:
66
import ast
77
from collections.abc import Generator
8-
from typing import Any, Optional, Protocol, Union
8+
from typing import Any, Protocol, Union
99

1010
Function = Union[ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda]
1111
Comprehension = Union[ast.ListComp, ast.SetComp, ast.DictComp, ast.GeneratorExp]
1212
Import = Union[ast.Import, ast.ImportFrom]
1313
Flake8Generator = Generator[tuple[int, int, str, Any], None, None]
1414

15-
class Name(Protocol):
16-
asname: Optional[str]
17-
name: str
18-
1915
class HasPosition(Protocol):
2016
@property
2117
def lineno(self) -> int:

0 commit comments

Comments
 (0)