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,255 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Finds modules, classes, functions and fields that have no docstring.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import abc
|
|
8
|
+
import importlib
|
|
9
|
+
import sys
|
|
10
|
+
from collections.abc import Iterator
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from enum import StrEnum
|
|
13
|
+
from functools import cached_property
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from types import ModuleType
|
|
16
|
+
from typing import override
|
|
17
|
+
|
|
18
|
+
import libcst
|
|
19
|
+
|
|
20
|
+
from basstler.code_modification.positions import PositionedModule
|
|
21
|
+
from basstler.code_modification.syntax import BodySyntax
|
|
22
|
+
from basstler.code_rules.checker import CodeRuleViolation
|
|
23
|
+
from basstler.code_rules.syntax_tree_checker import SyntaxTreeChecker
|
|
24
|
+
|
|
25
|
+
# %% what is documented
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class DocumentedKind(StrEnum):
|
|
29
|
+
"""
|
|
30
|
+
The kinds of code that carry a docstring.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
MODULE = "module"
|
|
34
|
+
"""
|
|
35
|
+
A whole source file.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
CLASS = "class"
|
|
39
|
+
"""
|
|
40
|
+
A class definition.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
FUNCTION = "function"
|
|
44
|
+
"""
|
|
45
|
+
A function or method definition.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
FIELD = "field"
|
|
49
|
+
"""
|
|
50
|
+
An annotated assignment in a class body.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True)
|
|
55
|
+
class ImportedModule:
|
|
56
|
+
"""
|
|
57
|
+
A source file as the module Python imports it, which tells whether a method
|
|
58
|
+
implements an abstract method of a base class.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
path: Path
|
|
62
|
+
"""
|
|
63
|
+
The source file of the module.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
@cached_property
|
|
67
|
+
def module(self) -> ModuleType | None:
|
|
68
|
+
"""
|
|
69
|
+
The module imported from :attr:`path`; none when it cannot be imported.
|
|
70
|
+
"""
|
|
71
|
+
source = self.path.resolve()
|
|
72
|
+
root = source.parent
|
|
73
|
+
while (root / "__init__.py").is_file():
|
|
74
|
+
root = root.parent
|
|
75
|
+
if str(root) not in sys.path:
|
|
76
|
+
sys.path.insert(0, str(root))
|
|
77
|
+
name = ".".join(source.relative_to(root).with_suffix("").parts)
|
|
78
|
+
try:
|
|
79
|
+
module = importlib.import_module(name.removesuffix(".__init__"))
|
|
80
|
+
except Exception:
|
|
81
|
+
# Handle unimportable modules gracefully: whatever the import raises, only
|
|
82
|
+
# the methods of this module go unchecked.
|
|
83
|
+
return None
|
|
84
|
+
if module.__file__ is None or Path(module.__file__).resolve() != source:
|
|
85
|
+
return None
|
|
86
|
+
return module
|
|
87
|
+
|
|
88
|
+
def class_named(self, scope: tuple[str, ...] | None) -> type | None:
|
|
89
|
+
"""
|
|
90
|
+
:param scope: The names of a class and the classes enclosing it; none for a
|
|
91
|
+
class defined inside a function.
|
|
92
|
+
:return: The class; none when it cannot be found.
|
|
93
|
+
"""
|
|
94
|
+
if scope is None or self.module is None:
|
|
95
|
+
return None
|
|
96
|
+
found: ModuleType | type | None = self.module
|
|
97
|
+
for name in scope:
|
|
98
|
+
found = vars(found).get(name)
|
|
99
|
+
if not isinstance(found, type):
|
|
100
|
+
return None
|
|
101
|
+
return found
|
|
102
|
+
|
|
103
|
+
def implements_abstract_method(
|
|
104
|
+
self, scope: tuple[str, ...] | None, method_name: str
|
|
105
|
+
) -> bool:
|
|
106
|
+
"""
|
|
107
|
+
:param scope: The names of a class and the classes enclosing it.
|
|
108
|
+
:param method_name: The name of a method of the class.
|
|
109
|
+
:return: Whether the first base class defining *method_name* leaves it abstract.
|
|
110
|
+
"""
|
|
111
|
+
for base in self.class_named(scope).__mro__[1:]:
|
|
112
|
+
if method_name in vars(base):
|
|
113
|
+
return (
|
|
114
|
+
isinstance(base, abc.ABCMeta)
|
|
115
|
+
and method_name in base.__abstractmethods__
|
|
116
|
+
)
|
|
117
|
+
return False
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# %% the check
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@dataclass(frozen=True)
|
|
124
|
+
class MissingDocstringChecker(SyntaxTreeChecker):
|
|
125
|
+
"""
|
|
126
|
+
Finds undocumented code; a method implementing an abstract method inherits its
|
|
127
|
+
base's docstring and needs none of its own.
|
|
128
|
+
"""
|
|
129
|
+
|
|
130
|
+
@override
|
|
131
|
+
def violations_of_module(
|
|
132
|
+
self, path: Path, module: PositionedModule
|
|
133
|
+
) -> Iterator[CodeRuleViolation]:
|
|
134
|
+
if module.module.body and module.module.get_docstring(clean=False) is None:
|
|
135
|
+
last_line = module.positions[module.module.body[-1]].end.line
|
|
136
|
+
yield self.violation(path, DocumentedKind.MODULE, 1, last_line)
|
|
137
|
+
yield from self.undocumented_definitions(
|
|
138
|
+
path, module, module.module, (), ImportedModule(path)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
def undocumented_definitions(
|
|
142
|
+
self,
|
|
143
|
+
path: Path,
|
|
144
|
+
module: PositionedModule,
|
|
145
|
+
parent: libcst.CSTNode,
|
|
146
|
+
scope: tuple[str, ...] | None,
|
|
147
|
+
imported: ImportedModule,
|
|
148
|
+
methods: frozenset[libcst.CSTNode] = frozenset(),
|
|
149
|
+
) -> Iterator[CodeRuleViolation]:
|
|
150
|
+
"""
|
|
151
|
+
:param module: The module of *path*.
|
|
152
|
+
:param parent: A node whose descendants are checked.
|
|
153
|
+
:param scope: The names of the classes enclosing *parent*; none inside a
|
|
154
|
+
function, where a class cannot be looked up.
|
|
155
|
+
:param imported: The module of *path* as Python imports it.
|
|
156
|
+
:param methods: The functions defined directly in the body of the class
|
|
157
|
+
enclosing *parent*.
|
|
158
|
+
:return: A violation for every undocumented class, function and field below
|
|
159
|
+
*parent*. A method is skipped when its class cannot be looked up, and
|
|
160
|
+
when it implements an abstract method, whose docstring suffices.
|
|
161
|
+
"""
|
|
162
|
+
for node in parent.children:
|
|
163
|
+
if isinstance(node, libcst.ClassDef):
|
|
164
|
+
yield from self.undocumented_fields(path, module, node)
|
|
165
|
+
if node.get_docstring(clean=False) is None:
|
|
166
|
+
yield self.definition_violation(
|
|
167
|
+
path, module, DocumentedKind.CLASS, node
|
|
168
|
+
)
|
|
169
|
+
class_scope = None if scope is None else (*scope, node.name.value)
|
|
170
|
+
members = frozenset(
|
|
171
|
+
statement
|
|
172
|
+
for statement in BodySyntax(node.body).statements
|
|
173
|
+
if isinstance(statement, libcst.FunctionDef)
|
|
174
|
+
)
|
|
175
|
+
yield from self.undocumented_definitions(
|
|
176
|
+
path, module, node, class_scope, imported, members
|
|
177
|
+
)
|
|
178
|
+
continue
|
|
179
|
+
if not isinstance(node, libcst.FunctionDef):
|
|
180
|
+
yield from self.undocumented_definitions(
|
|
181
|
+
path, module, node, scope, imported, methods
|
|
182
|
+
)
|
|
183
|
+
continue
|
|
184
|
+
if node.get_docstring(clean=False) is None and not self.is_exempt_method(
|
|
185
|
+
methods, scope, node, imported
|
|
186
|
+
):
|
|
187
|
+
yield self.definition_violation(
|
|
188
|
+
path, module, DocumentedKind.FUNCTION, node
|
|
189
|
+
)
|
|
190
|
+
yield from self.undocumented_definitions(path, module, node, None, imported)
|
|
191
|
+
|
|
192
|
+
@staticmethod
|
|
193
|
+
def is_exempt_method(
|
|
194
|
+
methods: frozenset[libcst.CSTNode],
|
|
195
|
+
scope: tuple[str, ...] | None,
|
|
196
|
+
node: libcst.FunctionDef,
|
|
197
|
+
imported: ImportedModule,
|
|
198
|
+
) -> bool:
|
|
199
|
+
"""
|
|
200
|
+
:return: Whether *node* is a method that needs no docstring of its own: its class
|
|
201
|
+
cannot be looked up, or it implements an abstract method.
|
|
202
|
+
"""
|
|
203
|
+
if node not in methods:
|
|
204
|
+
return False
|
|
205
|
+
if imported.class_named(scope) is None:
|
|
206
|
+
return True
|
|
207
|
+
return imported.implements_abstract_method(scope, node.name.value)
|
|
208
|
+
|
|
209
|
+
def undocumented_fields(
|
|
210
|
+
self, path: Path, module: PositionedModule, definition: libcst.ClassDef
|
|
211
|
+
) -> Iterator[CodeRuleViolation]:
|
|
212
|
+
"""
|
|
213
|
+
:return: A violation for every annotated assignment in *definition*'s body that no
|
|
214
|
+
string literal directly follows.
|
|
215
|
+
"""
|
|
216
|
+
statements = BodySyntax(definition.body).statements
|
|
217
|
+
for index, statement in enumerate(statements):
|
|
218
|
+
if not isinstance(statement, libcst.AnnAssign):
|
|
219
|
+
continue
|
|
220
|
+
following = statements[index + 1 : index + 2]
|
|
221
|
+
if following and BodySyntax.is_string(following[0]):
|
|
222
|
+
continue
|
|
223
|
+
location = module.positions[statement]
|
|
224
|
+
yield self.violation(
|
|
225
|
+
path, DocumentedKind.FIELD, location.start.line, location.end.line
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
def definition_violation(
|
|
229
|
+
self,
|
|
230
|
+
path: Path,
|
|
231
|
+
module: PositionedModule,
|
|
232
|
+
kind: DocumentedKind,
|
|
233
|
+
definition: libcst.ClassDef | libcst.FunctionDef,
|
|
234
|
+
) -> CodeRuleViolation:
|
|
235
|
+
"""
|
|
236
|
+
:return: The violation of the undocumented class or function *definition*,
|
|
237
|
+
starting at its first decorator, else at its keyword.
|
|
238
|
+
"""
|
|
239
|
+
decorators = definition.decorators
|
|
240
|
+
first = module.positions[decorators[0] if decorators else definition]
|
|
241
|
+
return self.violation(
|
|
242
|
+
path, kind, first.start.line, module.positions[definition].end.line
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
@staticmethod
|
|
246
|
+
def violation(
|
|
247
|
+
path: Path, kind: DocumentedKind, first_line: int, last_line: int
|
|
248
|
+
) -> CodeRuleViolation:
|
|
249
|
+
"""
|
|
250
|
+
:return: The violation of the undocumented *kind* of code spanning *first_line* to
|
|
251
|
+
*last_line* of *path*.
|
|
252
|
+
"""
|
|
253
|
+
return CodeRuleViolation(
|
|
254
|
+
path, first_line, last_line, f"{kind} has no docstring; add one"
|
|
255
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The errors raised while checking and fixing code against the rules.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
|
|
9
|
+
from basstler.exceptions import DataclassException
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class FixDoesNotConvergeError(DataclassException, RuntimeError):
|
|
14
|
+
"""
|
|
15
|
+
Raised when applying fixes leaves at least as many places breaking rules as there
|
|
16
|
+
were before, so fixing again would never end.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
lines: tuple[int, ...]
|
|
20
|
+
"""
|
|
21
|
+
The first line of each place still breaking a rule, counting from one.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def error_message(self) -> str:
|
|
25
|
+
return (
|
|
26
|
+
f"fixing left {len(self.lines)} places breaking a rule, no fewer than "
|
|
27
|
+
f"before, on lines {', '.join(map(str, self.lines))}"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
def suggest_correction(self) -> str:
|
|
31
|
+
return (
|
|
32
|
+
"A fix whose replacement still breaks its rule needs correcting; its "
|
|
33
|
+
"examples' tests show which."
|
|
34
|
+
)
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The fixes that remove what a checker reports, the behaviour each declares, and applying
|
|
3
|
+
them to a module.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from abc import ABC, abstractmethod
|
|
9
|
+
from collections.abc import Iterator
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from enum import StrEnum
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
import libcst
|
|
15
|
+
|
|
16
|
+
from basstler.class_property import classproperty
|
|
17
|
+
from basstler.code_modification.actions import (
|
|
18
|
+
Action,
|
|
19
|
+
ActionPlan,
|
|
20
|
+
AppliedAction,
|
|
21
|
+
ImportInsertion,
|
|
22
|
+
NodeReplacement,
|
|
23
|
+
)
|
|
24
|
+
from basstler.code_modification.positions import PositionedModule
|
|
25
|
+
from basstler.code_modification.source_file import PythonSourceFile
|
|
26
|
+
from basstler.code_modification.specification import Specification
|
|
27
|
+
from basstler.code_modification.syntax import CallSyntax
|
|
28
|
+
from basstler.code_rules.checker import CodeRuleChecker, CodeRuleViolation
|
|
29
|
+
from basstler.code_rules.commands import CodeRulesCommand
|
|
30
|
+
from basstler.code_rules.exceptions import FixDoesNotConvergeError
|
|
31
|
+
|
|
32
|
+
# %% behaviour
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class BehaviourEffect(StrEnum):
|
|
36
|
+
"""
|
|
37
|
+
Whether fixed code behaves as the code it replaces.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
PRESERVED = "preserved"
|
|
41
|
+
"""
|
|
42
|
+
The fixed code does what the code it replaces did.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
CHANGED = "changed"
|
|
46
|
+
"""
|
|
47
|
+
The fixed code does something else, which the declaration explains.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(frozen=True)
|
|
52
|
+
class BehaviourDeclaration:
|
|
53
|
+
"""
|
|
54
|
+
How a fix changes what the code it fixes does.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
effect: BehaviourEffect
|
|
58
|
+
"""
|
|
59
|
+
Whether the behaviour changes.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
explanation: str
|
|
63
|
+
"""
|
|
64
|
+
What the fixed code does differently, or why it does the same.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# %% fixes
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class CodeFix[SpecificationType: Specification](ABC):
|
|
72
|
+
"""
|
|
73
|
+
The one change that removes a place a checker specifies.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
@classproperty
|
|
77
|
+
@abstractmethod
|
|
78
|
+
def behaviour(cls) -> BehaviourDeclaration:
|
|
79
|
+
"""
|
|
80
|
+
How the fixed code behaves compared with the code it replaces.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
@classproperty
|
|
84
|
+
def required_import(cls) -> type | None:
|
|
85
|
+
"""
|
|
86
|
+
The class the replacement needs imported in the module, if any.
|
|
87
|
+
"""
|
|
88
|
+
return None
|
|
89
|
+
|
|
90
|
+
@abstractmethod
|
|
91
|
+
def replacement(self, specification: SpecificationType) -> libcst.CSTNode:
|
|
92
|
+
"""
|
|
93
|
+
:param specification: A place breaking the fix's rule.
|
|
94
|
+
:return: The node taking the place of the specification's node, which no
|
|
95
|
+
longer breaks the rule.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@dataclass(frozen=True)
|
|
100
|
+
class CallLackingKeywordArgument(Specification):
|
|
101
|
+
"""
|
|
102
|
+
A call that has to pass one more keyword argument.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
node: libcst.Call
|
|
106
|
+
"""
|
|
107
|
+
The call.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
superseded_argument: libcst.Arg | None = None
|
|
111
|
+
"""
|
|
112
|
+
The argument of the call the keyword argument takes the place of, if any.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class KeywordArgumentFix(CodeFix[CallLackingKeywordArgument], ABC):
|
|
117
|
+
"""
|
|
118
|
+
Passes a call one keyword argument, in place of the argument it supersedes where
|
|
119
|
+
the call has one.
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
@classproperty
|
|
123
|
+
@abstractmethod
|
|
124
|
+
def keyword(cls) -> str:
|
|
125
|
+
"""
|
|
126
|
+
The keyword the argument is passed by.
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
@classproperty
|
|
130
|
+
@abstractmethod
|
|
131
|
+
def value(cls) -> libcst.BaseExpression:
|
|
132
|
+
"""
|
|
133
|
+
The value passed.
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
def replacement(self, specification: CallLackingKeywordArgument) -> libcst.Call:
|
|
137
|
+
call = CallSyntax(specification.node)
|
|
138
|
+
argument = CallSyntax.keyword_argument(self.keyword, self.value)
|
|
139
|
+
if specification.superseded_argument is None:
|
|
140
|
+
return call.with_argument_appended(argument)
|
|
141
|
+
return call.with_argument_replaced(specification.superseded_argument, argument)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class FixableChecker[SpecificationType: Specification](CodeRuleChecker, ABC):
|
|
145
|
+
"""
|
|
146
|
+
A checker each place it reports specifies the change its fix makes there.
|
|
147
|
+
"""
|
|
148
|
+
|
|
149
|
+
@classproperty
|
|
150
|
+
@abstractmethod
|
|
151
|
+
def fix(cls) -> CodeFix[SpecificationType]:
|
|
152
|
+
"""
|
|
153
|
+
The fix removing each place the checker reports.
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
@classproperty
|
|
157
|
+
@abstractmethod
|
|
158
|
+
def violation_description(cls) -> str:
|
|
159
|
+
"""
|
|
160
|
+
What is wrong with each place the checker reports, as one sentence fragment.
|
|
161
|
+
"""
|
|
162
|
+
|
|
163
|
+
@abstractmethod
|
|
164
|
+
def specifications_of_module(
|
|
165
|
+
self, module: PositionedModule
|
|
166
|
+
) -> Iterator[SpecificationType]:
|
|
167
|
+
"""
|
|
168
|
+
:param module: A module to check.
|
|
169
|
+
:return: Every place in it that breaks the rule.
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
def checks(self, path: Path) -> bool:
|
|
173
|
+
"""
|
|
174
|
+
:param path: A Python source file.
|
|
175
|
+
:return: Whether the checker reports places in it.
|
|
176
|
+
"""
|
|
177
|
+
return True
|
|
178
|
+
|
|
179
|
+
def violations(self, path: Path) -> Iterator[CodeRuleViolation]:
|
|
180
|
+
if not self.checks(path):
|
|
181
|
+
return
|
|
182
|
+
module = PositionedModule(PythonSourceFile(path).parse())
|
|
183
|
+
for specification in self.specifications_of_module(module):
|
|
184
|
+
yield CodeRuleViolation(
|
|
185
|
+
path,
|
|
186
|
+
specification.location.start.line,
|
|
187
|
+
specification.location.end.line,
|
|
188
|
+
f"{self.violation_description}; `{CodeRulesCommand.FIX.invocation}` "
|
|
189
|
+
"fixes it",
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
# %% fixing a module
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
@dataclass(frozen=True)
|
|
197
|
+
class FixablePlace:
|
|
198
|
+
"""
|
|
199
|
+
A place breaking a rule, and the fix that removes it.
|
|
200
|
+
"""
|
|
201
|
+
|
|
202
|
+
fix: CodeFix
|
|
203
|
+
"""
|
|
204
|
+
The fix of the checker that found the place.
|
|
205
|
+
"""
|
|
206
|
+
|
|
207
|
+
specification: Specification
|
|
208
|
+
"""
|
|
209
|
+
The place.
|
|
210
|
+
"""
|
|
211
|
+
|
|
212
|
+
@property
|
|
213
|
+
def action(self) -> Action[libcst.Module]:
|
|
214
|
+
"""
|
|
215
|
+
The action making the fix's change at the place.
|
|
216
|
+
"""
|
|
217
|
+
return NodeReplacement(
|
|
218
|
+
self.specification.location,
|
|
219
|
+
self.specification.node,
|
|
220
|
+
self.fix.replacement(self.specification),
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
@dataclass(frozen=True)
|
|
225
|
+
class SourceFixer:
|
|
226
|
+
"""
|
|
227
|
+
Applies the fixes of a set of checkers to a module until no checker reports
|
|
228
|
+
anything.
|
|
229
|
+
"""
|
|
230
|
+
|
|
231
|
+
checkers: tuple[FixableChecker, ...]
|
|
232
|
+
"""
|
|
233
|
+
The checkers whose fixes are applied.
|
|
234
|
+
"""
|
|
235
|
+
|
|
236
|
+
def fix_module(self, source_file: PythonSourceFile) -> AppliedAction[libcst.Module]:
|
|
237
|
+
"""
|
|
238
|
+
:param source_file: The module to fix, which is left as it is.
|
|
239
|
+
:return: The module with every place the checkers report fixed, and the action
|
|
240
|
+
restoring the module exactly.
|
|
241
|
+
:raises FixDoesNotConvergeError: If a round of fixes leaves at least as many
|
|
242
|
+
places breaking a rule as there were before it.
|
|
243
|
+
"""
|
|
244
|
+
inverses: list[Action[libcst.Module]] = []
|
|
245
|
+
imports: list[type] = []
|
|
246
|
+
module = source_file.parse()
|
|
247
|
+
places = self.places(module, source_file.path)
|
|
248
|
+
while places:
|
|
249
|
+
imports.extend(
|
|
250
|
+
place.fix.required_import
|
|
251
|
+
for place in places
|
|
252
|
+
if place.fix.required_import is not None
|
|
253
|
+
)
|
|
254
|
+
applied = self.round_of_fixes(places).apply(module)
|
|
255
|
+
module = applied.result
|
|
256
|
+
inverses.append(applied.inverse)
|
|
257
|
+
remaining = self.places(module, source_file.path)
|
|
258
|
+
if len(remaining) >= len(places):
|
|
259
|
+
raise FixDoesNotConvergeError(
|
|
260
|
+
tuple(
|
|
261
|
+
place.specification.location.start.line for place in remaining
|
|
262
|
+
)
|
|
263
|
+
)
|
|
264
|
+
places = remaining
|
|
265
|
+
for imported in dict.fromkeys(imports):
|
|
266
|
+
applied = ImportInsertion(imported).apply(module)
|
|
267
|
+
module = applied.result
|
|
268
|
+
inverses.append(applied.inverse)
|
|
269
|
+
return AppliedAction(module, ActionPlan(tuple(reversed(inverses))))
|
|
270
|
+
|
|
271
|
+
def places(self, module: libcst.Module, path: Path) -> list[FixablePlace]:
|
|
272
|
+
"""
|
|
273
|
+
:param path: The file *module* was parsed from.
|
|
274
|
+
:return: Every place in *module* that one of :attr:`checkers` that checks *path*
|
|
275
|
+
reports.
|
|
276
|
+
"""
|
|
277
|
+
positioned = PositionedModule(module)
|
|
278
|
+
return [
|
|
279
|
+
FixablePlace(checker.fix, specification)
|
|
280
|
+
for checker in self.checkers
|
|
281
|
+
if checker.checks(path)
|
|
282
|
+
for specification in checker.specifications_of_module(positioned)
|
|
283
|
+
]
|
|
284
|
+
|
|
285
|
+
@staticmethod
|
|
286
|
+
def round_of_fixes(places: list[FixablePlace]) -> ActionPlan[libcst.Module]:
|
|
287
|
+
"""
|
|
288
|
+
:param places: Places breaking a rule, some perhaps inside others.
|
|
289
|
+
:return: The fixes of every place that overlaps no place before it, outermost
|
|
290
|
+
first, applied from the end of the module towards its start so that each
|
|
291
|
+
leaves the location of the next unchanged.
|
|
292
|
+
"""
|
|
293
|
+
chosen: list[FixablePlace] = []
|
|
294
|
+
for place in sorted(
|
|
295
|
+
places,
|
|
296
|
+
key=lambda place: (
|
|
297
|
+
place.specification.location.start.line,
|
|
298
|
+
place.specification.location.start.column,
|
|
299
|
+
-place.specification.location.end.line,
|
|
300
|
+
-place.specification.location.end.column,
|
|
301
|
+
),
|
|
302
|
+
):
|
|
303
|
+
if not any(
|
|
304
|
+
place.specification.overlaps(other.specification) for other in chosen
|
|
305
|
+
):
|
|
306
|
+
chosen.append(place)
|
|
307
|
+
return ActionPlan(tuple(place.action for place in reversed(chosen)))
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Generates the repository's ``AGENTS.md`` from the rules it declares.
|
|
3
|
+
|
|
4
|
+
Copied from krrood :cite:`krrood` (``krrood.development_tools.agents.instructions``)
|
|
5
|
+
until krrood's light core is released.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from basstler.code_rules import rules
|
|
14
|
+
from basstler.code_rules.commands import CodeRulesCommand
|
|
15
|
+
from basstler.code_rules.rule import Section
|
|
16
|
+
from basstler.code_rules.rule_module import RuleModule
|
|
17
|
+
from basstler.locations import InstructionsTemplate, ProjectLocation
|
|
18
|
+
from basstler.render_common import create_template_environment
|
|
19
|
+
from basstler.text_file import TextFile
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class AgentsInstructions:
|
|
24
|
+
"""
|
|
25
|
+
The ``AGENTS.md`` of one repository, listing the rules of the given sections.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
repository_root: Path
|
|
29
|
+
"""
|
|
30
|
+
The directory holding the repository's ``AGENTS.md``.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
sections: list[type[Section]]
|
|
34
|
+
"""
|
|
35
|
+
The top-level sections the file lists, each with the sections nested under it.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
@classmethod
|
|
39
|
+
def of_repository(cls, repository_root: Path) -> AgentsInstructions:
|
|
40
|
+
"""
|
|
41
|
+
:param repository_root: The directory holding the repository's ``AGENTS.md``.
|
|
42
|
+
:return: The file listing the sections of :mod:`basstler.code_rules.rules`.
|
|
43
|
+
"""
|
|
44
|
+
return cls(repository_root, RuleModule(rules).sections)
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def path(self) -> Path:
|
|
48
|
+
"""
|
|
49
|
+
The path of the repository's ``AGENTS.md``.
|
|
50
|
+
"""
|
|
51
|
+
return self.repository_root / ProjectLocation.AGENTS_INSTRUCTIONS
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def text(self) -> str:
|
|
55
|
+
"""
|
|
56
|
+
The content ``AGENTS.md`` must have: the rules of :attr:`sections`, under their
|
|
57
|
+
headings, in the order they are declared.
|
|
58
|
+
"""
|
|
59
|
+
template = create_template_environment().get_template(
|
|
60
|
+
InstructionsTemplate.AGENTS.name
|
|
61
|
+
)
|
|
62
|
+
return template.render(
|
|
63
|
+
sections=self.sections,
|
|
64
|
+
generate_command=CodeRulesCommand.AGENTS_MD.invocation,
|
|
65
|
+
check_command=CodeRulesCommand.CHECK.invocation,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def is_current(self) -> bool:
|
|
69
|
+
"""
|
|
70
|
+
:return: Whether ``AGENTS.md`` exists and equals :attr:`text`.
|
|
71
|
+
"""
|
|
72
|
+
if not self.path.is_file():
|
|
73
|
+
return False
|
|
74
|
+
return TextFile(self.path).read() == self.text
|
|
75
|
+
|
|
76
|
+
def write(self) -> None:
|
|
77
|
+
"""
|
|
78
|
+
Write ``AGENTS.md``, replacing whatever it held.
|
|
79
|
+
"""
|
|
80
|
+
TextFile(self.path).write(self.text)
|