Skip to content

Commit b8b9abd

Browse files
github-actions[bot]akirchhoff-modular
andauthored
Recognize new-style attrs decorators in too-few-public-methods check (#9346) (#9596)
Beginning with attrs 21.1.0, the recommended way to use attrs is through `import attrs` and using `attrs.define`/`attrs.frozen`, not `import attr` and `attr.s` or `attr.attrs`. Pylint does understand `attr.attrs` (#2988), but new-style uses of attrs are not understood to be data class decorators. Modify `_is_exempt_from_public_methods` to recognize `attrs.define` and `attrs.frozen` in a similar way as is currently done with `dataclasses.dataclass`. Closes #9345. (cherry picked from commit c032181) Co-authored-by: akirchhoff-modular <[email protected]>
1 parent 053c2c3 commit b8b9abd

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Treat `attrs.define` and `attrs.frozen` as dataclass decorators in
2+
`too-few-public-methods` check.
3+
4+
Closes #9345

pylint/checkers/design_analysis.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
SPECIAL_OBJ = re.compile("^_{2}[a-z]+_{2}$")
9393
DATACLASSES_DECORATORS = frozenset({"dataclass", "attrs"})
9494
DATACLASS_IMPORT = "dataclasses"
95+
ATTRS_DECORATORS = frozenset({"define", "frozen"})
96+
ATTRS_IMPORT = "attrs"
9597
TYPING_NAMEDTUPLE = "typing.NamedTuple"
9698
TYPING_TYPEDDICT = "typing.TypedDict"
9799
TYPING_EXTENSIONS_TYPEDDICT = "typing_extensions.TypedDict"
@@ -214,6 +216,10 @@ def _is_exempt_from_public_methods(node: astroid.ClassDef) -> bool:
214216
or DATACLASS_IMPORT in root_locals
215217
):
216218
return True
219+
if name in ATTRS_DECORATORS and (
220+
root_locals.intersection(ATTRS_DECORATORS) or ATTRS_IMPORT in root_locals
221+
):
222+
return True
217223
return False
218224

219225

tests/functional/t/too/too_few_public_methods_37.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import typing
99
from dataclasses import dataclass
1010

11+
import attrs # pylint: disable=import-error
12+
from attrs import define, frozen # pylint: disable=import-error
13+
1114

1215
@dataclasses.dataclass
1316
class ScheduledTxSearchModel:
@@ -40,3 +43,27 @@ class Point:
4043
def to_array(self):
4144
"""Convert to a NumPy array `np.array((x, y, z))`."""
4245
return self.attr1
46+
47+
48+
@define
49+
class AttrsBarePoint:
50+
x: float
51+
y: float
52+
53+
54+
@frozen
55+
class AttrsBareFrozenPoint:
56+
x: float
57+
y: float
58+
59+
60+
@attrs.define
61+
class AttrsQualifiedPoint:
62+
x: float
63+
y: float
64+
65+
66+
@attrs.frozen
67+
class AttrsQualifiedFrozenPoint:
68+
x: float
69+
y: float

0 commit comments

Comments
 (0)