Repository navigation
Expand file tree
/
Copy pathcheck_docs_formatted.py
More file actions
executable file
·416 lines (345 loc) · 12.7 KB
/
Copy pathcheck_docs_formatted.py
File metadata and controls
executable file
·416 lines (345 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python3
#
# /// script
# requires-python = ">=3.12"
# dependencies = ["ruff"]
#
# [tool.ty.rules]
# truthiness-test-of-none-union = "warn"
# blanket-ignore-comment = "warn"
# missing-type-argument = "warn"
# possibly-unresolved-reference = "warn"
# unsound-return-statement = "warn"
# unsound-yield = "warn"
# unsupported-dynamic-base = "warn"
# division-by-zero = "warn"
# dynamic-function-decorator-return = "warn"
# unsound-assignment = "warn"
# redundant-condition-strict = "warn"
# disjoint-cast = "warn"
# missing-direct-dependency = "warn"
#
# [tool.uv]
# no-build = true
# exclude-newer = "P7D"
# ///
"""Check code snippets in docs are formatted by Ruff."""
from __future__ import annotations
import argparse
import json
import os
import re
import subprocess
import textwrap
from pathlib import Path
from re import Match
from typing import TYPE_CHECKING, Literal
if TYPE_CHECKING:
from collections.abc import Sequence
SNIPPED_RE = re.compile(
r"(?P<before>^(?P<indent>\x20*)```(?:\s*(?P<language>\w+))?\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)```\s*$)",
re.DOTALL | re.MULTILINE,
)
# Long explanation: https://www.rexegg.com/regex-best-trick.html
#
# Short explanation:
# Match both code blocks and shortcut links, then discard the former.
# Whatever matched by the second branch is guaranteed to never be
# part of a code block, as that would already be caught by the first.
BACKTICKED_SHORTCUT_LINK_RE = re.compile(
rf"""(?msx)
(?:{SNIPPED_RE}
| \[`(?P<name>[^`\n]+)`](?![\[(])
)
"""
)
# For some rules, we don't want Ruff to fix the formatting as this would "fix" the
# example.
KNOWN_FORMATTING_VIOLATIONS = [
"avoidable-escaped-quote",
"bad-quotes-docstring",
"bad-quotes-inline-string",
"bad-quotes-multiline-string",
"blank-line-after-decorator",
"blank-line-before-class",
"blank-line-before-function",
"blank-line-between-methods",
"blank-lines-after-function-or-class",
"blank-lines-before-nested-definition",
"blank-lines-top-level",
"docstring-tab-indentation",
"explicit-string-concatenation",
"f-string-missing-placeholders",
"incorrect-blank-line-after-class",
"incorrect-blank-line-before-class",
"indentation-with-invalid-multiple",
"indentation-with-invalid-multiple-comment",
"line-too-long",
"missing-trailing-comma",
"missing-whitespace",
"missing-whitespace-after-keyword",
"missing-whitespace-around-arithmetic-operator",
"missing-whitespace-around-bitwise-or-shift-operator",
"missing-whitespace-around-modulo-operator",
"missing-whitespace-around-operator",
"missing-whitespace-around-parameter-equals",
"module-import-not-at-top-of-file",
"multi-line-implicit-string-concatenation",
"multiple-leading-hashes-for-block-comment",
"multiple-spaces-after-comma",
"multiple-spaces-after-keyword",
"multiple-spaces-after-operator",
"multiple-spaces-before-keyword",
"multiple-spaces-before-operator",
"multiple-statements-on-one-line-colon",
"multiple-statements-on-one-line-semicolon",
"no-indented-block-comment",
"no-return-argument-annotation-in-stub",
"no-space-after-block-comment",
"no-space-after-inline-comment",
"non-empty-stub-body",
"over-indentation",
"over-indented",
"pass-statement-stub-body",
"prohibited-trailing-comma",
"redundant-backslash",
"shebang-leading-whitespace",
"single-line-implicit-string-concatenation",
"surrounding-whitespace",
"too-few-spaces-before-inline-comment",
"too-many-blank-lines",
"too-many-boolean-expressions",
"trailing-comma-on-bare-tuple",
"triple-single-quotes",
"under-indentation",
"unexpected-indentation-comment",
"unexpected-spaces-around-keyword-parameter-equals",
"unicode-kind-prefix",
"unnecessary-class-parentheses",
"unnecessary-escaped-quote",
"useless-semicolon",
"whitespace-after-decorator",
"whitespace-after-open-bracket",
"whitespace-before-close-bracket",
"whitespace-before-parameters",
"whitespace-before-punctuation",
]
# For some docs, Ruff is unable to parse the example code.
KNOWN_PARSE_ERRORS = [
"ban-lazy", # requires Python 3.15
"blank-line-with-whitespace",
"indented-form-feed",
"lazy-import-immediately-resolved", # requires Python 3.15
"lazy-import-mismatch", # requires Python 3.15
"missing-newline-at-end-of-file",
"mixed-spaces-and-tabs",
"no-indented-block",
"non-pep695-type-alias", # requires Python 3.12
"quadratic-list-summation", # requires Python 3.15
"require-lazy", # requires Python 3.15
"syntax-error",
"tab-after-comma",
"tab-after-keyword",
"tab-after-operator",
"tab-before-keyword",
"tab-before-operator",
"too-many-newlines-at-end-of-file",
"trailing-whitespace",
"unexpected-indentation",
]
class CodeBlockError(Exception):
"""A code block parse error."""
class InvalidInput(ValueError):
"""Raised when ruff fails to parse file."""
def format_str(code: str, extension: Literal["py", "pyi"]) -> str:
"""Format a code block with ruff by writing to a temporary file."""
# Run ruff to format the tmp file
try:
completed_process = subprocess.run(
["ruff", "format", "--stdin-filename", f"file.{extension}", "-"],
check=True,
capture_output=True,
text=True,
input=code,
)
except subprocess.CalledProcessError as e:
err = e.stderr
if "error: Failed to parse" in err:
raise InvalidInput(err) from e
raise NotImplementedError(
"This error has not been handled correctly, please update "
f"`check_docs_formatted.py\n\nError:\n\n{err}",
) from e
return completed_process.stdout
def format_contents(src: str) -> tuple[str, Sequence[CodeBlockError]]:
"""Format a single docs content."""
errors: list[CodeBlockError] = []
def _snipped_match(match: Match[str]) -> str:
language = match["language"]
extension: Literal["py", "pyi"]
match language:
case "python":
extension = "py"
case "pyi":
extension = "pyi"
case _:
# We are only interested in checking the formatting of py or pyi code
# blocks so we can return early if the language is not one of these.
return f"{match['before']}{match['code']}{match['after']}"
code = textwrap.dedent(match["code"])
try:
code = format_str(code, extension)
except InvalidInput as e:
errors.append(CodeBlockError(e))
except NotImplementedError:
raise
code = textwrap.indent(code, match["indent"])
return f"{match['before']}{code}{match['after']}"
src = SNIPPED_RE.sub(_snipped_match, src)
return src, errors
def format_file(file: Path, error_known: bool, args: argparse.Namespace) -> int:
"""Check the formatting of a single docs file.
Returns the exit code for the script.
"""
with file.open() as f:
contents = f.read()
if file.parent.name == "rules":
# Check contents contains "What it does" section
if "## What it does" not in contents:
print(f"Docs for `{file.name}` are missing the `What it does` section.")
return 1
# Check contents contains "Why is this bad?" section
if "## Why is this bad?" not in contents:
print(f"Docs for `{file.name}` are missing the `Why is this bad?` section.")
return 1
# Remove everything before the first example
contents = contents[contents.find("## Example") :]
# Remove everything after the last example
contents = contents[: contents.rfind("```")] + "```"
new_contents, errors = format_contents(contents)
if errors and not args.skip_errors and not error_known:
for error in errors:
rule_name = file.name.split(".")[0]
print(
f"Docs parse error for `{rule_name}` docs. Either fix or add to "
f"`KNOWN_PARSE_ERRORS`. {error}",
)
return 2
if contents != new_contents:
rule_name = file.name.split(".")[0]
print(
f"Rule `{rule_name}` docs are not formatted. Either format the rule or add "
f"to `KNOWN_FORMATTING_VIOLATIONS`. The example section should be "
f"rewritten to:",
)
# Add indentation so that snipped can be copied directly to docs
for line in new_contents.splitlines():
output_line = "///"
if len(line) > 0:
output_line = f"{output_line} {line}"
print(output_line)
print("\n")
return 1
return 0
def find_backticked_shortcut_links(
path: Path, all_config_names: dict[str, object]
) -> set[str]:
"""Check for links of the form: [`foobar`].
See explanation at #16010.
"""
with path.open() as file:
contents = file.read()
broken_link_names: set[str] = set()
for match in BACKTICKED_SHORTCUT_LINK_RE.finditer(contents):
name = match["name"]
if name is not None and name not in all_config_names:
broken_link_names.add(name)
return broken_link_names
def main(argv: Sequence[str] | None = None) -> int:
"""Check code snippets in docs are formatted by Ruff."""
parser = argparse.ArgumentParser(
description="Check code snippets in docs are formatted by Ruff.",
)
parser.add_argument("--skip-errors", action="store_true")
args = parser.parse_args(argv)
# Get static docs
static_docs = [Path("docs") / f for f in os.listdir("docs") if f.endswith(".md")]
# Check rules generated
if not Path("docs/rules").exists():
print("Please generate rules first.")
return 1
# Get generated rules
generated_docs = [
Path("docs/rules") / f for f in os.listdir("docs/rules") if f.endswith(".md")
]
if len(generated_docs) == 0:
print("Please generate rules first.")
return 1
# Check known formatting violations and parse errors are sorted alphabetically and
# have no duplicates. This will reduce the diff when adding new violations
for known_list, file_string in [
(KNOWN_FORMATTING_VIOLATIONS, "formatting violations"),
(KNOWN_PARSE_ERRORS, "parse errors"),
]:
if known_list != sorted(known_list):
print(
f"Known {file_string} is not sorted alphabetically. Please sort and "
f"re-run.",
)
return 1
duplicates = list({x for x in known_list if known_list.count(x) > 1})
if len(duplicates) > 0:
print(f"Known {file_string} has duplicates:")
print("\n".join([f" - {x}" for x in duplicates]))
print("Please remove them and re-run.")
return 1
ruff_config_output = subprocess.check_output(
["ruff", "config", "--output-format", "json"], encoding="utf-8"
)
all_config_names = json.loads(ruff_config_output)
violations = 0
errors = 0
broken_links: dict[str, set[str]] = {}
print("Checking docs formatting...")
for file in [*static_docs, *generated_docs]:
rule_name = file.name.split(".")[0]
if rule_name in KNOWN_FORMATTING_VIOLATIONS:
continue
error_known = rule_name in KNOWN_PARSE_ERRORS
result = format_file(file, error_known, args)
if result == 1:
violations += 1
elif result == 2 and not error_known:
errors += 1
broken_links_in_file = find_backticked_shortcut_links(file, all_config_names)
if broken_links_in_file:
broken_links[file.name] = broken_links_in_file
if violations > 0:
print(f"Formatting violations identified: {violations}")
if errors > 0:
print(f"New code block parse errors identified: {errors}")
if broken_links:
print()
print("Do not use backticked shortcut links: [`foobar`]")
print(
"They work with Mkdocs but cannot be rendered by CommonMark and GFM-compliant implementers."
)
print("Instead, use an explicit label:")
print("```markdown")
print("[`lorem.ipsum`][lorem-ipsum]")
print()
print("[lorem-ipsum]: https://example.com/")
print("```")
print()
print("The following links are found to be broken:")
for filename, link_names in broken_links.items():
print(f"- {filename}:")
print("\n".join(f" - {name}" for name in link_names))
if violations > 0 or errors > 0 or broken_links:
return 1
print("All docs are formatted correctly.")
return 0
if __name__ == "__main__":
raise SystemExit(main())