Repository navigation
Expand file tree
/
Copy pathaudit_python.py
More file actions
2330 lines (2039 loc) · 82.3 KB
/
Copy pathaudit_python.py
File metadata and controls
2330 lines (2039 loc) · 82.3 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Audit Python for high-signal Power of Ten and code-smell findings.
This checker is intentionally dependency-free. It catches mechanically visible
signals and leaves design-sensitive smells to the companion skill's semantic
review. It is a guardrail, not a safety proof or a full static analyzer.
"""
from __future__ import annotations
import argparse
import ast
import io
import json
import os
import re
import subprocess
import sys
import tokenize
from collections import Counter, defaultdict, deque
from dataclasses import asdict, dataclass
from itertools import islice
from pathlib import Path
from types import MappingProxyType
from typing import Iterable, Iterator, Mapping, Sequence
try:
import tomllib
except ImportError: # Python 3.10 has no tomllib; project config is unavailable.
tomllib = None
EXCLUDED_DIRECTORIES = frozenset({
".git",
".hg",
".mypy_cache",
".pytest_cache",
".ruff_cache",
".tox",
".venv",
"__pycache__",
"build",
"dist",
"node_modules",
"site-packages",
"vendor",
"venv",
})
MAX_INPUT_PATHS = 256
MAX_PYTHON_FILES = 4_096
MAX_DISCOVERED_PATHS = 32_768
MAX_DIRECTORY_VISITS = MAX_DISCOVERED_PATHS + MAX_INPUT_PATHS + 1
MAX_SOURCE_BYTES = 2_000_000
MAX_TOTAL_SOURCE_BYTES = 32_000_000
MAX_GIT_OUTPUT_BYTES = 32_000_000
MAX_GIT_SECONDS = 30
MAX_FINDINGS = 10_000
MAX_AST_NODES = 100_000
MAX_CALL_GRAPH_STEPS = 1_000_000
MAX_CHAIN_DEPTH = 64
MAX_NUMERIC_UNARY_DEPTH = 8
MUTABLE_BUILDERS = frozenset({"dict", "list", "set", "defaultdict", "OrderedDict"})
DYNAMIC_BUILTINS = frozenset({"__import__", "compile", "eval", "exec"})
REFLECTION_BUILTINS = frozenset({"delattr", "getattr", "setattr"})
MUTATING_METHODS = frozenset({"add", "append", "extend", "insert", "setdefault", "update"})
TERMINATORS = (ast.Break, ast.Continue, ast.Raise, ast.Return)
MAX_BLOCK_DEPTH = 4
NESTING_STATEMENTS = (
ast.AsyncFor,
ast.AsyncFunctionDef,
ast.AsyncWith,
ast.ClassDef,
ast.For,
ast.FunctionDef,
ast.If,
ast.Match,
ast.Try,
ast.While,
ast.With,
)
MAX_DOCSTRING_LINES = 2_000
MAX_CONFIG_BYTES = 1_000_000
MAX_CONFIG_SEARCH_DEPTH = 64
CONFIG_FILENAME = "pyproject.toml"
CONFIG_TABLE = "reliable-python"
CONFIG_KEY = "docstring-style"
DOCSTYLE_ENV_VAR = "RELIABLE_PYTHON_DOCSTYLE"
DEFAULT_DOCSTRING_STYLE = "numpy"
DOCSTRING_STYLES = ("numpy", "google", "rest", "any")
STYLE_LABELS = MappingProxyType({"numpy": "NumPy", "google": "Google", "rest": "reST"})
# Canonical numpydoc sections; see https://numpydoc.readthedocs.io/en/latest/format.html
NUMPY_SECTION_NAMES = frozenset({
"Attributes",
"Examples",
"Methods",
"Notes",
"Other Parameters",
"Parameters",
"Raises",
"Receives",
"References",
"Returns",
"See Also",
"Warnings",
"Warns",
"Yields",
})
GOOGLE_SECTION_NAMES = frozenset({
"Args",
"Arguments",
"Attributes",
"Example",
"Examples",
"Keyword Args",
"Keyword Arguments",
"Methods",
"Note",
"Notes",
"Other Parameters",
"Raises",
"References",
"Returns",
"See Also",
"Todo",
"Warning",
"Warnings",
"Yields",
})
GOOGLE_PARAMETER_SECTIONS = frozenset({
"Args",
"Arguments",
"Keyword Args",
"Keyword Arguments",
})
DOCSTRING_EXEMPT_DECORATORS = frozenset({"deleter", "overload", "setter"})
REST_FIELD_RE = re.compile(
r"^\s*:(?:param|parameter|arg|argument|key|keyword|type|returns?|rtype"
r"|raises?|except|exception|var|ivar|cvar|vartype)\b"
)
REST_PARAMETER_RE = re.compile(
r"^\s*:(?:param|parameter|arg|argument|key|keyword)\s+"
r"(?:[^\s:]+\s+)?(?P<name>\*{0,2}\w+)\s*:"
)
SUPPRESSION_TOKEN = "quality: ignore["
SUPPRESSION_RE = re.compile(
r"#\s*quality:\s*ignore\[([A-Z][A-Z0-9]+)\]\s*-\s*(\S.*)$"
)
HUNK_RE = re.compile(r"@@\s+-\d+(?:,\d+)?\s+\+(\d+)(?:,(\d+))?\s+@@")
SEVERITY_RANK = MappingProxyType({"note": 0, "warning": 1, "error": 2})
class AuditError(ValueError):
"""Raised when a requested audit cannot be completed reliably."""
@dataclass(frozen=True)
class Finding:
"""One reported rule violation or code smell.
Attributes
----------
code : str
Rule identifier, such as ``"POT02"``, ``"CS14"``, or ``"DOC01"``.
severity : str
One of ``"note"``, ``"warning"``, or ``"error"``.
path : str
File the finding was reported against.
line : int
First source line covered by the finding, 1-indexed.
end_line : int
Last source line covered, used to intersect against a diff.
message : str
What the code demonstrably does.
remedy : str
Smallest correction that resolves the finding.
"""
code: str
severity: str
path: str
line: int
end_line: int
message: str
remedy: str
# quality: ignore[POT05] - module boundaries validate CLI, Git, source, AST, and output data
def _limit_finding(path: Path, message: str, remedy: str) -> Finding:
return Finding("LIMIT001", "error", str(path), 1, 1, message, remedy)
def _record_finding(findings: list[Finding], finding: Finding) -> None:
if len(findings) >= MAX_FINDINGS:
findings[MAX_FINDINGS - 1] = finding
else:
findings.append(finding)
def _record_limit(
findings: list[Finding], path: Path, message: str, remedy: str
) -> None:
_record_finding(findings, _limit_finding(path, message, remedy))
@dataclass(frozen=True)
class FunctionInfo:
"""A function or method located in the parsed module.
Attributes
----------
qualified_name : str
Dotted path from module scope, such as ``"ReviewContext.report"``.
class_name : str or None
Dotted name of the enclosing class, or ``None`` for a plain function.
node : ast.FunctionDef or ast.AsyncFunctionDef
Definition node this record describes.
"""
qualified_name: str
class_name: str | None
node: ast.FunctionDef | ast.AsyncFunctionDef
@dataclass(frozen=True)
class DiffSelection:
"""The changed Python scope discovered from a Git working tree.
Attributes
----------
root : Path
Absolute repository root the paths are resolved against.
paths : tuple of Path
Changed Python files that exist on disk.
changed_lines : dict
Maps each path to the inclusive line ranges that the diff touched.
"""
root: Path
paths: tuple[Path, ...]
changed_lines: dict[Path, tuple[tuple[int, int], ...]]
class SourceLines(list[str]):
"""Source lines plus suppression comments identified by Python tokenization."""
def __init__(self, source: str) -> None:
super().__init__(source.splitlines())
self.suppressions: dict[int, set[str]] = defaultdict(set)
self.invalid_suppressions: set[int] = set()
try:
tokens = tokenize.generate_tokens(io.StringIO(source).readline)
# quality: ignore[POT02] - source is rejected above MAX_SOURCE_BYTES
for token in tokens:
if token.type != tokenize.COMMENT or SUPPRESSION_TOKEN not in token.string:
continue
match = SUPPRESSION_RE.search(token.string)
if match:
# quality: ignore[POT03] - markers are capped by source byte size
self.suppressions[token.start[0]].add(match.group(1))
else:
# quality: ignore[POT03] - markers are capped by source byte size
self.invalid_suppressions.add(token.start[0])
except (IndentationError, tokenize.TokenError):
pass
@dataclass
class ReviewContext:
"""Per-file state shared by every check in one audit pass."""
tree: ast.Module
lines: SourceLines
path: Path
parents: dict[ast.AST, ast.AST]
findings: list[Finding]
docstring_style: str = DEFAULT_DOCSTRING_STYLE
def report(
self,
*,
code: str,
severity: str,
node: ast.AST,
message: str,
remedy: str,
) -> None:
"""Record one finding unless a suppression covers it.
Parameters
----------
code : str
Rule identifier to report.
severity : str
One of ``"note"``, ``"warning"``, or ``"error"``.
node : ast.AST
Node whose line range the finding anchors to.
message : str
What the code demonstrably does.
remedy : str
Smallest correction that resolves the finding.
Raises
------
AuditError
If the file already holds ``MAX_FINDINGS`` findings.
"""
# quality: ignore[POT08] - ast.AST line metadata is optional by contract
line = int(getattr(node, "lineno", 1))
if _is_suppressed(self.lines, line, code):
return
if len(self.findings) >= MAX_FINDINGS:
raise AuditError(f"finding count exceeds {MAX_FINDINGS}")
self.findings.append(
Finding(
code,
severity,
str(self.path),
line,
_node_end(node),
message,
remedy,
)
)
def _run_git(root: Path, arguments: Sequence[str]) -> subprocess.CompletedProcess[bytes]:
try:
return subprocess.run(
["git", "-c", "core.quotepath=false", *arguments],
cwd=root,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=MAX_GIT_SECONDS,
)
except subprocess.TimeoutExpired as error:
raise AuditError(
f"git command exceeded {MAX_GIT_SECONDS} seconds"
) from error
def _git_stdout(result: subprocess.CompletedProcess[bytes], operation: str) -> bytes:
if result.returncode != 0:
detail = result.stderr.decode("utf-8", errors="replace").strip()
raise AuditError(f"git {operation} failed: {detail or f'exit {result.returncode}'}")
if len(result.stdout) > MAX_GIT_OUTPUT_BYTES:
raise AuditError(
f"git {operation} output exceeds {MAX_GIT_OUTPUT_BYTES} bytes"
)
return result.stdout
def _git_root(cwd: Path) -> Path:
result = _run_git(cwd, ["rev-parse", "--show-toplevel"])
if result.returncode != 0:
raise AuditError("--git-diff requires a Git working tree")
stdout = _git_stdout(result, "rev-parse")
return Path(stdout.decode("utf-8", errors="surrogateescape").strip()).resolve()
def _has_head(root: Path) -> bool:
return _run_git(root, ["rev-parse", "--verify", "HEAD"]).returncode == 0
def _decode_nul_paths(payload: bytes) -> set[str]:
return {
item.decode("utf-8", errors="surrogateescape")
for item in payload.split(b"\0")
if item
}
def _changed_path_names(root: Path, has_head: bool) -> tuple[set[str], set[str]]:
diff_args = ["diff", "--name-only", "-z", "--diff-filter=ACMR"]
if has_head:
result = _run_git(root, [*diff_args, "HEAD", "--", "*.py"])
tracked = _decode_nul_paths(_git_stdout(result, "diff --name-only"))
else:
cached_result = _run_git(root, [*diff_args, "--cached", "--", "*.py"])
unstaged_result = _run_git(root, [*diff_args, "--", "*.py"])
cached = _decode_nul_paths(_git_stdout(cached_result, "diff --cached --name-only"))
unstaged = _decode_nul_paths(_git_stdout(unstaged_result, "diff --name-only"))
tracked = cached | unstaged
untracked_result = _run_git(
root,
["ls-files", "--others", "--exclude-standard", "-z", "--", "*.py"],
)
untracked = _decode_nul_paths(_git_stdout(untracked_result, "ls-files --others"))
return tracked, untracked
def _parse_patch_ranges(payload: bytes, root: Path) -> dict[Path, list[tuple[int, int]]]:
ranges: dict[Path, list[tuple[int, int]]] = defaultdict(list)
current_path: Path | None = None
text = payload.decode("utf-8", errors="replace")
# quality: ignore[POT02] - payload is rejected above MAX_GIT_OUTPUT_BYTES
for line in text.splitlines():
if line.startswith("+++ "):
name = line[4:]
if name == "/dev/null":
current_path = None
else:
current_path = (root / (name[2:] if name.startswith("b/") else name)).resolve()
continue
match = HUNK_RE.match(line)
if current_path is None or match is None:
continue
start = int(match.group(1))
count = int(match.group(2) or "1")
if count > 0:
# quality: ignore[POT03] - one range per hunk in the bounded payload
ranges[current_path].append((start, start + count - 1))
else:
# quality: ignore[POT03] - one range per hunk in the bounded payload
ranges[current_path].append((max(1, start - 1), max(1, start)))
return ranges
def _patch_ranges(root: Path, has_head: bool) -> dict[Path, list[tuple[int, int]]]:
args = ["diff", "--unified=0", "--no-color"]
payloads: list[bytes]
if has_head:
result = _run_git(root, [*args, "HEAD", "--", "*.py"])
payloads = [_git_stdout(result, "diff patch")]
else:
cached_result = _run_git(root, [*args, "--cached", "--", "*.py"])
unstaged_result = _run_git(root, [*args, "--", "*.py"])
payloads = [
_git_stdout(cached_result, "diff --cached patch"),
_git_stdout(unstaged_result, "diff patch"),
]
combined: dict[Path, list[tuple[int, int]]] = defaultdict(list)
# quality: ignore[POT02] - payloads contains at most cached and unstaged output
for payload in payloads:
# quality: ignore[POT02] - each parsed range set is capped by Git output bytes
for path, items in _parse_patch_ranges(payload, root).items():
# quality: ignore[POT03] - combined hunk count is capped by Git output bytes
combined[path].extend(items)
return combined
def collect_git_diff(cwd: Path) -> DiffSelection:
"""Determine which Python files and lines the working tree changed.
Tracked changes are read against ``HEAD`` when it exists, and against the
index otherwise. Untracked files count as changed in their entirety.
Parameters
----------
cwd : Path
Directory inside the Git working tree to inspect.
Returns
-------
DiffSelection
Repository root, changed Python files, and their changed line ranges.
Raises
------
AuditError
If `cwd` is outside a Git working tree, a Git command fails or times
out, or the change set exceeds ``MAX_PYTHON_FILES``.
"""
root = _git_root(cwd)
has_head = _has_head(root)
tracked, untracked = _changed_path_names(root, has_head)
ranges = _patch_ranges(root, has_head)
all_names = sorted(tracked | untracked)
if len(all_names) > MAX_PYTHON_FILES:
raise AuditError(
f"changed Python file count exceeds {MAX_PYTHON_FILES}"
)
paths = tuple((root / name).resolve() for name in all_names if (root / name).is_file())
# quality: ignore[POT02] - all_names is rejected above MAX_PYTHON_FILES
for name in untracked:
path = (root / name).resolve()
if not path.is_file():
continue
ranges[path] = [(1, MAX_SOURCE_BYTES + 1)]
frozen_ranges = {path: tuple(ranges.get(path, ())) for path in paths}
return DiffSelection(root=root, paths=paths, changed_lines=frozen_ranges)
def _register_python_file(candidate: Path, seen: set[Path]) -> Path | None:
resolved = candidate.resolve()
if not resolved.is_file() or resolved in seen:
return None
if len(seen) >= MAX_PYTHON_FILES:
raise AuditError(f"Python file count exceeds {MAX_PYTHON_FILES}")
seen.add(resolved)
return resolved
def _validated_input_path(supplied: Path) -> Path:
path = supplied.resolve()
if not path.exists():
raise AuditError(f"audit path does not exist: {supplied}")
if not path.is_file() and not path.is_dir():
raise AuditError(f"audit path is not a regular file or directory: {supplied}")
if path.is_file() and path.suffix != ".py":
raise AuditError(f"audit path is not Python source: {supplied}")
return path
def _classify_entry(entry: os.DirEntry, seen: set[Path]) -> tuple[Path | None, Path | None]:
candidate = Path(entry.path)
if entry.is_dir(follow_symlinks=False):
return (None, None) if entry.name in EXCLUDED_DIRECTORIES else (candidate, None)
if candidate.suffix == ".py" and entry.is_file():
return None, _register_python_file(candidate, seen)
return None, None
def _consume_entries(
entries: Iterable[os.DirEntry], seen: set[Path], discovered: int
) -> tuple[list[Path], list[Path], int]:
queued: list[Path] = []
sources: list[Path] = []
# quality: ignore[POT02] - every entry is charged to the hard discovery cap
for entry in entries:
discovered += 1
if discovered > MAX_DISCOVERED_PATHS:
raise AuditError(f"discovered path count exceeds {MAX_DISCOVERED_PATHS}")
child, source = _classify_entry(entry, seen)
if child is not None:
# quality: ignore[POT03] - one queued directory per capped entry
queued.append(child)
if source is not None:
# quality: ignore[POT03] - one source file per capped entry
sources.append(source)
return queued, sources, discovered
def _scan_directory(
directory: Path, seen: set[Path], discovered: int
) -> tuple[list[Path], list[Path], int]:
try:
with os.scandir(directory) as entries:
return _consume_entries(entries, seen, discovered)
except OSError as error:
raise AuditError(f"cannot inspect audit directory {directory}: {error}") from error
def _iter_python_files(paths: Iterable[Path]) -> Iterator[Path]:
supplied_paths = tuple(islice(paths, MAX_INPUT_PATHS + 1))
if len(supplied_paths) > MAX_INPUT_PATHS:
raise AuditError(f"audit accepts at most {MAX_INPUT_PATHS} input paths")
seen: set[Path] = set()
directories: list[Path] = []
discovered = 0
# quality: ignore[POT02] - supplied_paths is rejected above MAX_INPUT_PATHS
for supplied in supplied_paths:
path = _validated_input_path(supplied)
if path.is_dir():
# quality: ignore[POT03] - supplied directories are capped at MAX_INPUT_PATHS
directories.append(path)
continue
registered = _register_python_file(path, seen)
if registered is not None:
yield registered
for _ in range(MAX_DIRECTORY_VISITS):
if not directories:
return
queued, sources, discovered = _scan_directory(directories.pop(), seen, discovered)
# quality: ignore[POT03] - queued directories are capped by the discovery cap
directories.extend(queued)
yield from sources
if directories:
raise AuditError(f"directory traversal exceeds {MAX_DIRECTORY_VISITS} visits")
def _node_end(node: ast.AST) -> int:
# quality: ignore[POT08] - ast.AST line metadata is optional by contract
return int(getattr(node, "end_lineno", getattr(node, "lineno", 1)))
def _is_suppressed(lines: SourceLines, line_number: int, code: str) -> bool:
suppressions = lines.suppressions
for candidate in (line_number, line_number - 1):
if code in suppressions.get(candidate, ()):
return True
return False
def _collect_functions(tree: ast.Module) -> list[FunctionInfo]:
functions: list[FunctionInfo] = []
pending: list[tuple[ast.AST, tuple[str, ...], tuple[str, ...]]] = [
(tree, (), ())
]
for _ in range(MAX_AST_NODES):
if not pending:
return functions
node, scope, class_stack = pending.pop()
child_scope = scope
child_class_stack = class_stack
if isinstance(node, ast.ClassDef):
child_scope = (*scope, node.name)
child_class_stack = (*class_stack, node.name)
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
qualified_name = ".".join((*scope, node.name))
class_name = ".".join(class_stack) or None
functions.append(FunctionInfo(qualified_name, class_name, node))
child_scope = (*scope, node.name)
children = tuple(ast.iter_child_nodes(node))
# quality: ignore[POT02] - the parsed tree was rejected above MAX_AST_NODES
for child in reversed(children):
if len(pending) >= MAX_AST_NODES:
raise AuditError(f"AST worklist exceeds {MAX_AST_NODES} nodes")
# quality: ignore[POT03] - pending is checked before every insertion
pending.append((child, child_scope, child_class_stack))
if pending:
raise AuditError(f"AST traversal exceeds {MAX_AST_NODES} nodes")
return functions
def _walk_without_nested_definitions(node: ast.AST) -> Iterator[ast.AST]:
pending: deque[ast.AST] = deque(ast.iter_child_nodes(node))
for _ in range(MAX_AST_NODES):
if not pending:
return
current = pending.popleft()
if isinstance(current, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Lambda)):
continue
yield current
# quality: ignore[POT02] - child count is checked against MAX_AST_NODES
for child in ast.iter_child_nodes(current):
if len(pending) >= MAX_AST_NODES:
raise AuditError(f"AST worklist exceeds {MAX_AST_NODES} nodes")
# quality: ignore[POT03] - pending is checked before every insertion
pending.append(child)
if pending:
raise AuditError(f"AST traversal exceeds {MAX_AST_NODES} nodes")
def _build_parent_map(tree: ast.AST) -> dict[ast.AST, ast.AST]:
parents: dict[ast.AST, ast.AST] = {}
pending: deque[ast.AST] = deque([tree])
for _ in range(MAX_AST_NODES):
if not pending:
return parents
parent = pending.popleft()
# quality: ignore[POT02] - the outer MAX_AST_NODES loop caps all children
for child in ast.iter_child_nodes(parent):
if len(parents) >= MAX_AST_NODES - 1:
raise AuditError(f"AST exceeds {MAX_AST_NODES} nodes")
parents[child] = parent
# quality: ignore[POT03] - parent count is checked before every insertion
pending.append(child)
if pending:
raise AuditError(f"AST exceeds {MAX_AST_NODES} nodes")
return parents
def _resolve_local_call(
call: ast.Call,
function: FunctionInfo,
names: dict[str, list[str]],
) -> str | None:
target = call.func
if isinstance(target, ast.Name) and len(names.get(target.id, ())) == 1:
return names[target.id][0]
if (
isinstance(target, ast.Attribute)
and isinstance(target.value, ast.Name)
and target.value.id in {"self", "cls"}
and function.class_name
):
candidate = f"{function.class_name}.{target.attr}"
return candidate
return None
def _call_graph(functions: Sequence[FunctionInfo]) -> dict[str, set[str]]:
names: dict[str, list[str]] = defaultdict(list)
# quality: ignore[POT02] - functions comes from an AST capped at MAX_AST_NODES
for function in functions:
# quality: ignore[POT03] - one name entry per bounded function
names[function.node.name].append(function.qualified_name)
graph: dict[str, set[str]] = {function.qualified_name: set() for function in functions}
# quality: ignore[POT02] - functions comes from an AST capped at MAX_AST_NODES
for function in functions:
# quality: ignore[POT02] - the helper enforces MAX_AST_NODES per function
for node in _walk_without_nested_definitions(function.node):
target = (
_resolve_local_call(node, function, names)
if isinstance(node, ast.Call)
else None
)
if target in graph:
# quality: ignore[POT03] - graph edges are capped by source AST nodes
graph[function.qualified_name].add(target)
return graph
def _origin_is_in_a_cycle(
graph: dict[str, set[str]], origin: str, total_steps: int
) -> tuple[bool, int]:
pending = deque(graph[origin])
queued = set(pending)
for _ in range(MAX_AST_NODES):
if not pending:
return False, total_steps
total_steps += 1
if total_steps > MAX_CALL_GRAPH_STEPS:
raise AuditError(f"call graph traversal exceeds {MAX_CALL_GRAPH_STEPS} steps")
current = pending.popleft()
if current == origin:
return True, total_steps
# quality: ignore[POT02] - graph edges derive from the bounded source AST
for target in graph.get(current, ()):
if target not in queued:
# quality: ignore[POT03] - queued is capped by graph node count
queued.add(target)
# quality: ignore[POT03] - each graph node is enqueued at most once
pending.append(target)
raise AuditError(f"call graph depth exceeds {MAX_AST_NODES} functions")
def _recursive_functions(functions: Sequence[FunctionInfo]) -> set[str]:
graph = _call_graph(functions)
recursive: set[str] = set()
total_steps = 0
# quality: ignore[POT02] - graph size is capped and total work has a hard ceiling
for origin in graph:
in_cycle, total_steps = _origin_is_in_a_cycle(graph, origin, total_steps)
if in_cycle:
# quality: ignore[POT03] - recursive members cannot exceed graph nodes
recursive.add(origin)
return recursive
def _numeric_constant(node: ast.AST) -> float | None:
current = node
sign = 1.0
for _ in range(MAX_NUMERIC_UNARY_DEPTH):
if not isinstance(current, ast.UnaryOp):
break
if isinstance(current.op, ast.USub):
sign *= -1
elif not isinstance(current.op, ast.UAdd):
return None
current = current.operand
if isinstance(current, ast.UnaryOp):
return None
if (
isinstance(current, ast.Constant)
and isinstance(current.value, (int, float))
and not isinstance(current.value, bool)
):
return sign * float(current.value)
return None
def _is_preset_number(node: ast.AST) -> bool:
return _numeric_constant(node) is not None or (
isinstance(node, ast.Name) and node.id.isupper()
)
def _previous_statement(
node: ast.stmt, parents: dict[ast.AST, ast.AST]
) -> ast.stmt | None:
parent = parents.get(node)
if parent is None:
return None
# quality: ignore[POT02] - ast.iter_fields has a fixed schema per AST node type
for _, value in ast.iter_fields(parent):
if not isinstance(value, list) or node not in value:
continue
index = value.index(node)
return value[index - 1] if index > 0 else None
return None
def _has_preset_initial_value(statement: ast.stmt | None, name: str) -> bool:
if isinstance(statement, ast.Assign) and len(statement.targets) == 1:
target = statement.targets[0]
return (
isinstance(target, ast.Name)
and target.id == name
and _is_preset_number(statement.value)
)
if isinstance(statement, ast.AnnAssign):
target = statement.target
return (
isinstance(target, ast.Name)
and target.id == name
and statement.value is not None
and _is_preset_number(statement.value)
)
return False
def _has_unconditional_counter_update(
node: ast.While, name: str, increasing: bool
) -> bool:
updates = [
statement
for statement in node.body
if isinstance(statement, ast.AugAssign)
and isinstance(statement.target, ast.Name)
and statement.target.id == name
]
if len(updates) != 1:
return False
update = updates[0]
amount = _numeric_constant(update.value)
correct_direction = (
increasing and isinstance(update.op, ast.Add)
) or (
not increasing and isinstance(update.op, ast.Sub)
)
if amount is None or amount <= 0 or not correct_direction:
return False
writes = [
child
for child in _walk_without_nested_definitions(node)
if isinstance(child, ast.Name)
and isinstance(child.ctx, ast.Store)
and child.id == name
]
has_continue = any(
isinstance(child, ast.Continue)
for child in _walk_without_nested_definitions(node)
)
return len(writes) == 1 and writes[0] is update.target and not has_continue
def _while_is_statically_bounded(
node: ast.While, parents: dict[ast.AST, ast.AST]
) -> bool:
test = node.test
if not isinstance(test, ast.Compare) or len(test.ops) != 1 or len(test.comparators) != 1:
return False
if not isinstance(test.left, ast.Name):
return False
bound = test.comparators[0]
if not _is_preset_number(bound):
return False
previous = _previous_statement(node, parents)
if not _has_preset_initial_value(previous, test.left.id):
return False
if isinstance(test.ops[0], (ast.Lt, ast.LtE)):
return _has_unconditional_counter_update(node, test.left.id, increasing=True)
if isinstance(test.ops[0], (ast.Gt, ast.GtE)):
return _has_unconditional_counter_update(node, test.left.id, increasing=False)
return False
def _iterable_bound_kind(node: ast.AST) -> str | None:
if isinstance(node, (ast.List, ast.Set, ast.Tuple)):
return "fixed" if not any(isinstance(item, ast.Starred) for item in node.elts) else None
if isinstance(node, ast.Subscript) and isinstance(node.slice, ast.Slice):
upper = node.slice.upper
capped = upper is not None and _is_preset_number(upper)
return "truncating" if capped else None
if not isinstance(node, ast.Call):
return None
name = _call_name(node.func)
if name == "range" and node.args:
fixed = len(node.args) <= 3 and all(_is_preset_number(item) for item in node.args)
return "fixed" if fixed else None
if name.endswith("islice") and len(node.args) >= 2:
stop_index = 1 if len(node.args) == 2 else 2
bound = node.args[stop_index] if stop_index < len(node.args) else None
capped = bound is not None and _is_preset_number(bound)
return "truncating" if capped else None
return None
def _call_name(node: ast.AST) -> str:
parts: list[str] = []
current = node
for _ in range(MAX_CHAIN_DEPTH):
if not isinstance(current, ast.Attribute):
break
parts.append(current.attr)
current = current.value
if isinstance(current, ast.Name):
parts.append(current.id)
return ".".join(reversed(parts))
def _mutable_default(node: ast.AST) -> bool:
if isinstance(node, (ast.Dict, ast.List, ast.Set)):
return True
return isinstance(node, ast.Call) and _call_name(node.func).split(".")[-1] in MUTABLE_BUILDERS
def _meaningful_check_count(function: FunctionInfo) -> int:
count = 0
# quality: ignore[POT02] - the helper enforces MAX_AST_NODES per function
for node in _walk_without_nested_definitions(function.node):
if isinstance(node, ast.Assert):
count += 1
elif isinstance(node, ast.If) and any(isinstance(item, ast.Raise) for item in node.body):
count += 1
elif isinstance(node, ast.Call):
name = _call_name(node.func).split(".")[-1].lower()
if name.startswith(("check", "ensure", "require", "validate")):
count += 1
return count
def _find_project_config(start: Path) -> Path | None:
current = start.resolve()
for _ in range(MAX_CONFIG_SEARCH_DEPTH):
candidate = current / CONFIG_FILENAME
if candidate.is_file():
return candidate
if current.parent == current:
return None
current = current.parent
return None
def _read_config_table(path: Path) -> dict[str, object]:
try:
with path.open("rb") as handle:
payload = handle.read(MAX_CONFIG_BYTES + 1)
except OSError as error:
raise AuditError(f"cannot read {path}: {error}") from error
if len(payload) > MAX_CONFIG_BYTES:
raise AuditError(f"{path} exceeds {MAX_CONFIG_BYTES} bytes")
try:
document = tomllib.loads(payload.decode("utf-8"))
except (UnicodeDecodeError, tomllib.TOMLDecodeError) as error:
raise AuditError(f"cannot parse {path}: {error}") from error
tools = document.get("tool")
section = tools.get(CONFIG_TABLE) if isinstance(tools, dict) else None
return section if isinstance(section, dict) else {}
def _validated_style(value: object, origin: str) -> str:
if not isinstance(value, str) or value.lower() not in DOCSTRING_STYLES:
supported = ", ".join(DOCSTRING_STYLES)
raise AuditError(
f"unknown docstring style {value!r} in {origin}; expected one of {supported}"
)
return value.lower()
def _configured_style(start: Path) -> tuple[str, str]:
config_path = _find_project_config(start)
if config_path is None:
return DEFAULT_DOCSTRING_STYLE, "built-in default"
if tomllib is None:
return (
DEFAULT_DOCSTRING_STYLE,
f"built-in default; reading {CONFIG_FILENAME} requires Python 3.11 or newer",
)
value = _read_config_table(config_path).get(CONFIG_KEY)
if value is None:
return DEFAULT_DOCSTRING_STYLE, "built-in default"
origin = f"{config_path} [tool.{CONFIG_TABLE}] {CONFIG_KEY}"
return _validated_style(value, origin), str(config_path)
def resolve_docstring_style(
requested: str | None, environment: Mapping[str, str], start: Path
) -> tuple[str, str]:
"""Resolve the documentation convention from all configuration sources.
Precedence is the command-line flag, then the environment variable, then
``[tool.reliable-python] docstring-style`` in the nearest `pyproject.toml`
at or above `start`, then the built-in default.
Parameters
----------
requested : str or None
Value supplied on the command line, or ``None`` when the flag was
omitted.
environment : mapping of str to str
Process environment consulted for `DOCSTYLE_ENV_VAR`.
start : Path
Directory the `pyproject.toml` search begins from, walking upward.
Returns
-------
tuple of (str, str)
The resolved style and a human-readable description of where it came
from, suitable for reporting to a user who is debugging configuration.
Raises
------
AuditError
If any source supplies a style outside `DOCSTRING_STYLES`, or the
configuration file cannot be read or parsed.
"""
if requested is not None:
return _validated_style(requested, "--docstring-style"), "--docstring-style"
from_environment = environment.get(DOCSTYLE_ENV_VAR)
if from_environment is not None:
return _validated_style(from_environment, DOCSTYLE_ENV_VAR), DOCSTYLE_ENV_VAR
return _configured_style(start)
def _indent_width(line: str) -> int:
return len(line) - len(line.lstrip())