quality-graph-python 0.1.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.
- qg_python/__init__.py +1 -0
- qg_python/diff.py +106 -0
- qg_python/flaky.py +67 -0
- qg_python/no_comments.py +72 -0
- qg_python/object_annotations.py +80 -0
- qg_python/py.typed +1 -0
- qg_python/report.py +27 -0
- qg_python/suppressions.py +54 -0
- qg_python/time_bombs.py +61 -0
- qg_python/triple_quotes.py +154 -0
- quality_graph_python-0.1.0.dist-info/METADATA +12 -0
- quality_graph_python-0.1.0.dist-info/RECORD +14 -0
- quality_graph_python-0.1.0.dist-info/WHEEL +4 -0
- quality_graph_python-0.1.0.dist-info/entry_points.txt +7 -0
qg_python/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Reusable Python quality gates for Quality Graph."""
|
qg_python/diff.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Read changed repository lines without depending on a hosting provider."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
import re
|
|
7
|
+
import shutil
|
|
8
|
+
import tokenize
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import anyio
|
|
13
|
+
|
|
14
|
+
BASE_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_./-]*$")
|
|
15
|
+
HUNK_RE = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@")
|
|
16
|
+
GIT = shutil.which("git")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class ChangedFile:
|
|
21
|
+
"""Carry current source and added destination lines for one changed file."""
|
|
22
|
+
|
|
23
|
+
path: str
|
|
24
|
+
source: str
|
|
25
|
+
added_lines: frozenset[int]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def validated_base(value: str) -> str:
|
|
29
|
+
"""Return a ref safe to pass as one argv item to Git."""
|
|
30
|
+
if BASE_RE.fullmatch(value) is None:
|
|
31
|
+
message = f"invalid base ref: {value}"
|
|
32
|
+
raise ValueError(message)
|
|
33
|
+
return value
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def patch_for_base(base: str) -> str:
|
|
37
|
+
"""Return a zero-context patch from the merge base to HEAD."""
|
|
38
|
+
if GIT is None:
|
|
39
|
+
message = "git executable is required"
|
|
40
|
+
raise RuntimeError(message)
|
|
41
|
+
completed = anyio.run(
|
|
42
|
+
anyio.run_process,
|
|
43
|
+
[
|
|
44
|
+
GIT,
|
|
45
|
+
"diff",
|
|
46
|
+
"--unified=0",
|
|
47
|
+
"--no-ext-diff",
|
|
48
|
+
"--diff-filter=ACMR",
|
|
49
|
+
f"{validated_base(base)}...HEAD",
|
|
50
|
+
"--",
|
|
51
|
+
],
|
|
52
|
+
)
|
|
53
|
+
return completed.stdout.decode()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def added_lines_by_path(patch: str) -> dict[str, frozenset[int]]:
|
|
57
|
+
"""Parse added destination line numbers from a unified Git patch."""
|
|
58
|
+
path: str | None = None
|
|
59
|
+
line_number: int | None = None
|
|
60
|
+
result: dict[str, set[int]] = {}
|
|
61
|
+
for line in patch.splitlines():
|
|
62
|
+
if line.startswith("diff --git "):
|
|
63
|
+
path = None
|
|
64
|
+
line_number = None
|
|
65
|
+
continue
|
|
66
|
+
if line.startswith("+++ "):
|
|
67
|
+
path = _patch_path(line[4:])
|
|
68
|
+
if path is not None:
|
|
69
|
+
result.setdefault(path, set())
|
|
70
|
+
continue
|
|
71
|
+
match = HUNK_RE.match(line)
|
|
72
|
+
if match is not None:
|
|
73
|
+
line_number = int(match.group(1))
|
|
74
|
+
continue
|
|
75
|
+
if path is None or line_number is None or line.startswith("\\ No newline"):
|
|
76
|
+
continue
|
|
77
|
+
if line.startswith("+"):
|
|
78
|
+
result[path].add(line_number)
|
|
79
|
+
line_number += 1
|
|
80
|
+
elif not line.startswith("-"):
|
|
81
|
+
line_number += 1
|
|
82
|
+
return {path: frozenset(lines) for path, lines in result.items() if lines}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _patch_path(value: str) -> str | None:
|
|
86
|
+
decoded = value
|
|
87
|
+
if value.startswith('"'):
|
|
88
|
+
literal = str(ast.literal_eval(value))
|
|
89
|
+
decoded = literal.encode("latin1").decode("utf-8")
|
|
90
|
+
return decoded[2:] if decoded.startswith("b/") else None
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def changed_files(base: str, suffixes: tuple[str, ...]) -> tuple[ChangedFile, ...]:
|
|
94
|
+
"""Load changed existing files matching the selected suffixes."""
|
|
95
|
+
result = []
|
|
96
|
+
for path, lines in added_lines_by_path(patch_for_base(base)).items():
|
|
97
|
+
source_path = Path(path)
|
|
98
|
+
if source_path.suffix.lower() not in suffixes or not source_path.is_file():
|
|
99
|
+
continue
|
|
100
|
+
if source_path.suffix.lower() == ".py":
|
|
101
|
+
with tokenize.open(source_path) as source:
|
|
102
|
+
text = source.read()
|
|
103
|
+
else:
|
|
104
|
+
text = source_path.read_text(encoding="utf-8")
|
|
105
|
+
result.append(ChangedFile(path, text, lines))
|
|
106
|
+
return tuple(sorted(result, key=lambda item: item.path))
|
qg_python/flaky.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Repeat changed Python tests to expose order-independent flakiness."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Protocol
|
|
9
|
+
|
|
10
|
+
import anyio
|
|
11
|
+
|
|
12
|
+
from qg_python.diff import added_lines_by_path, patch_for_base
|
|
13
|
+
|
|
14
|
+
MIN_ATTEMPTS = 2
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ProcessResult(Protocol):
|
|
18
|
+
"""Expose the subprocess state needed by the flaky gate."""
|
|
19
|
+
|
|
20
|
+
returncode: int
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async def run_test(command: list[str]) -> ProcessResult:
|
|
24
|
+
"""Run one pytest command without raising for a test failure."""
|
|
25
|
+
return await anyio.run_process(command, check=False)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def changed_test_files(base: str) -> tuple[str, ...]:
|
|
29
|
+
"""Return changed existing Python test files in deterministic order."""
|
|
30
|
+
return tuple(
|
|
31
|
+
sorted(
|
|
32
|
+
path
|
|
33
|
+
for path in added_lines_by_path(patch_for_base(base))
|
|
34
|
+
if Path(path).is_file()
|
|
35
|
+
and Path(path).suffix == ".py"
|
|
36
|
+
and (Path(path).name.startswith("test_") or "/tests/" in f"/{path}")
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def repeat(files: tuple[str, ...], attempts: int) -> int:
|
|
42
|
+
"""Run changed test files repeatedly with retries disabled."""
|
|
43
|
+
results = []
|
|
44
|
+
for attempt in range(1, attempts + 1):
|
|
45
|
+
completed = anyio.run(
|
|
46
|
+
run_test,
|
|
47
|
+
[sys.executable, "-m", "pytest", "-q", *files],
|
|
48
|
+
)
|
|
49
|
+
results.append(completed.returncode)
|
|
50
|
+
if completed.returncode != 0:
|
|
51
|
+
sys.stderr.write(f"changed tests failed on repeat {attempt}/{attempts}\n")
|
|
52
|
+
if len(set(results)) > 1:
|
|
53
|
+
sys.stderr.write("changed tests are flaky across repeated runs\n")
|
|
54
|
+
return 1
|
|
55
|
+
return results[0]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def main(arguments: list[str] | None = None) -> int:
|
|
59
|
+
"""Repeat changed Python test files selected from a Git diff."""
|
|
60
|
+
parser = argparse.ArgumentParser()
|
|
61
|
+
parser.add_argument("--base", default="origin/main")
|
|
62
|
+
parser.add_argument("--attempts", type=int, default=3)
|
|
63
|
+
args = parser.parse_args(arguments)
|
|
64
|
+
if args.attempts < MIN_ATTEMPTS:
|
|
65
|
+
parser.error("--attempts must be at least 2")
|
|
66
|
+
files = changed_test_files(args.base)
|
|
67
|
+
return 0 if not files else repeat(files, args.attempts)
|
qg_python/no_comments.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Reject Python comments outside narrowly allowed tool directives."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import re
|
|
7
|
+
import tokenize
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from qg_python.report import Finding, report
|
|
11
|
+
|
|
12
|
+
ALLOWED_COMMENT = re.compile(
|
|
13
|
+
r"^#\s*(?:noqa|nosec|type:\s*(?:ignore|noqa)|pragma:\s*no mutate)\b",
|
|
14
|
+
re.IGNORECASE,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def python_files(roots: tuple[Path, ...]) -> tuple[Path, ...]:
|
|
19
|
+
"""Return sorted Python files below selected roots."""
|
|
20
|
+
return tuple(
|
|
21
|
+
sorted(
|
|
22
|
+
path
|
|
23
|
+
for root in roots
|
|
24
|
+
for path in root.rglob("*.py")
|
|
25
|
+
if not any(part in {".venv", "__pycache__", "mutants"} for part in path.parts)
|
|
26
|
+
)
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def scan_file(path: Path) -> tuple[Finding, ...]:
|
|
31
|
+
"""Return disallowed comment tokens in one Python file."""
|
|
32
|
+
findings = []
|
|
33
|
+
try:
|
|
34
|
+
with tokenize.open(path) as source:
|
|
35
|
+
for token in tokenize.generate_tokens(source.readline):
|
|
36
|
+
if token.type != tokenize.COMMENT:
|
|
37
|
+
continue
|
|
38
|
+
text = token.string
|
|
39
|
+
if text.startswith("#!") or ALLOWED_COMMENT.match(text):
|
|
40
|
+
continue
|
|
41
|
+
findings.append(
|
|
42
|
+
Finding(
|
|
43
|
+
path.as_posix(),
|
|
44
|
+
token.start[0],
|
|
45
|
+
token.start[1] + 1,
|
|
46
|
+
f"code comment is not allowed: {text}",
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
except (IndentationError, SyntaxError, tokenize.TokenError) as error:
|
|
50
|
+
location = (
|
|
51
|
+
error.args[1] if len(error.args) > 1 and isinstance(error.args[1], tuple) else (1, 1)
|
|
52
|
+
)
|
|
53
|
+
findings.append(
|
|
54
|
+
Finding(
|
|
55
|
+
path.as_posix(),
|
|
56
|
+
int(location[0]),
|
|
57
|
+
int(location[1]),
|
|
58
|
+
f"cannot tokenize Python: {error}",
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
return tuple(findings)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def main(arguments: list[str] | None = None) -> int:
|
|
65
|
+
"""Scan configured roots and return a process exit code."""
|
|
66
|
+
parser = argparse.ArgumentParser()
|
|
67
|
+
parser.add_argument("roots", type=Path, nargs="+")
|
|
68
|
+
args = parser.parse_args(arguments)
|
|
69
|
+
findings = tuple(
|
|
70
|
+
finding for path in python_files(tuple(args.roots)) for finding in scan_file(path)
|
|
71
|
+
)
|
|
72
|
+
return report(findings)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Reject overly broad object annotations on changed Python lines."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import ast
|
|
7
|
+
|
|
8
|
+
from qg_python.diff import changed_files
|
|
9
|
+
from qg_python.report import Finding, report
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def annotation_nodes(tree: ast.AST) -> tuple[ast.expr, ...]:
|
|
13
|
+
"""Collect annotations that can contain object references."""
|
|
14
|
+
nodes: list[ast.expr] = []
|
|
15
|
+
for node in ast.walk(tree):
|
|
16
|
+
if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef):
|
|
17
|
+
arguments = [*node.args.posonlyargs, *node.args.args, *node.args.kwonlyargs]
|
|
18
|
+
arguments.extend(
|
|
19
|
+
argument for argument in (node.args.vararg, node.args.kwarg) if argument is not None
|
|
20
|
+
)
|
|
21
|
+
nodes.extend(argument.annotation for argument in arguments if argument.annotation)
|
|
22
|
+
if node.returns is not None:
|
|
23
|
+
nodes.append(node.returns)
|
|
24
|
+
elif isinstance(node, ast.AnnAssign):
|
|
25
|
+
nodes.append(node.annotation)
|
|
26
|
+
return tuple(nodes)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def object_location(annotation: ast.expr) -> tuple[int, int] | None:
|
|
30
|
+
"""Return the first direct, qualified, or forward object annotation."""
|
|
31
|
+
for node in ast.walk(annotation):
|
|
32
|
+
if isinstance(node, ast.Name) and node.id == "object":
|
|
33
|
+
return node.lineno, node.col_offset
|
|
34
|
+
if isinstance(node, ast.Attribute) and node.attr == "object":
|
|
35
|
+
return node.lineno, node.col_offset
|
|
36
|
+
if isinstance(node, ast.Constant) and isinstance(node.value, str):
|
|
37
|
+
try:
|
|
38
|
+
parsed = ast.parse(node.value, mode="eval")
|
|
39
|
+
except SyntaxError:
|
|
40
|
+
continue
|
|
41
|
+
if object_location(parsed.body) is not None:
|
|
42
|
+
return node.lineno, node.col_offset
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def scan_source(path: str, source: str, changed: frozenset[int]) -> tuple[Finding, ...]:
|
|
47
|
+
"""Find changed annotations using object in one Python source."""
|
|
48
|
+
try:
|
|
49
|
+
tree = ast.parse(source, filename=path)
|
|
50
|
+
except SyntaxError as error:
|
|
51
|
+
return (
|
|
52
|
+
Finding(path, error.lineno or 1, error.offset or 1, f"cannot parse Python: {error}"),
|
|
53
|
+
)
|
|
54
|
+
findings = []
|
|
55
|
+
for annotation in annotation_nodes(tree):
|
|
56
|
+
location = object_location(annotation)
|
|
57
|
+
if location is None or location[0] not in changed:
|
|
58
|
+
continue
|
|
59
|
+
findings.append(
|
|
60
|
+
Finding(
|
|
61
|
+
path,
|
|
62
|
+
location[0],
|
|
63
|
+
location[1] + 1,
|
|
64
|
+
f"annotation must be more specific than object: {ast.unparse(annotation)}",
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
return tuple(sorted(set(findings)))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def main(arguments: list[str] | None = None) -> int:
|
|
71
|
+
"""Check changed Python annotations against a Git base."""
|
|
72
|
+
parser = argparse.ArgumentParser()
|
|
73
|
+
parser.add_argument("--base", default="origin/main")
|
|
74
|
+
args = parser.parse_args(arguments)
|
|
75
|
+
findings = tuple(
|
|
76
|
+
finding
|
|
77
|
+
for changed in changed_files(args.base, (".py",))
|
|
78
|
+
for finding in scan_source(changed.path, changed.source, changed.added_lines)
|
|
79
|
+
)
|
|
80
|
+
return report(findings)
|
qg_python/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
qg_python/report.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Render deterministic source diagnostics for local and CI gates."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True, order=True)
|
|
10
|
+
class Finding:
|
|
11
|
+
"""Describe one source-located quality violation."""
|
|
12
|
+
|
|
13
|
+
path: str
|
|
14
|
+
line: int
|
|
15
|
+
column: int
|
|
16
|
+
message: str
|
|
17
|
+
|
|
18
|
+
def diagnostic(self) -> str:
|
|
19
|
+
"""Render the shared compiler-style diagnostic format."""
|
|
20
|
+
return f"{self.path}:{self.line}:{self.column}: error: {self.message}"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def report(findings: tuple[Finding, ...]) -> int:
|
|
24
|
+
"""Print sorted findings and return the gate exit code."""
|
|
25
|
+
if findings:
|
|
26
|
+
sys.stdout.write("\n".join(finding.diagnostic() for finding in sorted(findings)) + "\n")
|
|
27
|
+
return int(bool(findings))
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Reject newly introduced lint, typing, security, and coverage suppressions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from qg_python.diff import changed_files
|
|
9
|
+
from qg_python.report import Finding, report
|
|
10
|
+
|
|
11
|
+
SOURCE_RE = re.compile(
|
|
12
|
+
r"#\s*(?:noqa|nosec|type:\s*ignore|pyright:\s*ignore|pylint:\s*(?:disable|skip-file)"
|
|
13
|
+
r"|pragma:\s*no cover)",
|
|
14
|
+
re.IGNORECASE,
|
|
15
|
+
)
|
|
16
|
+
CONFIG_RE = re.compile(
|
|
17
|
+
r"(?:per-file-ignores|extend-per-file-ignores|extend-ignore|ignore|disable|exclude)\s*[=:]",
|
|
18
|
+
re.IGNORECASE,
|
|
19
|
+
)
|
|
20
|
+
SUFFIXES = (".py", ".toml", ".yaml", ".yml", ".json")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def scan_source(path: str, source: str, added: frozenset[int]) -> tuple[Finding, ...]:
|
|
24
|
+
"""Find suppression directives on added lines."""
|
|
25
|
+
pattern = SOURCE_RE if path.endswith(".py") else CONFIG_RE
|
|
26
|
+
findings = []
|
|
27
|
+
for line_number, line in enumerate(source.splitlines(), 1):
|
|
28
|
+
if line_number not in added:
|
|
29
|
+
continue
|
|
30
|
+
match = pattern.search(line)
|
|
31
|
+
if match is None:
|
|
32
|
+
continue
|
|
33
|
+
findings.append(
|
|
34
|
+
Finding(
|
|
35
|
+
path,
|
|
36
|
+
line_number,
|
|
37
|
+
match.start() + 1,
|
|
38
|
+
f"new suppression is not allowed: {match.group().strip()}",
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
return tuple(findings)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def main(arguments: list[str] | None = None) -> int:
|
|
45
|
+
"""Check changed files for newly introduced suppressions."""
|
|
46
|
+
parser = argparse.ArgumentParser()
|
|
47
|
+
parser.add_argument("--base", default="origin/main")
|
|
48
|
+
args = parser.parse_args(arguments)
|
|
49
|
+
findings = tuple(
|
|
50
|
+
finding
|
|
51
|
+
for changed in changed_files(args.base, SUFFIXES)
|
|
52
|
+
for finding in scan_source(changed.path, changed.source, changed.added_lines)
|
|
53
|
+
)
|
|
54
|
+
return report(findings)
|
qg_python/time_bombs.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Detect plausible Unix timestamp literals introduced on changed source lines."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from qg_python.diff import changed_files
|
|
9
|
+
from qg_python.report import Finding, report
|
|
10
|
+
|
|
11
|
+
NUMBER_RE = re.compile(r"(?<![\w.])\d(?:_?\d){8,18}(?![\w.])")
|
|
12
|
+
TIMESTAMP_SCALES = (
|
|
13
|
+
("nanoseconds", 10**9),
|
|
14
|
+
("microseconds", 10**6),
|
|
15
|
+
("milliseconds", 10**3),
|
|
16
|
+
("seconds", 1),
|
|
17
|
+
)
|
|
18
|
+
EARLIEST_SECONDS = 9_466_848 * 100
|
|
19
|
+
LATEST_SECONDS = 72_581_184 * 100
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def timestamp_unit(literal: str) -> str | None:
|
|
23
|
+
"""Classify a plausible timestamp literal by unit."""
|
|
24
|
+
value = int(literal.replace("_", ""))
|
|
25
|
+
for unit, scale in TIMESTAMP_SCALES:
|
|
26
|
+
if EARLIEST_SECONDS * scale <= value <= LATEST_SECONDS * scale:
|
|
27
|
+
return unit
|
|
28
|
+
return None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def scan_source(path: str, source: str, added: frozenset[int]) -> tuple[Finding, ...]:
|
|
32
|
+
"""Find plausible timestamps on added lines."""
|
|
33
|
+
findings = []
|
|
34
|
+
for line_number, line in enumerate(source.splitlines(), 1):
|
|
35
|
+
if line_number not in added:
|
|
36
|
+
continue
|
|
37
|
+
for match in NUMBER_RE.finditer(line):
|
|
38
|
+
unit = timestamp_unit(match.group())
|
|
39
|
+
if unit is not None:
|
|
40
|
+
findings.append(
|
|
41
|
+
Finding(
|
|
42
|
+
path,
|
|
43
|
+
line_number,
|
|
44
|
+
match.start() + 1,
|
|
45
|
+
f"possible Unix timestamp in {unit}: {match.group()}",
|
|
46
|
+
)
|
|
47
|
+
)
|
|
48
|
+
return tuple(findings)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def main(arguments: list[str] | None = None) -> int:
|
|
52
|
+
"""Check changed Python and shell source for timestamp literals."""
|
|
53
|
+
parser = argparse.ArgumentParser()
|
|
54
|
+
parser.add_argument("--base", default="origin/main")
|
|
55
|
+
args = parser.parse_args(arguments)
|
|
56
|
+
findings = tuple(
|
|
57
|
+
finding
|
|
58
|
+
for changed in changed_files(args.base, (".py", ".sh"))
|
|
59
|
+
for finding in scan_source(changed.path, changed.source, changed.added_lines)
|
|
60
|
+
)
|
|
61
|
+
return report(findings)
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""Enforce readable delimiters for changed triple-quoted Python strings."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import ast
|
|
7
|
+
import io
|
|
8
|
+
import re
|
|
9
|
+
import tokenize
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
from qg_python.diff import changed_files
|
|
13
|
+
from qg_python.report import Finding, report
|
|
14
|
+
|
|
15
|
+
STRING_RE = re.compile(r"^(?:r|u|b|f|br|rb|fr|rf)?(?P<delimiter>'''|\"\"\")", re.IGNORECASE)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class StringSpan:
|
|
20
|
+
"""Describe one tokenized triple-quoted string span."""
|
|
21
|
+
|
|
22
|
+
start_line: int
|
|
23
|
+
start_column: int
|
|
24
|
+
opening_end: int
|
|
25
|
+
end_line: int
|
|
26
|
+
closing_start: int
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def scan_source(path: str, source: str, added: frozenset[int]) -> tuple[Finding, ...]:
|
|
30
|
+
"""Find changed triple-quote delimiters with inline content."""
|
|
31
|
+
lines = source.splitlines()
|
|
32
|
+
findings: list[Finding] = []
|
|
33
|
+
fstrings: list[tuple[tokenize.TokenInfo, str | None]] = []
|
|
34
|
+
docstrings = _docstring_positions(source)
|
|
35
|
+
try:
|
|
36
|
+
tokens = tokenize.generate_tokens(io.StringIO(source).readline)
|
|
37
|
+
for token in tokens:
|
|
38
|
+
if token.type == tokenize.FSTRING_START:
|
|
39
|
+
match = STRING_RE.match(token.string)
|
|
40
|
+
fstrings.append((token, match.group("delimiter") if match is not None else None))
|
|
41
|
+
continue
|
|
42
|
+
if token.type == tokenize.FSTRING_END:
|
|
43
|
+
start, delimiter = fstrings.pop()
|
|
44
|
+
if delimiter is not None:
|
|
45
|
+
findings.extend(
|
|
46
|
+
_span_findings(
|
|
47
|
+
path,
|
|
48
|
+
lines,
|
|
49
|
+
added,
|
|
50
|
+
StringSpan(
|
|
51
|
+
start.start[0],
|
|
52
|
+
start.end[1] - len(delimiter),
|
|
53
|
+
start.end[1],
|
|
54
|
+
token.end[0],
|
|
55
|
+
token.end[1] - len(delimiter),
|
|
56
|
+
),
|
|
57
|
+
is_docstring=False,
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
continue
|
|
61
|
+
if token.type != tokenize.STRING:
|
|
62
|
+
continue
|
|
63
|
+
match = STRING_RE.match(token.string)
|
|
64
|
+
if match is None:
|
|
65
|
+
continue
|
|
66
|
+
delimiter = match.group("delimiter")
|
|
67
|
+
findings.extend(
|
|
68
|
+
_span_findings(
|
|
69
|
+
path,
|
|
70
|
+
lines,
|
|
71
|
+
added,
|
|
72
|
+
StringSpan(
|
|
73
|
+
token.start[0],
|
|
74
|
+
token.start[1] + match.start("delimiter"),
|
|
75
|
+
token.start[1] + match.end(),
|
|
76
|
+
token.end[0],
|
|
77
|
+
token.end[1] - len(delimiter),
|
|
78
|
+
),
|
|
79
|
+
is_docstring=(token.start[0], token.start[1]) in docstrings,
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
except (IndentationError, tokenize.TokenError) as error:
|
|
83
|
+
findings.append(Finding(path, 1, 1, f"cannot tokenize Python: {error}"))
|
|
84
|
+
return tuple(findings)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _span_findings(
|
|
88
|
+
path: str,
|
|
89
|
+
lines: list[str],
|
|
90
|
+
added: frozenset[int],
|
|
91
|
+
span: StringSpan,
|
|
92
|
+
*,
|
|
93
|
+
is_docstring: bool,
|
|
94
|
+
) -> tuple[Finding, ...]:
|
|
95
|
+
if is_docstring:
|
|
96
|
+
return ()
|
|
97
|
+
if span.start_line == span.end_line:
|
|
98
|
+
return (
|
|
99
|
+
(
|
|
100
|
+
Finding(
|
|
101
|
+
path,
|
|
102
|
+
span.start_line,
|
|
103
|
+
span.start_column + 1,
|
|
104
|
+
"one-line string must use quotes",
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
if span.start_line in added
|
|
108
|
+
else ()
|
|
109
|
+
)
|
|
110
|
+
findings = []
|
|
111
|
+
if span.start_line in added and lines[span.start_line - 1][span.opening_end :].strip():
|
|
112
|
+
findings.append(
|
|
113
|
+
Finding(path, span.start_line, span.start_column + 1, "triple quote must open alone")
|
|
114
|
+
)
|
|
115
|
+
if span.end_line in added and lines[span.end_line - 1][: span.closing_start].strip():
|
|
116
|
+
findings.append(
|
|
117
|
+
Finding(path, span.end_line, span.closing_start + 1, "triple quote must close alone")
|
|
118
|
+
)
|
|
119
|
+
return tuple(findings)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _docstring_positions(source: str) -> frozenset[tuple[int, int]]:
|
|
123
|
+
try:
|
|
124
|
+
tree = ast.parse(source)
|
|
125
|
+
except SyntaxError:
|
|
126
|
+
return frozenset()
|
|
127
|
+
positions = []
|
|
128
|
+
for node in ast.walk(tree):
|
|
129
|
+
if not isinstance(node, ast.Module | ast.ClassDef | ast.FunctionDef | ast.AsyncFunctionDef):
|
|
130
|
+
continue
|
|
131
|
+
body = getattr(node, "body", None)
|
|
132
|
+
if not isinstance(body, list) or not body:
|
|
133
|
+
continue
|
|
134
|
+
first = body[0]
|
|
135
|
+
if (
|
|
136
|
+
isinstance(first, ast.Expr)
|
|
137
|
+
and isinstance(first.value, ast.Constant)
|
|
138
|
+
and isinstance(first.value.value, str)
|
|
139
|
+
):
|
|
140
|
+
positions.append((first.value.lineno, first.value.col_offset))
|
|
141
|
+
return frozenset(positions)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def main(arguments: list[str] | None = None) -> int:
|
|
145
|
+
"""Check changed Python triple-quoted strings."""
|
|
146
|
+
parser = argparse.ArgumentParser()
|
|
147
|
+
parser.add_argument("--base", default="origin/main")
|
|
148
|
+
args = parser.parse_args(arguments)
|
|
149
|
+
findings = tuple(
|
|
150
|
+
finding
|
|
151
|
+
for changed in changed_files(args.base, (".py",))
|
|
152
|
+
for finding in scan_source(changed.path, changed.source, changed.added_lines)
|
|
153
|
+
)
|
|
154
|
+
return report(findings)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: quality-graph-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Reusable Python quality gates for Quality Graph
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: anyio>=4.11.0
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# Quality Graph Python Gates
|
|
11
|
+
|
|
12
|
+
Provider-independent source and diff quality gates for Python repositories.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
qg_python/__init__.py,sha256=Iu124eR7Rnbwe4288dacUDh9QVBgQRJ8nIgZgj_y48I,55
|
|
2
|
+
qg_python/diff.py,sha256=vcS5gXms_yfYUW0AuOVeovXencs0rWWSFlpKZlQhtQk,3371
|
|
3
|
+
qg_python/flaky.py,sha256=onX7fvUtLmgqVbHEMb2gIK-9X89kbZPtcjYOcZI525I,2150
|
|
4
|
+
qg_python/no_comments.py,sha256=sFl60AGeHZvpKdNohb0kkwlzvyZvCaPVjWeb-VPpGN4,2318
|
|
5
|
+
qg_python/object_annotations.py,sha256=nhgI0KJGkZtTSMzQneNdk2pesJjIoh27Uk023QezuFw,3078
|
|
6
|
+
qg_python/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
7
|
+
qg_python/report.py,sha256=t5zIYuCxy-WPgtNHXETKDLHTkzqkEw8jUFiGaYOwSEs,763
|
|
8
|
+
qg_python/suppressions.py,sha256=i9HL8-OsU_cS1mTZTw19Y90uttmWWwLkVa1MEEeJf3E,1753
|
|
9
|
+
qg_python/time_bombs.py,sha256=rwiACRITy4w8cRnD1W6lY7w0plExF8hXJCA-EXZs-Gg,2011
|
|
10
|
+
qg_python/triple_quotes.py,sha256=aLWGlIsOjXNeLxDsRjJz3ifedY8tamGot9ywsXZa4Pg,5281
|
|
11
|
+
quality_graph_python-0.1.0.dist-info/METADATA,sha256=lEw6izsl8orlFPbyemhFUkmxo3N8ZzVX7Nwh2nZWe6A,345
|
|
12
|
+
quality_graph_python-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
13
|
+
quality_graph_python-0.1.0.dist-info/entry_points.txt,sha256=1n7ymDB-MgzrAF8Rw0hnB2VClnA8mfLjT-BMAAboW-s,330
|
|
14
|
+
quality_graph_python-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
[console_scripts]
|
|
2
|
+
qg-python-flaky = qg_python.flaky:main
|
|
3
|
+
qg-python-no-comments = qg_python.no_comments:main
|
|
4
|
+
qg-python-object-annotations = qg_python.object_annotations:main
|
|
5
|
+
qg-python-suppressions = qg_python.suppressions:main
|
|
6
|
+
qg-python-time-bombs = qg_python.time_bombs:main
|
|
7
|
+
qg-python-triple-quotes = qg_python.triple_quotes:main
|