Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: prevent new mocks and constructor-bypassing test subclasses (#43572)

Asuka Minato020b02c95451
diff --git a/.github/workflows/main-ci.yml b/.github/workflows/main-ci.yml
index d0014f95cef..3eb116a037d 100644
--- a/.github/workflows/main-ci.yml
+++ b/.github/workflows/main-ci.yml
@@ -64,10 +64,13 @@ jobs:
- 'scripts/ast_grep_guard.py'
- 'scripts/check_no_new_getattr.py'
- 'scripts/check_no_spec_mock.py'
+ - 'scripts/check_no_new_test_doubles.py'
- 'scripts/check_no_new_controller_sqlalchemy.py'
- 'scripts/lint_controller_sqlalchemy.py'
- 'scripts/ast_grep_rules/no_new_getattr.yml'
- 'scripts/ast_grep_rules/no_spec_mock.yml'
+ - 'scripts/ast_grep_rules/no_new_mock.yml'
+ - 'scripts/ast_grep_rules/no_new_stub_subclass.yml'
- 'scripts/ast_grep_rules/no_new_controller_sqlalchemy.yml'
- 'scripts/ast_grep_rules/no_new_session_mock.yml'
- '.github/workflows/style.yml'
@@ -164,11 +167,14 @@ jobs:
- 'scripts/ast_grep_guard.py'
- 'scripts/check_no_new_getattr.py'
- 'scripts/check_no_spec_mock.py'
+ - 'scripts/check_no_new_test_doubles.py'
- 'scripts/check_no_new_controller_sqlalchemy.py'
- 'scripts/check_no_new_session_mock.py'
- 'scripts/lint_controller_sqlalchemy.py'
- 'scripts/ast_grep_rules/no_new_getattr.yml'
- 'scripts/ast_grep_rules/no_spec_mock.yml'
+ - 'scripts/ast_grep_rules/no_new_mock.yml'
+ - 'scripts/ast_grep_rules/no_new_stub_subclass.yml'
- 'scripts/ast_grep_rules/no_new_controller_sqlalchemy.yml'
- 'scripts/ast_grep_rules/no_new_session_mock.yml'
- '.github/workflows/style.yml'
diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml
index 17ba4c33d76..ba29b192da3 100644
--- a/.github/workflows/style.yml
+++ b/.github/workflows/style.yml
@@ -97,6 +97,14 @@ jobs:
if: inputs.run-python-style
run: uv run --project api python scripts/check_no_spec_mock.py
+ - name: Run No New Test Doubles Guard
+ if: inputs.run-python-style || inputs.run-dify-agent-style
+ # A PR event can retain an older base SHA after its target branch advances.
+ # Checkout tests the merge commit. Its first parent is the tested target revision.
+ env:
+ GUARD_BASE_REV: ${{ github.event_name == 'pull_request' && 'HEAD^1' || inputs.base-rev }}
+ run: uv run --project api python scripts/check_no_new_test_doubles.py --base-rev "$GUARD_BASE_REV"
+
- name: Run Type Checks
if: inputs.run-python-style
env:
diff --git a/api/tests/unit_tests/commands/test_check_no_new_test_doubles.py b/api/tests/unit_tests/commands/test_check_no_new_test_doubles.py
new file mode 100644
index 00000000000..526b3b1b38c
--- /dev/null
+++ b/api/tests/unit_tests/commands/test_check_no_new_test_doubles.py
@@ -0,0 +1,329 @@
+"""Run real Git diffs and ast-grep rules for the incremental test-double guard."""
+
+from __future__ import annotations
+
+import re
+import subprocess
+import sys
+import textwrap
+from pathlib import Path
+
+import pytest
+
+REPO_ROOT = Path(__file__).resolve().parents[4]
+SCRIPT = REPO_ROOT / "scripts" / "check_no_new_test_doubles.py"
+TEST_PATH = "api/tests/unit_tests/test_example.py"
+
+
+def git(repo: Path, *args: str) -> str:
+ return subprocess.run(["git", *args], cwd=repo, capture_output=True, text=True, check=True).stdout.strip()
+
+
+def write_source(repo: Path, source: str, path: str = TEST_PATH) -> None:
+ target = repo / path
+ target.parent.mkdir(parents=True, exist_ok=True)
+ target.write_text(textwrap.dedent(source).lstrip(), encoding="utf-8")
+
+
+def commit(repo: Path) -> str:
+ git(repo, "add", ".")
+ git(repo, "commit", "-m", "test fixture")
+ return git(repo, "rev-parse", "HEAD")
+
+
+def repo(tmp_path: Path) -> Path:
+ git(tmp_path, "init", "-b", "main")
+ git(tmp_path, "config", "user.name", "Guard Tests")
+ git(tmp_path, "config", "user.email", "[email protected]")
+ write_source(tmp_path, "value = 1\n")
+ commit(tmp_path)
+ return tmp_path
+
+
+def run_guard(repo: Path, *args: str) -> subprocess.CompletedProcess[str]:
+ return subprocess.run([sys.executable, str(SCRIPT), *args], cwd=repo, capture_output=True, text=True, check=False)
+
+
+def check_source(repo: Path, source: str) -> subprocess.CompletedProcess[str]:
+ write_source(repo, source)
+ git(repo, "add", TEST_PATH)
+ return run_guard(repo, "--staged")
+
+
+ "source",
+ [
+ "value = Mock()",
+ "value = MagicMock(return_value=1)",
+ "value = SimpleNamespace(id='id')",
+ "value = unittest.mock.Mock()",
+ "value = mock.MagicMock()",
+ "value = types.SimpleNamespace()",
+ "from unittest.mock import Mock as Factory\nvalue = Factory()",
+ "from unittest.mock import (MagicMock as Factory, patch)\nvalue = Factory()",
+ "from types import SimpleNamespace as Record\nvalue = Record(id=1)",
+ "from unittest import mock as doubles\nvalue = doubles.Mock()",
+ "import types as values\nvalue = values.SimpleNamespace()",
+ "factory = Mock\nvalue = factory()",
+ "factory = mock.MagicMock\nvalue = factory()",
+ "factory = types.SimpleNamespace\nvalue = factory()",
+ "value = Mock(\n name='value',\n)",
+ "def fixture():\n return [Mock() for _ in range(2)]",
+ ],
+)
+def test_rejects_new_mock_calls(repo: Path, source: str) -> None:
+ result = check_source(repo, source)
+ assert result.returncode == 1, result.stderr
+ assert f"{TEST_PATH}:" in result.stderr
+ assert "no-new-mock" in result.stderr
+
+
+ "source",
+ [
+ "# Mock()\nvalue = 'MagicMock() SimpleNamespace()'",
+ "value = RealModel(id='id')",
+ "value = AsyncMock()", # Not included in this three-constructor rule.
+ "from other_library import Mock as Builder\nvalue = Builder()",
+ "from other_library import SimpleNamespace as Builder\nvalue = Builder()",
+ "def Mock():\n return 1", # A declaration is not a constructor call.
+ ],
+)
+def test_ignores_strings_comments_and_unrelated_constructors(repo: Path, source: str) -> None:
+ result = check_source(repo, source)
+ assert result.returncode == 0, result.stderr
+
+
+ "base",
+ [
+ "ModelInstance",
+ "ModelManager",
+ "DatasetRetrieval",
+ "TraceQueueManager",
+ "MessageBasedAppQueueManager",
+ "threading.Thread",
+ ],
+)
[email protected]("kind", ["explicit", "dataclass"])
+def test_rejects_runtime_subclasses_without_initialization(repo: Path, base: str, kind: str) -> None:
+ source = (
+ f"class Recorder({base}):\n def __init__(self):\n self.calls = []\n"
+ if kind == "explicit"
+ else f"@dataclass\nclass Recorder({base}):\n value: str\n"
+ )
+ result = check_source(repo, source)
+ assert result.returncode == 1, result.stderr
+ assert "no-new-stub-subclass: Recorder" in result.stderr
+
+
+ "source",
+ [
+ "from core.model_manager import ModelInstance as Base\nclass Recorder(Base):\n def __init__(self): pass",
+ "class Recorder(Mixin, ModelInstance):\n def __init__(self): pass",
+ "@dataclasses.dataclass(frozen=True)\nclass Recorder(ModelInstance):\n value: str",
+ "class Recorder(ModelInstance):\n def __init__(self): Other.__init__(self)",
+ "class Recorder(ModelInstance):\n def __init__(self): pass\n def run(self): super().__init__()",
+ "class Recorder(ModelInstance):\n def __init__(self):\n def unused(): super().__init__()",
+ ],
+)
+def test_rejects_subclass_aliases_and_unrelated_initializer_calls(repo: Path, source: str) -> None:
+ result = check_source(repo, source)
+ assert result.returncode == 1, result.stderr
+ assert "no-new-stub-subclass" in result.stderr
+
+
+ "source",
+ [
+ "class Recorder(ModelInstance):\n def __init__(self): super().__init__(config)",
+ "class Recorder(ModelInstance):\n def __init__(self): ModelInstance.__init__(self, config)",
+ "class Recorder(ModelInstance):\n def invoke_llm(self): raise ValueError('injected failure')",
+ "@dataclass(init=False)\nclass Recorder(ModelInstance):\n value: str",
+ "@dataclass\nclass Recorder(ModelInstance):\n def __init__(self): super().__init__()",
+ "@dataclass\nclass Recorder(ModelInstance):\n def __post_init__(self): super().__init__()",
+ "@dataclass\nclass Value:\n value: str",
+ "class Port(Protocol):\n def __init__(self): pass",
+ "class Reply(BaseModel):\n value: str",
+ "class Options(TypedDict):\n value: str",
+ "class TestExample(BaseTest):\n def __init__(self): pass",
+ "@dataclass\nclass Outer:\n class Inner(ModelInstance): pass",
+ "class Outer(ModelInstance):\n class Inner:\n def __init__(self): pass",
+ ],
+)
+def test_allows_real_initialization_and_non_runtime_classes(repo: Path, source: str) -> None:
+ result = check_source(repo, source)
+ assert result.returncode == 0, result.stderr
+
+
[email protected]("change", ["remove_super", "add_decorator"])
+def test_detects_subclass_changes_outside_the_class_header(repo: Path, change: str) -> None:
+ before = "class Recorder(ModelInstance):\n def __init__(self):\n super().__init__()\n"
+ after = before.replace("super().__init__()", "self.calls = []")
+ if change == "add_decorator":
+ before = "class Recorder(ModelInstance):\n value: str\n"
+ after = "@dataclass\n" + before
+ write_source(repo, before)
+ baseline = commit(repo)
+ write_source(repo, after)
+ commit(repo)
+ result = run_guard(repo, "--base-rev", baseline)
+ assert result.returncode == 1, result.stderr
+ assert "no-new-stub-subclass" in result.stderr
+
+
+def test_detects_new_alias_binding_even_when_call_line_is_unchanged(repo: Path) -> None:
+ source = "from domain import RealModel as Factory\n\nvalue = Factory()\n"
+ write_source(repo, source)
+ baseline = commit(repo)
+ write_source(repo, source.replace("from domain import RealModel", "from unittest.mock import Mock"))
+ commit(repo)
+ result = run_guard(repo, "--base-rev", baseline)
+ assert result.returncode == 1, result.stderr
+ assert "no-new-mock" in result.stderr
+
+
+def test_keeps_unchanged_baseline_but_rejects_an_additional_identical_call(repo: Path) -> None:
+ source = "value = Mock()\nclass Legacy(ModelInstance):\n def __init__(self): pass\n"
+ write_source(repo, source)
+ baseline = commit(repo)
+ write_source(repo, source + "answer = 42\n")
+ commit(repo)
+ result = run_guard(repo, "--base-rev", baseline)
+ assert result.returncode == 0, result.stderr
+ write_source(repo, source + "another = Mock()\n")
+ commit(repo)
+ result = run_guard(repo, "--base-rev", baseline)
+ assert result.returncode == 1, result.stderr
+ assert "no-new-mock" in result.stderr
+
+
+def test_replacing_one_mock_with_another_is_not_grandfathered(repo: Path) -> None:
+ write_source(repo, "value = Mock()\n")
+ baseline = commit(repo)
+ write_source(repo, "value = MagicMock()\n")
+ commit(repo)
+ result = run_guard(repo, "--base-rev", baseline)
+ assert result.returncode == 1, result.stderr
+
+
[email protected](("reason", "expected"), [(" -- abstract adapter contract", 0), ("", 1)])
+def test_subclass_exception_requires_a_reason(repo: Path, reason: str, expected: int) -> None:
+ source = (
+ f"class Recorder(ModelInstance): # guard-ignore: no-new-stub-subclass{reason}\n def __init__(self): pass\n"
+ )
+ result = check_source(repo, source)
+ assert result.returncode == expected, result.stderr
+
+
+def test_mock_rule_applies_outside_tests_but_runtime_subclass_rule_does_not(repo: Path) -> None:
+ write_source(repo, "class Specialized(ModelInstance):\n def __init__(self): pass\n", "api/services/example.py")
+ git(repo, "add", ".")
+ result = run_guard(repo, "--staged")
+ assert result.returncode == 0, result.stderr
+ write_source(repo, "value = SimpleNamespace()\n", "api/services/example.py")
+ git(repo, "add", ".")
+ result = run_guard(repo, "--staged")
+ assert result.returncode == 1, result.stderr
+ assert "api/services/example.py:1:" in result.stderr
+
+
+def test_staged_mode_does_not_read_unstaged_content(repo: Path) -> None:
+ write_source(repo, "value = RealModel()\n")
+ git(repo, "add", TEST_PATH)
+ write_source(repo, "value = Mock()\n")
+ result = run_guard(repo, "--staged")
+ assert result.returncode == 0, result.stderr
+
+
[email protected]("mode", ["staged", "base"])
[email protected]("kind", ["mock", "subclass"])
[email protected]("add_violation", [False, True])
[email protected]("filename", ["test_renamed.py", "test renamed.py"])
+def test_renamed_files_keep_baseline_but_reject_new_violations(
+ repo: Path, mode: str, kind: str, add_violation: bool, filename: str
+) -> None:
+ legacy = "value = Mock()\n" if kind == "mock" else "class Legacy(ModelInstance):\n def __init__(self): pass\n"
+ added = "another = MagicMock()\n" if kind == "mock" else "class New(ModelInstance):\n def __init__(self): pass\n"
+ source = "".join(f"# unchanged context {index}\n" for index in range(40)) + legacy
+ write_source(repo, source)
+ baseline = commit(repo)
+ destination = f"api/tests/unit_tests/{filename}"
+ git(repo, "mv", TEST_PATH, destination)
+ write_source(repo, source + (added if add_violation else ""), destination)
+ git(repo, "add", ".")
+ assert git(repo, "diff", "--cached", "--name-status", "--find-renames").startswith("R")
+ if mode == "base":
+ commit(repo)
+ result = run_guard(repo, "--base-rev", baseline)
+ else:
+ result = run_guard(repo, "--staged")
+ assert result.returncode == int(add_violation), result.stderr
+ if add_violation:
+ assert destination in result.stderr
+
+
+def test_invalid_revision_fails_closed(repo: Path) -> None:
+ result = run_guard(repo, "--base-rev", "missing-revision")
+ assert result.returncode == 2
+ assert result.stderr
+
+
+def test_requires_an_explicit_diff_source(repo: Path) -> None:
+ assert run_guard(repo).returncode == 2
+ assert run_guard(repo, "--staged", "--base-rev", "HEAD").returncode == 2
+
+
[email protected]("kind", ["mock", "subclass"])
[email protected]("add_violation", [False, True])
+def test_pr_merge_uses_the_tested_target_revision(repo: Path, kind: str, add_violation: bool) -> None:
+ stale_base = git(repo, "rev-parse", "HEAD")
+ violation = (
+ "value = Mock()\n" if kind == "mock" else "class Recorder(ModelInstance):\n def __init__(self): pass\n"
+ )
+ git(repo, "checkout", "-b", "feature")
+ pr_path = "api/tests/unit_tests/test_pr.py"
+ write_source(repo, violation if add_violation else "value = RealModel()\n", pr_path)
+ commit(repo)
+ git(repo, "checkout", "main")
+ write_source(repo, violation)
+ target_revision = commit(repo)
+ git(repo, "merge", "--no-ff", "feature", "-m", "PR merge")
+
+ # The stale event base incorrectly reports changes already in the target branch.
+ stale_result = run_guard(repo, "--base-rev", stale_base)
+ assert stale_result.returncode == 1, stale_result.stderr
+ assert TEST_PATH in stale_result.stderr
+ assert git(repo, "rev-parse", "HEAD^1") == target_revision
+
+ result = run_guard(repo, "--base-rev", "HEAD^1")
+ assert result.returncode == int(add_violation), result.stderr
+ assert TEST_PATH not in result.stderr
+ if add_violation:
+ assert pr_path in result.stderr
+
+
+def test_ci_runs_the_guard_and_classifies_its_implementation_changes() -> None:
+ style = (REPO_ROOT / ".github/workflows/style.yml").read_text(encoding="utf-8")
+ assert (
+ " - name: Run No New Test Doubles Guard\n"
+ " if: inputs.run-python-style || inputs.run-dify-agent-style\n"
+ " # A PR event can retain an older base SHA after its target branch advances.\n"
+ " # Checkout tests the merge commit. Its first parent is the tested target revision.\n"
+ " env:\n"
+ " GUARD_BASE_REV: ${{ github.event_name == 'pull_request' && 'HEAD^1' || inputs.base-rev }}\n"
+ " run: uv run --project api python scripts/check_no_new_test_doubles.py "
+ '--base-rev "$GUARD_BASE_REV"' in style
+ )
+ workflow = (REPO_ROOT / ".github/workflows/main-ci.yml").read_text(encoding="utf-8")
+ for name in ("api", "python-style"):
+ path_filter = re.search(rf"(?m)^ {name}:\n((?: - '[^']+'\n)+)", workflow)
+ assert path_filter is not None
+ for path in (
+ "scripts/check_no_new_test_doubles.py",
+ "scripts/ast_grep_rules/no_new_mock.yml",
+ "scripts/ast_grep_rules/no_new_stub_subclass.yml",
+ ):
+ assert path in path_filter.group(1)
diff --git a/scripts/ast_grep_rules/no_new_mock.yml b/scripts/ast_grep_rules/no_new_mock.yml
new file mode 100644
index 00000000000..1b77d10acfe
--- /dev/null
+++ b/scripts/ast_grep_rules/no_new_mock.yml
@@ -0,0 +1,57 @@
+id: no-new-mock
+language: python
+severity: error
+message: Use a real instance instead of adding a Mock, MagicMock, or SimpleNamespace constructor.
+rule:
+ all:
+ - pattern: $CALLEE($$$ARGS)
+ - any:
+ - has:
+ field: function
+ stopBy: end
+ regex: '(^|\.)(Mock|MagicMock|SimpleNamespace)$'
+ - inside:
+ kind: module
+ stopBy: end
+ has:
+ stopBy: end
+ all:
+ - pattern:
+ context: from package import $ORIGINAL as $CALLEE
+ selector: aliased_import
+ - any:
+ - all:
+ - has:
+ field: name
+ stopBy: end
+ regex: '^(Mock|MagicMock)$'
+ - inside:
+ kind: import_from_statement
+ stopBy: end
+ has:
+ field: module_name
+ stopBy: end
+ regex: '^unittest\.mock$'
+ - all:
+ - has:
+ field: name
+ stopBy: end
+ regex: '^SimpleNamespace$'
+ - inside:
+ kind: import_from_statement
+ stopBy: end
+ has:
+ field: module_name
+ stopBy: end
+ regex: '^types$'
+ - inside:
+ kind: module
+ stopBy: end
+ has:
+ stopBy: end
+ all:
+ - pattern: $CALLEE = $ORIGINAL
+ - has:
+ field: right
+ stopBy: end
+ regex: '(^|\.)(Mock|MagicMock|SimpleNamespace)$'
diff --git a/scripts/ast_grep_rules/no_new_stub_subclass.yml b/scripts/ast_grep_rules/no_new_stub_subclass.yml
new file mode 100644
index 00000000000..a1216f365cc
--- /dev/null
+++ b/scripts/ast_grep_rules/no_new_stub_subclass.yml
@@ -0,0 +1,98 @@
+id: no-new-stub-subclass
+language: python
+severity: error
+message: Instantiate the production runtime class; do not bypass its constructor in a test subclass.
+utils:
+ runtime-base:
+ any:
+ - regex: '(^|\.)(ModelInstance|ModelManager|DatasetRetrieval|TraceQueueManager|MessageBasedAppQueueManager|Thread)$'
+ - all:
+ - pattern: $BASE
+ - inside:
+ kind: module
+ stopBy: end
+ has:
+ stopBy: end
+ all:
+ - pattern:
+ context: from package import $ORIGINAL as $BASE
+ selector: aliased_import
+ - has:
+ field: name
+ stopBy: end
+ regex: '^(ModelInstance|ModelManager|DatasetRetrieval|TraceQueueManager|MessageBasedAppQueueManager|Thread)$'
+ initializer:
+ kind: function_definition
+ has:
+ field: name
+ stopBy: end
+ regex: ^__init__$
+ delegates:
+ has:
+ # Nested definitions do not initialize the containing object.
+ stopBy:
+ any:
+ - kind: function_definition
+ - kind: class_definition
+ - kind: lambda
+ any:
+ - pattern: super($$$ARGS).__init__($$$PARAMS)
+ - pattern: $BASE.__init__(self, $$$PARAMS)
+ dataclass-decorator:
+ kind: decorator
+ regex: '^@(dataclasses\.)?dataclass(\(|$)'
+ not:
+ has:
+ stopBy: end
+ pattern:
+ context: dataclass(init=False)
+ selector: keyword_argument
+rule:
+ all:
+ - kind: class_definition
+ - has:
+ field: name
+ stopBy: neighbor
+ pattern: $NAME
+ - has:
+ field: superclasses
+ stopBy: neighbor
+ has:
+ stopBy: neighbor
+ all:
+ - pattern: $BASE
+ - matches: runtime-base
+ - any:
+ - has:
+ field: body
+ stopBy: neighbor
+ has:
+ # Only methods of this class, not constructors of nested classes.
+ stopBy: neighbor
+ all:
+ - matches: initializer
+ - not:
+ matches: delegates
+ - all:
+ - inside:
+ # A nested class must not inherit its outer class's decorator.
+ stopBy: neighbor
+ kind: decorated_definition
+ has:
+ stopBy: neighbor
+ matches: dataclass-decorator
+ - not:
+ has:
+ field: body
+ stopBy: neighbor
+ has:
+ stopBy: neighbor
+ any:
+ - matches: initializer
+ - all:
+ - kind: function_definition
+ - has:
+ field: name
+ stopBy: end
+ regex: ^__post_init__$
+ - matches: delegates
diff --git a/scripts/check_no_new_test_doubles.py b/scripts/check_no_new_test_doubles.py
new file mode 100644
index 00000000000..da7ed8f27cf
--- /dev/null
+++ b/scripts/check_no_new_test_doubles.py
@@ -0,0 +1,134 @@
+"""Reject net-new mock constructors and constructor-bypassing runtime subclasses.
+
+Mock, MagicMock and SimpleNamespace calls are checked in all changed Python
+sources, including common qualified names, import aliases and direct factory
+aliases. AsyncMock and method patching are outside this rule's scope.
+
+The subclass rule covers the audited ModelInstance, ModelManager,
+DatasetRetrieval, TraceQueueManager, MessageBasedAppQueueManager and Thread
+base names (including qualified names and import aliases) in api/tests/. It
+checks explicit __init__ overrides and dataclass-generated initializers. This
+is a syntactic guard, not general Python inheritance or execution analysis.
+Protocol and Pydantic model implementations are not targeted.
+
+Both rules retain the provided Git baseline. Identical mock calls are counted
+per file: an additional copy or a different constructor call is rejected.
+Git-detected renames retain the source file's baseline, including paths with
+spaces. Subclass matches are compared by class name within each changed file,
+not only by the line of the class header:
+removing a super call or adding a dataclass decorator must still be detected.
+An existing offending class can be edited or removed without adding another.
+For a justified subclass exception only, add
+`# guard-ignore: no-new-stub-subclass -- <reason>` on the class header.
+"""
+
+from __future__ import annotations
+
+import argparse
+import sys
+from collections import Counter
+from pathlib import Path
+
+from ast_grep_guard import (
+ Match,
+ Violation,
+ git_output,
+ has_reasoned_guard_ignore,
+ is_python_source_path,
+ parse_args,
+ print_violations,
+ rule_path,
+ run_ast_grep,
+)
+
+STUB_RULE_ID = "no-new-stub-subclass"
+
+
+def changed_file_versions(args: argparse.Namespace) -> dict[str, tuple[str, str]]:
+ """Read Git blobs, preserving rename baselines and NUL-delimited filenames."""
+ revisions = ["--cached"] if args.staged else [f"{args.base_rev}..HEAD"]
+ status_text = git_output(
+ "diff", "--name-status", "-z", "--find-renames", "--diff-filter=AMR", "--no-ext-diff", *revisions
+ )
+ if not status_text:
+ return {}
+ fields = iter(status_text.rstrip("\0").split("\0"))
+ old_revision = "HEAD" if args.staged else args.base_rev
+ new_revision = "" if args.staged else "HEAD"
+ versions: dict[str, tuple[str, str]] = {}
+ for status in fields:
+ try:
+ old_path = next(fields)
+ path = next(fields) if status.startswith("R") else old_path
+ except StopIteration as exc:
+ raise RuntimeError("Incomplete Git name-status output") from exc
+ if not is_python_source_path(path):
+ continue
+ old_source = "" if status == "A" else git_output("show", f"{old_revision}:{old_path}")
+ versions[path] = (old_source, git_output("show", f"{new_revision}:{path}"))
+ return versions
+
+
+def find_new_mock_calls(changed: dict[str, tuple[str, str]]) -> list[Violation]:
+ """Keep identical baseline calls, but catch alias changes outside their hunks."""
+ violations: list[Violation] = []
+ for path, (old_source, new_source) in changed.items():
+ old_calls = Counter(match.text for match in run_ast_grep(old_source, rule=rule_path("no_new_mock.yml")))
+ for match in run_ast_grep(new_source, rule=rule_path("no_new_mock.yml")):
+ if old_calls[match.text]:
+ old_calls[match.text] -= 1
+ continue
+ violations.append(
+ Violation(
+ path=path,
+ line_number=match.line_number,
+ message="no-new-mock: instantiate a real class instead of Mock, MagicMock or SimpleNamespace",
+ )
+ )
+ return violations
+
+
+def is_reportable_subclass(match: Match) -> bool:
+ return not has_reasoned_guard_ignore(match.source_line, STUB_RULE_ID)
+
+
+def find_new_stub_subclasses(changed: dict[str, tuple[str, str]]) -> list[Violation]:
+ violations: list[Violation] = []
+ for path, (old_source, new_source) in changed.items():
+ if not Path(path).is_relative_to("api/tests"):
+ continue
+ old_matches = run_ast_grep(old_source, rule=rule_path("no_new_stub_subclass.yml"))
+ new_matches = run_ast_grep(new_source, rule=rule_path("no_new_stub_subclass.yml"))
+ old_names = Counter(match.meta_variables["NAME"] for match in old_matches if is_reportable_subclass(match))
+ for match in new_matches:
+ if not is_reportable_subclass(match):
+ continue
+ name = match.meta_variables["NAME"]
+ if old_names[name]:
+ old_names[name] -= 1
+ continue
+ violations.append(
+ Violation(
+ path=path,
+ line_number=match.line_number,
+ message=f"{STUB_RULE_ID}: {name} bypasses a runtime constructor; instantiate the real class",
+ )
+ )
+ return violations
+
+
+def main() -> int:
+ try:
+ args = parse_args(__doc__)
+ changed = changed_file_versions(args)
+ violations = find_new_mock_calls(changed)
+ violations.extend(find_new_stub_subclasses(changed))
+ except (OSError, RuntimeError) as exc:
+ print(str(exc), file=sys.stderr)
+ return 2
+ print_violations(sorted(violations, key=lambda item: (item.path, item.line_number)))
+ return 1 if violations else 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())