Whole file

sbdchd/flake8-pie

The author described this change as fix(rule): false positive for prefer dataclass (#44). It counts as a record because the check below fails on the code as it stood at 349e0a33b and passes on c84f9e6e4, with nothing else changed between the two runs.

Fix saved2021-04-15
Sharing licenceBSD-2-Clause · LICENSE
Change size+15 5

What the code was meant to do, written into the code itself as a save note

fix(rule): false positive for prefer dataclass (#44)

The change

77 from flake8_pie.base import Error
88
99
10-def _is_dataclass_like_stmt(stmt: ast.stmt) -> bool:
11- return isinstance(stmt, ast.AnnAssign) or (
12- isinstance(stmt, ast.FunctionDef) and stmt.name == "__init__"
13- )
10+def _has_dataclass_like_body(body: Sequence[ast.stmt]) -> bool:
11+ """
12+ Has at least one dataclass like assignment stmt and doesn't have any
13+ methods besides __init__.
14+ """
15+ found_assignment_stmt = False
16+ for stmt in body:
17+ if isinstance(stmt, ast.FunctionDef) and stmt.name == "__init__":
18+ continue
19+ elif isinstance(stmt, ast.AnnAssign):
20+ found_assignment_stmt = True
21+ else:
22+ return False
23+ return found_assignment_stmt
1424
1525
1626 def pie793_prefer_dataclass(
2333 not inside_inheriting_cls
2434 and not node.bases
2535 and not node.decorator_list
26- and all(_is_dataclass_like_stmt(stmt) for stmt in node.body)
36+ and _has_dataclass_like_body(node.body)
2737 ):
2838 errors.append(PIE793(lineno=node.lineno, col_offset=node.col_offset))
2939

The check that tells the two apart

failpass·flake8_pie/tests/test_pie793_prefer_dataclass.py::test_prefer_dataclass[\nclass

Check file flake8_pie/tests/test_pie793_prefer_dataclass.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it349e0a33b3b68d4be19eda8159e032b137c31223
Broken version dated2021-04-12
Moduleflake8_pie.pie793_prefer_dataclass
Units changedpie793_prefer_dataclass
Fingerprint6d9e44d5c158ad71
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.