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.
Projectsbdchd/flake8-pie
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
| 7 | 7 | from flake8_pie.base import Error | |
| 8 | 8 | ||
| 9 | 9 | ||
| 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 | |
| 14 | 24 | ||
| 15 | 25 | ||
| 16 | 26 | def pie793_prefer_dataclass( | |
| ⋯ | |||
| 23 | 33 | not inside_inheriting_cls | |
| 24 | 34 | and not node.bases | |
| 25 | 35 | 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) | |
| 27 | 37 | ): | |
| 28 | 38 | errors.append(PIE793(lineno=node.lineno, col_offset=node.col_offset)) | |
| 29 | 39 | ||
The check that tells the two apart
fail→pass·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.