basstler-rules 4.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- basstler/code_modification/__init__.py +7 -0
- basstler/code_modification/actions.py +171 -0
- basstler/code_modification/exceptions.py +127 -0
- basstler/code_modification/file_actions.py +141 -0
- basstler/code_modification/module_relocation.py +256 -0
- basstler/code_modification/patterns.py +86 -0
- basstler/code_modification/positions.py +39 -0
- basstler/code_modification/references.py +45 -0
- basstler/code_modification/source_file.py +27 -0
- basstler/code_modification/source_tree.py +107 -0
- basstler/code_modification/specification.py +41 -0
- basstler/code_modification/syntax.py +209 -0
- basstler/code_rules/__init__.py +4 -0
- basstler/code_rules/address_building.py +94 -0
- basstler/code_rules/bash_by_name.py +103 -0
- basstler/code_rules/checker.py +163 -0
- basstler/code_rules/command_line.py +165 -0
- basstler/code_rules/commands.py +47 -0
- basstler/code_rules/docstring_presence.py +255 -0
- basstler/code_rules/exceptions.py +34 -0
- basstler/code_rules/fixes.py +307 -0
- basstler/code_rules/instructions.py +80 -0
- basstler/code_rules/locale_encoding.py +315 -0
- basstler/code_rules/package_boundaries.py +344 -0
- basstler/code_rules/prose_length.py +220 -0
- basstler/code_rules/rule.py +158 -0
- basstler/code_rules/rule_module.py +84 -0
- basstler/code_rules/rules/__init__.py +3 -0
- basstler/code_rules/rules/code_quality.py +848 -0
- basstler/code_rules/rules/project.py +171 -0
- basstler/code_rules/subprocess_function.py +26 -0
- basstler/code_rules/syntax_tree_checker.py +38 -0
- basstler/code_rules/text_file_usage.py +146 -0
- basstler/code_rules/tree_removal.py +64 -0
- basstler/templates/agents.md.jinja +16 -0
- basstler_rules-4.2.0.dist-info/METADATA +15 -0
- basstler_rules-4.2.0.dist-info/RECORD +40 -0
- basstler_rules-4.2.0.dist-info/WHEEL +4 -0
- basstler_rules-4.2.0.dist-info/licenses/LICENSE +202 -0
- basstler_rules-4.2.0.dist-info/licenses/NOTICE +13 -0
|
@@ -0,0 +1,848 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The rules on the quality of the code, whatever the project.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import override
|
|
8
|
+
|
|
9
|
+
from basstler.class_property import classproperty
|
|
10
|
+
from basstler.code_rules.address_building import HandBuiltAddressChecker
|
|
11
|
+
from basstler.code_rules.docstring_presence import MissingDocstringChecker
|
|
12
|
+
from basstler.code_rules.prose_length import ProseLengthChecker
|
|
13
|
+
from basstler.code_rules.rule import CodeReference, Section
|
|
14
|
+
from basstler.exceptions import DataclassException
|
|
15
|
+
from basstler.locations import PathEnumeration
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class CodeQualityRules(Section):
|
|
19
|
+
"""
|
|
20
|
+
The rules on the quality of the code, whatever the project.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class RuleApplicationRules(Section):
|
|
25
|
+
"""
|
|
26
|
+
The rules on when and how far the other rules apply.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
@classproperty
|
|
30
|
+
@override
|
|
31
|
+
def parent(cls) -> type[Section]:
|
|
32
|
+
return CodeQualityRules
|
|
33
|
+
|
|
34
|
+
LEAVE_UNTOUCHED_CODE_ALONE = (
|
|
35
|
+
"Existing code that breaks these rules stays as it is unless a change "
|
|
36
|
+
"touches it or the user asks for the cleanup. When you modify a function or"
|
|
37
|
+
" class, bring that function or class in line with these rules as part of "
|
|
38
|
+
"the change, and leave code the change does not otherwise modify alone. If "
|
|
39
|
+
"the cleanup renames a method and so changes the API, ask the user first"
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
ASK_BEFORE_DEPARTING_FROM_A_RULE = (
|
|
43
|
+
"When a rule seems wrong for the case at hand - a failing test that is "
|
|
44
|
+
"itself wrong, an exception that has to be caught, a value that genuinely "
|
|
45
|
+
"has to live at module level - ask the user before departing from it; never"
|
|
46
|
+
" decide the exception on your own"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class RuleModellingRules(Section):
|
|
51
|
+
"""
|
|
52
|
+
The rules on writing the rules themselves.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
@classproperty
|
|
56
|
+
@override
|
|
57
|
+
def parent(cls) -> type[Section]:
|
|
58
|
+
"""
|
|
59
|
+
Nested under :class:`CodeQualityRules`, unlike :meth:`Section.parent`.
|
|
60
|
+
"""
|
|
61
|
+
return CodeQualityRules
|
|
62
|
+
|
|
63
|
+
SPLIT_OFF_THE_CHECKABLE_PART = (
|
|
64
|
+
"When a script can check part of a rule, split the rule: the checkable "
|
|
65
|
+
"part becomes a rule of its own that carries its checker, and the part "
|
|
66
|
+
"only a person can judge stays a rule without one"
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class AskTheDeveloperRules(Section):
|
|
71
|
+
"""
|
|
72
|
+
The rules on what to ask the developer instead of deciding.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
@classproperty
|
|
76
|
+
@override
|
|
77
|
+
def parent(cls) -> type[Section]:
|
|
78
|
+
return CodeQualityRules
|
|
79
|
+
|
|
80
|
+
ASK_INSTEAD_OF_INVENTING_REASONS = (
|
|
81
|
+
"When you are unsure why something was done or why a specific number was "
|
|
82
|
+
"chosen, ask; never invent a reason and write it down as a comment"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
ASK_ABOUT_TEST_ONLY_METHODS = (
|
|
86
|
+
"When a method is never used outside of tests, ask whether it can be removed"
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
ASK_BEFORE_ADDING_DEPENDENCIES = "Before adding a new dependency, ask"
|
|
90
|
+
|
|
91
|
+
SUGGEST_MORE_POWERFUL_PACKAGES = (
|
|
92
|
+
"If a package could be replaced by a more powerful one, tell us"
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
ASK_BEFORE_WORKING_AROUND_A_DENIAL = (
|
|
96
|
+
"When the permission system or its classifier denies a tool call, never "
|
|
97
|
+
"work around the denial and never drop the step silently: ask the user to "
|
|
98
|
+
"accept it, naming the exact action and why it is needed, and run it only "
|
|
99
|
+
"once they agree"
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class TestingRules(Section):
|
|
104
|
+
"""
|
|
105
|
+
The rules on tests.
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
@classproperty
|
|
109
|
+
@override
|
|
110
|
+
def parent(cls) -> type[Section]:
|
|
111
|
+
return CodeQualityRules
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class WritingTestsRules(Section):
|
|
115
|
+
"""
|
|
116
|
+
The rules on writing tests.
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
@classproperty
|
|
120
|
+
@override
|
|
121
|
+
def parent(cls) -> type[Section]:
|
|
122
|
+
return TestingRules
|
|
123
|
+
|
|
124
|
+
WRITE_TESTS_WITH_PYTEST = (
|
|
125
|
+
"Write tests with pytest: plain test functions or classes, pytest fixtures,"
|
|
126
|
+
" `pytest.raises` and plain `assert`"
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
REUSE_FIXTURES = "Reuse the existing fixtures in `conftest.py`"
|
|
130
|
+
|
|
131
|
+
WORK_TEST_DRIVEN = (
|
|
132
|
+
"Work test-driven: prove a bug with a meaningful, failing test before "
|
|
133
|
+
"fixing it. Every new feature and fix is covered by tests"
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
NEVER_MODIFY_A_FAILING_TEST = (
|
|
137
|
+
"When fixing a failing test, never modify the test itself"
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
NAME_TEST_CLASSES_AFTER_BEHAVIOUR = (
|
|
141
|
+
"Name test classes, and the mimic classes tests use, after the pattern or "
|
|
142
|
+
"behaviour they exercise, not after the external class they stand in for"
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
KEEP_SNIPPETS_IN_FILES = (
|
|
146
|
+
"Keep code snippets in separate files of the correct type and import or "
|
|
147
|
+
"read them into the test; never embed them as strings"
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class AssertionsRules(Section):
|
|
152
|
+
"""
|
|
153
|
+
The rules on what a test asserts.
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
@classproperty
|
|
157
|
+
@override
|
|
158
|
+
def parent(cls) -> type[Section]:
|
|
159
|
+
return TestingRules
|
|
160
|
+
|
|
161
|
+
ASSERT_EXACT_EXPECTED_VALUES = (
|
|
162
|
+
"Assert equality to the expected value whenever it can be determined, never"
|
|
163
|
+
" only a weaker check such as not-None or not-empty"
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
ASSERT_AGAINST_THE_DEFINITION = (
|
|
167
|
+
"Assert against the definition, not a retyped copy of it: the enum member, "
|
|
168
|
+
"the `classproperty`, or the value from the fixture the code under test "
|
|
169
|
+
"consumed. Where a type distinguishes the case - a distinct exception "
|
|
170
|
+
"class, an enum member - assert the type instead of matching message text. "
|
|
171
|
+
"A retyped literal keeps passing when the original changes"
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
TEST_ONE_BEHAVIOUR = (
|
|
175
|
+
"Each test checks the one behaviour it names and fails only for its own "
|
|
176
|
+
"reason: assert exactly the values that behaviour determines, not "
|
|
177
|
+
"incidental output such as the wording of an error message the test is not "
|
|
178
|
+
"about. Where another test already asserts a value exactly, derive the "
|
|
179
|
+
"expected value from the production code that computes it instead of "
|
|
180
|
+
"hardcoding a second copy. Tests are independent of each other"
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
SHOW_WHAT_IS_MATCHED = (
|
|
184
|
+
"A test that asserts where something was found, or which inputs match, "
|
|
185
|
+
"shows the code itself next to the expectation: assert the source text "
|
|
186
|
+
"found, never bare line numbers or positions in a file the reader has to "
|
|
187
|
+
"open, and list each input beside whether it should match"
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class ExternalServicesAndCIRules(Section):
|
|
192
|
+
"""
|
|
193
|
+
The rules on tests that reach external services, and on CI.
|
|
194
|
+
"""
|
|
195
|
+
|
|
196
|
+
@classproperty
|
|
197
|
+
@override
|
|
198
|
+
def parent(cls) -> type[Section]:
|
|
199
|
+
return TestingRules
|
|
200
|
+
|
|
201
|
+
EVERY_TEST_RUNS_IN_CI = (
|
|
202
|
+
"Every added test is part of the CI suite. A test that needs live external "
|
|
203
|
+
"calls or credentials CI does not have is skipped there (or removed, if "
|
|
204
|
+
"new) so it cannot break the pipeline"
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
APPROVE_CI_CREDENTIALS = (
|
|
208
|
+
"A test that needs credentials in CI must be approved by the user and have "
|
|
209
|
+
"those credentials available in CI; otherwise it is skipped there"
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
MOCK_EXTERNAL_APIS = (
|
|
213
|
+
"Mock external APIs; call one live only to download a dataset other tests need"
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
PAIR_LIVE_TESTS_WITH_MOCKED_ONES = (
|
|
217
|
+
"A test that calls an external API live must have a skip condition so it "
|
|
218
|
+
"does not run in CI, and must be paired with an equivalent mocked test that"
|
|
219
|
+
" does"
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
class RunningTestsRules(Section):
|
|
224
|
+
"""
|
|
225
|
+
The rules on running tests.
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
@classproperty
|
|
229
|
+
@override
|
|
230
|
+
def parent(cls) -> type[Section]:
|
|
231
|
+
return TestingRules
|
|
232
|
+
|
|
233
|
+
RUN_TESTS_WITH_PYTEST = "Run tests with pytest"
|
|
234
|
+
|
|
235
|
+
USE_THE_PROJECT_INTERPRETER = (
|
|
236
|
+
"Always use the project's configured Python interpreter for tests and commands"
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
class CodeStyleRules(Section):
|
|
241
|
+
"""
|
|
242
|
+
The rules on code style.
|
|
243
|
+
"""
|
|
244
|
+
|
|
245
|
+
@classproperty
|
|
246
|
+
@override
|
|
247
|
+
def parent(cls) -> type[Section]:
|
|
248
|
+
return CodeQualityRules
|
|
249
|
+
|
|
250
|
+
RUFF_CHECK_PASSES = (
|
|
251
|
+
"`ruff check .` passes on every change. The rules it enforces are "
|
|
252
|
+
"configured in `pyproject.toml` and not repeated here; never silence a "
|
|
253
|
+
"finding with `noqa` or a new ignore without asking"
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
USE_DATACLASSES = (
|
|
257
|
+
"Use dataclasses: a `ValidatedJSONModel` for data parsed from outside the "
|
|
258
|
+
"program (an API response, a configuration file, a command's input), plain "
|
|
259
|
+
"dataclasses everywhere else"
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
DIVIDE_FILES_INTO_SECTIONS = (
|
|
263
|
+
"Divide every file, source and test, into sections with `# %% <short "
|
|
264
|
+
"description>` headers (e.g. `# %% same-noun disambiguation`), never "
|
|
265
|
+
"decorative box-drawing dividers"
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
GROUP_PRIMITIVES_INTO_DATACLASSES = (
|
|
269
|
+
"A group of primitives that travels together, or a return type that keeps "
|
|
270
|
+
"being repeated, becomes a dataclass"
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
NEVER_DUPLICATE_CODE = (
|
|
274
|
+
"Never duplicate code. Never put methods in a catch-all module such as "
|
|
275
|
+
"`utils.py`; move them onto the class that owns the behaviour"
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
ACCESS_ATTRIBUTES_DIRECTLY = (
|
|
279
|
+
"Access attributes with `.`, never with `getattr`, and never wrap attribute"
|
|
280
|
+
" access in try-except"
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
NEVER_REIMPLEMENT = (
|
|
284
|
+
"Never reimplement what the codebase or an existing dependency already "
|
|
285
|
+
"provides. When searching for it, never cut off the output of grep or other"
|
|
286
|
+
" searches; narrow the search instead"
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
class NamingRules(Section):
|
|
291
|
+
"""
|
|
292
|
+
The rules on naming.
|
|
293
|
+
"""
|
|
294
|
+
|
|
295
|
+
@classproperty
|
|
296
|
+
@override
|
|
297
|
+
def parent(cls) -> type[Section]:
|
|
298
|
+
return CodeStyleRules
|
|
299
|
+
|
|
300
|
+
CORRECT_SIMPLE_DESCRIPTIVE_NAMES = (
|
|
301
|
+
"Names are technically correct, simple and descriptive, in that order. An "
|
|
302
|
+
"inaccurate name is worse than a vague one, because a reader who trusts it "
|
|
303
|
+
"stops reading"
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
NAMES_READ_ON_THEIR_OWN = (
|
|
307
|
+
"A name stays correct and understandable when read on its own, without its "
|
|
308
|
+
"class or a keyword argument next to it - after `value = "
|
|
309
|
+
"instance.attribute`, in a log line, in a traceback, or when passed on "
|
|
310
|
+
"positionally. A `Pipe` field `size` says nothing once it leaves the class;"
|
|
311
|
+
" `inner_diameter` does. A generic word that would fit anything never "
|
|
312
|
+
"passes this test"
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
NO_AMBIGUOUS_NAMES = (
|
|
316
|
+
"A name is unambiguous: a word that also names something else a reader may "
|
|
317
|
+
"know - a wheel, a release, a package - carries the word that says which, as "
|
|
318
|
+
"`PythonWheel` does, so the name says what the class represents without its "
|
|
319
|
+
"module beside it"
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
NO_BARE_PREPOSITION_NAMES = (
|
|
323
|
+
"A method name is never a bare preposition such as `of`, `for` or `with`: it "
|
|
324
|
+
"says nothing without its class and its arguments beside it. Name what it "
|
|
325
|
+
"builds from or does, as in `RenameMap.from_decisions`"
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
USE_PLAIN_WORDS = (
|
|
329
|
+
"Use the plain word every reader already knows. Use a specialist, "
|
|
330
|
+
"metaphorical or in-house term only where it is genuinely the precise word,"
|
|
331
|
+
" never as shorthand between the people who were in the discussion"
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
USE_THE_DOMAIN_WORD = (
|
|
335
|
+
"Where the domain or file format already has a word for something, use that"
|
|
336
|
+
" word"
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
NEVER_ABBREVIATE = "Never abbreviate an identifier"
|
|
340
|
+
|
|
341
|
+
NAME_WHAT_NOT_HOW = (
|
|
342
|
+
"A name says *what* a thing is or does - never *how* it does it, *when* it "
|
|
343
|
+
"runs, or the layer or mechanism it is built on. Keep it short"
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
NO_FOREIGN_VOCABULARY = (
|
|
347
|
+
"A name whose meaning has to be looked up elsewhere is wrong. Never adopt "
|
|
348
|
+
"another system's vocabulary as an identifier of ours: name the thing for "
|
|
349
|
+
"what it is here, and explain a foreign shape in the docstring"
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
VERB_AND_NOUN_PHRASES = (
|
|
353
|
+
"Methods are verb phrases for what they do; classes and attributes are noun"
|
|
354
|
+
" phrases for what they are, except a mixin that only adds one field, which "
|
|
355
|
+
"is named `Has<Field>`, as in `HasRepository`. Name a field for its subject, "
|
|
356
|
+
"not for the shape of its value"
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
ONE_NAME_PER_OPERATION = (
|
|
360
|
+
"One operation has one name throughout a module. Where callers depend on "
|
|
361
|
+
"that name, declare it in a base class instead of leaving it a convention "
|
|
362
|
+
"each class is trusted to follow"
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
NAME_ENUM_MEMBERS_FOR_THEIR_SITUATION = (
|
|
366
|
+
"Name an enum member for the situation it means, not for the function it "
|
|
367
|
+
"dispatches to or the text it renders"
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
NO_REPEATED_WORDS = (
|
|
371
|
+
"Never repeat the enclosing type's name in its members, or the same word "
|
|
372
|
+
"twice within one name"
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
NEVER_SHADOW_BOUND_NAMES = (
|
|
376
|
+
"Never take an identifier the language or something in scope already binds:"
|
|
377
|
+
" `Enum` reserves `name`, a parameter `field` shadows `dataclasses.field`, "
|
|
378
|
+
"a field shadows a method of the same name. These fail at runtime or "
|
|
379
|
+
"silently, not at import"
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
FINISH_RENAMES = (
|
|
383
|
+
"A rename is finished only when every reader of the old name, docstrings "
|
|
384
|
+
"and comments included, uses the new one and the tests pass. A mechanical "
|
|
385
|
+
"rename across a file is exactly where a method and a field converge on one"
|
|
386
|
+
" name"
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
SUSPECT_VAGUE_CODE = (
|
|
390
|
+
"When no honest specific name exists, suspect the code, not your "
|
|
391
|
+
"vocabulary: a thing that can only be described vaguely usually has no "
|
|
392
|
+
"single subject (a container for whatever its caller passed, a function "
|
|
393
|
+
"doing two jobs). Remove it instead of hunting for a better word"
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
class DesignPrinciplesRules(Section):
|
|
398
|
+
"""
|
|
399
|
+
The rules on design.
|
|
400
|
+
"""
|
|
401
|
+
|
|
402
|
+
@classproperty
|
|
403
|
+
@override
|
|
404
|
+
def parent(cls) -> type[Section]:
|
|
405
|
+
return CodeQualityRules
|
|
406
|
+
|
|
407
|
+
OBJECT_ORIENTED_AND_SOLID = (
|
|
408
|
+
("Use strictly object-oriented design and always apply the SOLID principles:"),
|
|
409
|
+
(
|
|
410
|
+
(
|
|
411
|
+
"Single Responsibility: each class and method does one thing. If a "
|
|
412
|
+
"method has cyclomatic complexity in the hundreds, refactor"
|
|
413
|
+
),
|
|
414
|
+
"Open/Closed: open for extension, closed for modification",
|
|
415
|
+
(
|
|
416
|
+
"Liskov Substitution: subtypes are substitutable for their base "
|
|
417
|
+
"types without breaking behaviour"
|
|
418
|
+
),
|
|
419
|
+
(
|
|
420
|
+
"Interface Segregation: many small interfaces over one large one. "
|
|
421
|
+
"An interface is an explicit superclass or mixin, never a "
|
|
422
|
+
"`Protocol`"
|
|
423
|
+
),
|
|
424
|
+
(
|
|
425
|
+
"Dependency Inversion: depend on abstractions, not concrete "
|
|
426
|
+
"implementations"
|
|
427
|
+
),
|
|
428
|
+
),
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
MODULAR_AND_DECOUPLED = "Keep code modular and decoupled"
|
|
432
|
+
|
|
433
|
+
NO_YAGNI = "Eliminate YAGNI smells"
|
|
434
|
+
|
|
435
|
+
HARD_TO_MISUSE_INTERFACES = "Make interfaces hard to misuse"
|
|
436
|
+
|
|
437
|
+
MODEL_RELATIONSHIPS_IN_THE_SOURCE = (
|
|
438
|
+
"Model the relationship between related concepts in the source, never only "
|
|
439
|
+
"in a test that checks two separate definitions agree: derive one from the "
|
|
440
|
+
"other, or unify them into one concept when that is the cleaner design by "
|
|
441
|
+
"SOLID and the domain's semantics"
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
OWNER_AS_A_REQUIRED_FIELD = (
|
|
445
|
+
"A thing that always belongs to another - a branch to its repository, a pull "
|
|
446
|
+
"request to the repository it is opened on - holds its owner as a required "
|
|
447
|
+
"field of its own class: never an optional field, and never a subclass that "
|
|
448
|
+
"adds the owner to an ownerless original"
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
ASSOCIATE_OR_SUBCLASS_FIRST = (
|
|
452
|
+
"Before writing a new class, look for an existing class it can hold as a "
|
|
453
|
+
"field or subclass, and use it; when related classes share fields or "
|
|
454
|
+
"behaviour, give them a common base"
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
SHARED_FIELDS_IN_ONE_MIXIN = (
|
|
458
|
+
"A field that means the same thing in several classes is declared once, in a "
|
|
459
|
+
"mixin or base those classes inherit, never redeclared in each; a set of "
|
|
460
|
+
"fields that travels together, such as a pull request's base and head, is "
|
|
461
|
+
"one such mixin. Two classes that mean the same thing under different names "
|
|
462
|
+
"are one class"
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
MODELS_BOUND_TO_THEIR_CONCEPT = (
|
|
466
|
+
"A model of an external resource or payload is bound to the concept it "
|
|
467
|
+
"describes, so every representation of one concept can be found from it: a "
|
|
468
|
+
"repository setting's model, the read that parses it and the write that "
|
|
469
|
+
"changes it are generic over that setting or hold it as a field, and the "
|
|
470
|
+
"step a user takes when the write is refused belongs to the write. Never a "
|
|
471
|
+
"free-standing copy of a concept another class already models"
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
ADDRESSES_ON_THEIR_SUBJECT = (
|
|
475
|
+
"Every address of an external service - a REST path, a web page - is built "
|
|
476
|
+
"by a method of the class of what it addresses (`Repository` for one within "
|
|
477
|
+
"a repository), never appended to that class's address by hand where it is "
|
|
478
|
+
"used",
|
|
479
|
+
(),
|
|
480
|
+
HandBuiltAddressChecker(),
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
class MethodsRules(Section):
|
|
485
|
+
"""
|
|
486
|
+
The rules on methods.
|
|
487
|
+
"""
|
|
488
|
+
|
|
489
|
+
@classproperty
|
|
490
|
+
@override
|
|
491
|
+
def parent(cls) -> type[Section]:
|
|
492
|
+
return DesignPrinciplesRules
|
|
493
|
+
|
|
494
|
+
EXTRACT_METHODS_THAT_EARN_THEIR_ABSTRACTION = (
|
|
495
|
+
"Every method hides what actually runs from the reader, so extract one only"
|
|
496
|
+
" when its abstraction is worth more than reading the executed code in "
|
|
497
|
+
"place"
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
ONE_LEVEL_OF_ABSTRACTION = (
|
|
501
|
+
"Keep every statement in a method on the same level of abstraction: a "
|
|
502
|
+
"method either strings together named steps or carries out one step's "
|
|
503
|
+
"detail, never both"
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
NO_FORWARDING_METHODS = (
|
|
507
|
+
"Never write a method that only forwards to or renames another call. A "
|
|
508
|
+
"one-line method is allowed only when it implements an abstract method or a"
|
|
509
|
+
" `classproperty`, caches its result, or removes duplication - and in the "
|
|
510
|
+
"last case, first try to abstract the duplicated code itself"
|
|
511
|
+
)
|
|
512
|
+
|
|
513
|
+
INLINE_SINGLE_CALLER_METHODS = (
|
|
514
|
+
"A method with a single caller must earn its place under these rules; "
|
|
515
|
+
"otherwise inline it"
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
GUARD_CLAUSES = (
|
|
519
|
+
"Reduce nesting and complexity with guard clauses: handle alternative "
|
|
520
|
+
"outputs first by inverting conditions and returning early, so the main "
|
|
521
|
+
"branch holds the main output and the biggest compute. A single level of "
|
|
522
|
+
"abstraction never justifies deep nesting; split the method instead"
|
|
523
|
+
)
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
class ErrorsRules(Section):
|
|
527
|
+
"""
|
|
528
|
+
The rules on raising and modelling errors.
|
|
529
|
+
"""
|
|
530
|
+
|
|
531
|
+
@classproperty
|
|
532
|
+
@override
|
|
533
|
+
def parent(cls) -> type[Section]:
|
|
534
|
+
return DesignPrinciplesRules
|
|
535
|
+
|
|
536
|
+
NEVER_TRY_EXCEPT = (
|
|
537
|
+
"Never use try-except; a program in an illegal state raises an appropriate "
|
|
538
|
+
"exception"
|
|
539
|
+
)
|
|
540
|
+
|
|
541
|
+
ONE_EXCEPTION_TYPE_PER_FAILURE = (
|
|
542
|
+
"Define a few exception types, one per kind of failure (a git command that "
|
|
543
|
+
"fails, a GitHub request that is refused, a configuration that does not "
|
|
544
|
+
"parse), each a dataclass carrying what the reader needs to fix it. Never a"
|
|
545
|
+
" new class for every place that raises"
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
PLACE_IN_THE_CLOSEST_MODULE = (
|
|
549
|
+
(
|
|
550
|
+
"Put each exception in the `exceptions.py` closest to the code that "
|
|
551
|
+
"raises it:"
|
|
552
|
+
),
|
|
553
|
+
(
|
|
554
|
+
(
|
|
555
|
+
"An exception raised only within one subpackage goes in that "
|
|
556
|
+
"subpackage's `exceptions.py`; create the file if the subpackage "
|
|
557
|
+
"has none"
|
|
558
|
+
),
|
|
559
|
+
(
|
|
560
|
+
"An exception raised across several subpackages goes in the "
|
|
561
|
+
"`exceptions.py` of their nearest common package"
|
|
562
|
+
),
|
|
563
|
+
),
|
|
564
|
+
)
|
|
565
|
+
|
|
566
|
+
SUBCLASS_THE_BASE_EXCEPTION = (
|
|
567
|
+
f"Custom exceptions subclass {CodeReference(DataclassException).path}, "
|
|
568
|
+
"implementing its `error_message` and `suggest_correction`"
|
|
569
|
+
)
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
class ConstantsAndClassLevelValuesRules(Section):
|
|
573
|
+
"""
|
|
574
|
+
The rules on constants and values declared on a class.
|
|
575
|
+
"""
|
|
576
|
+
|
|
577
|
+
@classproperty
|
|
578
|
+
@override
|
|
579
|
+
def parent(cls) -> type[Section]:
|
|
580
|
+
return DesignPrinciplesRules
|
|
581
|
+
|
|
582
|
+
TRUE_CONSTANTS_AT_MODULE_LEVEL = (
|
|
583
|
+
"A true constant - a value no caller would change, such as a limit an "
|
|
584
|
+
"external API sets - is a plain module-level `UPPER_CASE` constant"
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
FIXED_SETS_AS_ENUMS = "A fixed set of related values is a module-level enum"
|
|
588
|
+
|
|
589
|
+
CHANGEABLE_DEFAULTS_AS_PARAMETERS_OR_FIELDS = (
|
|
590
|
+
"A default a caller may want to change is a parameter of the method that "
|
|
591
|
+
"uses it if only that method does, otherwise a dataclass field with that "
|
|
592
|
+
"default. For example, a `Heater` takes `target_temperature: float = 20.0` "
|
|
593
|
+
"as a field instead of declaring `DEFAULT_TARGET_TEMPERATURE = 20.0`"
|
|
594
|
+
)
|
|
595
|
+
|
|
596
|
+
PER_SUBCLASS_VALUES_AS_CLASSPROPERTIES = (
|
|
597
|
+
"A value each subclass declares for itself is a "
|
|
598
|
+
f"{CodeReference(classproperty).path}"
|
|
599
|
+
)
|
|
600
|
+
|
|
601
|
+
NO_MUTABLE_MODULE_STATE = (
|
|
602
|
+
"Never keep mutable state at module level. `ClassVar` is allowed only for "
|
|
603
|
+
"state that explicitly has to be shared and mutable across every instance "
|
|
604
|
+
"of a class. This almost never applies"
|
|
605
|
+
)
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
class StructuredDataRules(Section):
|
|
609
|
+
"""
|
|
610
|
+
The rules on structured data over literals.
|
|
611
|
+
"""
|
|
612
|
+
|
|
613
|
+
@classproperty
|
|
614
|
+
@override
|
|
615
|
+
def parent(cls) -> type[Section]:
|
|
616
|
+
return DesignPrinciplesRules
|
|
617
|
+
|
|
618
|
+
PREFER_STRUCTURED_DATA = (
|
|
619
|
+
"Structured data is the default over bare strings, hardcoded values and "
|
|
620
|
+
"meaningless numbers: reach for the structured form first and justify the "
|
|
621
|
+
"literal, never the other way round"
|
|
622
|
+
)
|
|
623
|
+
|
|
624
|
+
FIXED_NAMES_AS_STRING_ENUMS = (
|
|
625
|
+
"A string that names a fixed thing - a state, a label, a filename, an "
|
|
626
|
+
"environment variable, a command flag, a status - is a `StrEnum` member. A "
|
|
627
|
+
"value spelled in two places has no single source to rename, and nothing "
|
|
628
|
+
"fails when the two drift apart"
|
|
629
|
+
)
|
|
630
|
+
|
|
631
|
+
PAYLOAD_KEYS_ONLY_IN_THE_MODEL = (
|
|
632
|
+
"A payload is a dataclass, and its keys are spelled only inside that "
|
|
633
|
+
"dataclass: as its field names where they match the payload, otherwise "
|
|
634
|
+
"as a field's alias or inside the method that parses or writes it. Never "
|
|
635
|
+
"declare an enum of payload keys, or of the values a payload tags its "
|
|
636
|
+
"shapes with, that nothing beyond the parser reads, and never declare one "
|
|
637
|
+
"key in two places"
|
|
638
|
+
)
|
|
639
|
+
|
|
640
|
+
ONE_MODEL_PER_PAYLOAD_SHAPE = (
|
|
641
|
+
"When the same thing arrives in several shapes - a check run and a commit "
|
|
642
|
+
"status, a GraphQL node and its REST resource - each shape is a model of "
|
|
643
|
+
"its own subclassing a common base that declares the shared fields, and "
|
|
644
|
+
'the payload\'s tag (`kind: Annotated[Literal["CheckRun"], '
|
|
645
|
+
'Field(alias="__typename")]`) or the endpoint picks the model. Never one '
|
|
646
|
+
"model listing every shape's keys as alias choices"
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
TYPED_ENUM_VALUES = (
|
|
650
|
+
"When the values are more than text - paths, numbers - give the enum values"
|
|
651
|
+
" of that type, mixing the type in (`IntEnum`, `StrEnum`)"
|
|
652
|
+
)
|
|
653
|
+
|
|
654
|
+
NO_MAGIC_NUMBERS = (
|
|
655
|
+
"A magic number becomes an enum member, a field default or a named "
|
|
656
|
+
"constant; a bare literal that carries meaning is unreadable where it is "
|
|
657
|
+
"used and unsearchable everywhere else"
|
|
658
|
+
)
|
|
659
|
+
|
|
660
|
+
PARSE_EXTERNAL_DATA_AT_THE_BOUNDARY = (
|
|
661
|
+
"Data whose shape someone else controls - an API response, a configuration "
|
|
662
|
+
"file - is parsed at the boundary into subclasses of "
|
|
663
|
+
"`basstler.json_model.ValidatedJSONModel`, so it is "
|
|
664
|
+
"validated once and the field names and the access path into the payload "
|
|
665
|
+
"are written once. A subclass is a frozen pydantic dataclass already: give "
|
|
666
|
+
"it no dataclass decorator, and pass the pydantic settings it needs as the "
|
|
667
|
+
"class keyword `config`"
|
|
668
|
+
)
|
|
669
|
+
|
|
670
|
+
NO_POSITIONAL_TUPLES = (
|
|
671
|
+
"A tuple whose positions carry meaning becomes a dataclass, or an enum when"
|
|
672
|
+
" the positions are a fixed set of alternatives rather than fields"
|
|
673
|
+
)
|
|
674
|
+
|
|
675
|
+
LONG_LITERALS_IN_FILES = (
|
|
676
|
+
"A long literal document - a query, a template, a schema - lives in a file "
|
|
677
|
+
"of its own type and is read in, never embedded as a string"
|
|
678
|
+
)
|
|
679
|
+
|
|
680
|
+
PATH_ENUMS_SUBCLASS_PATH_ENUMERATION = (
|
|
681
|
+
f"A path enum subclasses {CodeReference(PathEnumeration).path}, so every "
|
|
682
|
+
"path derived from a member is a plain `Path`; its members' `name` is the "
|
|
683
|
+
"path's last component, so read the member name as `_name_`"
|
|
684
|
+
)
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
class TypeHintsRules(Section):
|
|
688
|
+
"""
|
|
689
|
+
The rules on type hints.
|
|
690
|
+
"""
|
|
691
|
+
|
|
692
|
+
@classproperty
|
|
693
|
+
@override
|
|
694
|
+
def parent(cls) -> type[Section]:
|
|
695
|
+
return CodeQualityRules
|
|
696
|
+
|
|
697
|
+
ACCURATE_TYPE_HINTS = (
|
|
698
|
+
"Type hints are accurate: `Any` where the value genuinely is anything, "
|
|
699
|
+
"never as a shortcut"
|
|
700
|
+
)
|
|
701
|
+
|
|
702
|
+
MOST_SPECIFIC_TYPE = (
|
|
703
|
+
"A field, parameter or return value takes the most specific type that is "
|
|
704
|
+
"correct for its meaning and every one of its uses - a `Branch`, not the "
|
|
705
|
+
"`str` of its name; a `Path`, not a `str` - and a payload converts it to and "
|
|
706
|
+
"from its wire form at the boundary"
|
|
707
|
+
)
|
|
708
|
+
|
|
709
|
+
BOUND_GENERIC_PARAMETERS = (
|
|
710
|
+
"When each class in a family declares the type it handles, carry that type "
|
|
711
|
+
"as a bound generic parameter, not a `ClassVar`, so it is part of the "
|
|
712
|
+
"signature and cannot disagree with a separate attribute: bind it in each "
|
|
713
|
+
"member (`class MemberOfFamily(Family[ConcreteType])`)"
|
|
714
|
+
)
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
class DocumentationRules(Section):
|
|
718
|
+
"""
|
|
719
|
+
The rules on docstrings and comments.
|
|
720
|
+
"""
|
|
721
|
+
|
|
722
|
+
@classproperty
|
|
723
|
+
@override
|
|
724
|
+
def parent(cls) -> type[Section]:
|
|
725
|
+
return CodeQualityRules
|
|
726
|
+
|
|
727
|
+
DOCUMENT_EVERY_CONSTRUCT = (
|
|
728
|
+
"Every module, class, function and field has a docstring. A field's "
|
|
729
|
+
"docstring goes directly below the field, not in the class docstring. A "
|
|
730
|
+
"method implementing an abstract method is exempt: the abstract method's "
|
|
731
|
+
"docstring is enough",
|
|
732
|
+
(),
|
|
733
|
+
MissingDocstringChecker(),
|
|
734
|
+
)
|
|
735
|
+
|
|
736
|
+
MEANINGFUL_DOCUMENTATION = (
|
|
737
|
+
"Documentation is written in reStructuredText and says something the name "
|
|
738
|
+
"and the type hints do not: a module's or class's responsibility, a "
|
|
739
|
+
"function's contract, a field's unit or meaning"
|
|
740
|
+
)
|
|
741
|
+
|
|
742
|
+
OVERRIDES_INHERIT_DOCSTRINGS = (
|
|
743
|
+
"An implementation of an abstract method has no docstring and inherits the "
|
|
744
|
+
"base one. An override of an implemented method documents only what differs "
|
|
745
|
+
"from the base and references the overridden method with `:meth:`"
|
|
746
|
+
)
|
|
747
|
+
|
|
748
|
+
DOCSTRINGS_STATE_THE_CONTRACT = (
|
|
749
|
+
(
|
|
750
|
+
"A docstring states what the code does and its contract, short and to "
|
|
751
|
+
"the point. It never contains:"
|
|
752
|
+
),
|
|
753
|
+
(
|
|
754
|
+
"how the code does it",
|
|
755
|
+
(
|
|
756
|
+
"the ways a caller can supply a value (for example, that it may "
|
|
757
|
+
"also be given as a query instead of a concrete object)"
|
|
758
|
+
),
|
|
759
|
+
(
|
|
760
|
+
'the current callers or consumers ("used by X and Y") - that goes '
|
|
761
|
+
"stale and reads as exhaustive"
|
|
762
|
+
),
|
|
763
|
+
(
|
|
764
|
+
"justification of design decisions, comparison with rejected or "
|
|
765
|
+
"hypothetical designs, or review and implementation history"
|
|
766
|
+
),
|
|
767
|
+
"type information - the type hints carry it",
|
|
768
|
+
),
|
|
769
|
+
)
|
|
770
|
+
|
|
771
|
+
SHORT_COMMENTS = (
|
|
772
|
+
"Keep docstrings and comments short and never write walls of text. Comments"
|
|
773
|
+
" are meaningful and DRY; remove comments that restate the code"
|
|
774
|
+
)
|
|
775
|
+
|
|
776
|
+
SHORTEST_DOCSTRINGS = (
|
|
777
|
+
"Make every docstring as short as it can be: one line where one line says "
|
|
778
|
+
"it. A module's or class's docstring states its responsibility in a sentence "
|
|
779
|
+
"or two; it never lists every step it takes or every case it handles - those "
|
|
780
|
+
"belong to the methods that take them"
|
|
781
|
+
)
|
|
782
|
+
|
|
783
|
+
SPHINX_ROLES = (
|
|
784
|
+
"Reference code with Sphinx roles (`:func:`, `:class:`, `:attr:`, "
|
|
785
|
+
"`:meth:`), never with plain backticks, and use directives such as `.. "
|
|
786
|
+
"note::` and `.. warning::` for notes and warnings"
|
|
787
|
+
)
|
|
788
|
+
|
|
789
|
+
NO_CAPITALS_FOR_EMPHASIS = (
|
|
790
|
+
"Never use all-caps words for emphasis in docstrings or comments; use RST "
|
|
791
|
+
"emphasis (`*word*`). Genuine identifiers, acronyms and enum/constant names"
|
|
792
|
+
" (`UUID`, `WHERE`, `Definiteness.DEFINITE`) are exempt"
|
|
793
|
+
)
|
|
794
|
+
|
|
795
|
+
FORMAT_WITH_THE_HOOK = (
|
|
796
|
+
"Always format modified Python files with `ruff format` followed by "
|
|
797
|
+
"docformatter, through the repository's pre-commit hook "
|
|
798
|
+
"(`scripts/format_docstrings.py`)"
|
|
799
|
+
)
|
|
800
|
+
|
|
801
|
+
SHORT_DOCSTRINGS = (
|
|
802
|
+
f"A docstring holds at most {ProseLengthChecker.docstring_lines} lines of "
|
|
803
|
+
"prose, field lists and literal blocks aside, and a comment block at most "
|
|
804
|
+
f"{ProseLengthChecker.comment_lines} lines",
|
|
805
|
+
(),
|
|
806
|
+
ProseLengthChecker(),
|
|
807
|
+
)
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
class VersionControlRules(Section):
|
|
811
|
+
"""
|
|
812
|
+
The rules on commits, pushes and pull requests.
|
|
813
|
+
"""
|
|
814
|
+
|
|
815
|
+
@classproperty
|
|
816
|
+
@override
|
|
817
|
+
def parent(cls) -> type[Section]:
|
|
818
|
+
return CodeQualityRules
|
|
819
|
+
|
|
820
|
+
AUTHOR_AS_THE_HUMAN = (
|
|
821
|
+
"Author commits as the human user running the tool, with their own "
|
|
822
|
+
"configured git `user.name` and `user.email`. Never author or amend a "
|
|
823
|
+
"commit as an assistant, and never credit one as author or co-author: no "
|
|
824
|
+
"`Co-Authored-By:` trailer for an assistant, no `noreply@anthropic.com` or "
|
|
825
|
+
"similar as author or committer"
|
|
826
|
+
)
|
|
827
|
+
|
|
828
|
+
ACKNOWLEDGE_ASSISTANCE_PLAINLY = (
|
|
829
|
+
"A short plain line in the commit message body acknowledging assistant "
|
|
830
|
+
"help, such as `Made with AI assistance`, is encouraged. Never name the "
|
|
831
|
+
"model or the AI service"
|
|
832
|
+
)
|
|
833
|
+
|
|
834
|
+
RULES_APPLY_TO_EVERYONE = "These rules apply to every contributor and every tool"
|
|
835
|
+
|
|
836
|
+
NEVER_PUSH_UPSTREAM = (
|
|
837
|
+
"When you work in a fork, never push to the upstream repository - no "
|
|
838
|
+
"branch, no tag, whatever the reason. Push only to the fork; work reaches "
|
|
839
|
+
"upstream only through a pull request opened from the fork. Before any "
|
|
840
|
+
"push, read the full, untruncated `git remote -v` output and confirm the "
|
|
841
|
+
"target remote is the fork"
|
|
842
|
+
)
|
|
843
|
+
|
|
844
|
+
LEAVE_UPSTREAM_PULL_REQUESTS_ALONE = (
|
|
845
|
+
"Never comment on or modify pull requests on the upstream repository unless"
|
|
846
|
+
" the user has explicitly allowed it - through existing personal notes or "
|
|
847
|
+
"instructions, or by accepting when you ask"
|
|
848
|
+
)
|