Skip to content

Commit

Permalink
WPS230: Allow any number of attributes in @dataclass, closes #2448
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Dec 19, 2024
1 parent f281a0c commit 3207eb6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Semantic versioning in our case means:
- Allows underscores (`_`) with exactly 3 digits after it in `WPS303`, #3120
- Allows class / instance attribute shadowing
in `@dataclass`es for `WPS601`, #1926
- Allows any number of instance attributes on `@dataclass`es in `WPS230`, #2448
- Adds new rule to forbid `lambda` assigns to special attributes, #1733
- Adds new rule to check problematic function params, #1343
- Adds new rule to detect duplicate conditions in `if`s and `elif`s, #2241
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ def other(self):
{1}
"""

dataclass_template = """
@dataclass
class Test:
def __init__(self):
{0}
def other(self):
{1}
"""

not_a_dataclass_template = """
@not_a_dataclass
class Test:
def __init__(self):
{0}
def other(self):
{1}
"""

module_template = """
{0}
{1}
Expand All @@ -32,6 +52,8 @@ def some():
'code',
[
class_template,
dataclass_template,
not_a_dataclass_template,
module_template,
function_template,
],
Expand Down Expand Up @@ -78,6 +100,7 @@ def test_correct_attributes(
'code',
[
class_template,
not_a_dataclass_template,
],
)
@pytest.mark.parametrize(
Expand Down Expand Up @@ -112,6 +135,7 @@ def test_wrong_attributes_count(
@pytest.mark.parametrize(
'code',
[
dataclass_template,
module_template,
function_template,
],
Expand Down
2 changes: 2 additions & 0 deletions wemake_python_styleguide/violations/complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,8 @@ class TooManyPublicAttributesViolation(ASTViolation):
https://en.wikipedia.org/wiki/Coupling_(computer_programming)
.. versionadded:: 0.12.0
.. versionchanged:: 1.0.0
Any amount of attributes are allowed on ``@dataclasses``.
"""

Expand Down
3 changes: 3 additions & 0 deletions wemake_python_styleguide/visitors/ast/complexity/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def _check_base_classes(self, node: ast.ClassDef) -> None:
)

def _check_public_attributes(self, node: ast.ClassDef) -> None:
if classes.is_dataclass(node):
return # dataclasses can have any amount of attributes

_, instance_attributes = classes.get_attributes(
node,
include_annotated=False,
Expand Down

0 comments on commit 3207eb6

Please sign in to comment.