From 1dd3d5ab8d1c8c75ea0c1c4a5d7c63e869b06b70 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Sat, 28 Nov 2026 04:35:58 -0500 Subject: [PATCH] Add silent-fallback, BOUND-5 embedded-code, and cloned-family detectors --- .../detectors/_silent_fallbacks.py | 547 ++++++++++++++++++ nominal_refactor_advisor/patterns.py | 43 ++ tests/test_silent_fallbacks.py | 184 ++++++ 3 files changed, 774 insertions(+) create mode 100644 nominal_refactor_advisor/detectors/_silent_fallbacks.py create mode 100644 tests/test_silent_fallbacks.py diff --git a/nominal_refactor_advisor/detectors/_silent_fallbacks.py b/nominal_refactor_advisor/detectors/_silent_fallbacks.py new file mode 100644 index 00000000..cee79e3d --- /dev/null +++ b/nominal_refactor_advisor/detectors/_silent_fallbacks.py @@ -0,0 +1,547 @@ +"""Detectors for silent input/exception swallowers, embedded foreign code literals, +and cross-module cloned `DeclaredFamily` roots. + +Every detector here flags a concrete, high-precision structural defect that lets +missing inputs, programming bugs, inline non-Python applications, or parallel +registries pass static checks silently: + +- `EmbeddedCodeLiteralDetector` (`BOUND-5`): non-docstring multiline string literals + embedding substantial HTML/CSS/JS, JavaScript, shell, or SQL DDL source code. +- `SilentMissingPathFallbackDetector`: positive `.is_file()` / `.exists()` / `.is_dir()` + guards that drop entries from a declared path collection or silently substitute an + empty container or `Default*` / `Fallback*` family member. +- `SilentExceptionFallbackDetector` (`BOUND-7` EAFP bypass + broad silent swallow): + `except AttributeError` (with optional `TypeError`/`IndexError`/`KeyError`/`ValueError`) + or broad `except Exception`/`BaseException`/bare `except` whose handler body silently + swallows the error into a constant, empty container, `None`, `pass`, or `continue`. +- `ClonedDeclaredFamilyDetector` (`MEMB-1` / `TIME-1`): two or more unrelated modules + declaring distinct `DeclaredFamily` root classes with the same name. +""" + +from __future__ import annotations + +import ast +from collections import defaultdict +from dataclasses import dataclass + +from nominal_refactor_advisor.ast_tools import ParsedModule +from nominal_refactor_advisor.class_index import CompactIndexedClass +from nominal_refactor_advisor.detectors._base import ( + CompactClassRepositoryCandidateDetector, + CompactClassRepositoryContext, + DetectorConfig, + PerModuleIssueDetector, + finding_spec_template, +) +from nominal_refactor_advisor.models import RefactorFinding, SourceLocation +from nominal_refactor_advisor.patterns import PatternId +from nominal_refactor_advisor.taxonomy import CapabilityTag, ObservationTag + + +@dataclass(frozen=True) +class ForeignCodeLanguage: + """One non-Python language recognized inside large Python string literals.""" + + label: str + markers: tuple[str, ...] + strong_markers: tuple[str, ...] = () + + def matches(self, lowered: str) -> bool: + if any(marker in lowered for marker in self.strong_markers): + return True + return sum(1 for marker in self.markers if marker in lowered) >= 2 + + +_FOREIGN_CODE_LANGUAGES: tuple[ForeignCodeLanguage, ...] = ( + ForeignCodeLanguage( + label="HTML/CSS/JS", + markers=( + " frozenset[int]: + ids: set[int] = set() + for node in ast.walk(tree): + if ( + isinstance( + node, + (ast.Module, ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef), + ) + and node.body + ): + first = node.body[0] + if ( + isinstance(first, ast.Expr) + and isinstance(first.value, ast.Constant) + and isinstance(first.value.value, str) + ): + ids.add(id(first.value)) + return frozenset(ids) + + +def _string_literal_text(node: ast.AST) -> str | None: + if isinstance(node, ast.Constant) and isinstance(node.value, str): + return node.value + if isinstance(node, ast.JoinedStr): + parts = [ + part.value + for part in node.values + if isinstance(part, ast.Constant) and isinstance(part.value, str) + ] + return "".join(parts) if parts else None + return None + + +def _embedded_code_language(text: str) -> tuple[str, int, int] | None: + line_count = text.count("\n") + 1 + char_count = len(text) + if line_count < _EMBEDDED_CODE_MIN_LINES and char_count < _EMBEDDED_CODE_MIN_CHARS: + return None + lowered = text.lower() + for language in _FOREIGN_CODE_LANGUAGES: + if language.matches(lowered): + return language.label, line_count, char_count + return None + + +def _positive_existence_call(node: ast.AST) -> str | None: + """Return a positive (non-negated) `.is_file()` / `.exists()` / `.is_dir()` call + inside a boolean guard expression, ignoring negated checks (`if not p.exists()`).""" + + if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not): + return None + if ( + isinstance(node, ast.Call) + and isinstance(node.func, ast.Attribute) + and node.func.attr in _EXISTENCE_METHODS + and not node.args + and not node.keywords + ): + return ast.unparse(node) + if isinstance(node, ast.BoolOp): + for value in node.values: + hit = _positive_existence_call(value) + if hit is not None: + return hit + return None + + +def _is_filesystem_discovery_iterable(iterable: ast.expr) -> bool: + for node in ast.walk(iterable): + if isinstance(node, ast.Attribute) and node.attr in _FILESYSTEM_DISCOVERY_SOURCES: + return True + if isinstance(node, ast.Name) and node.id in {"os", "glob"}: + return True + return False + + +def _is_empty_or_sentinel_fallback(expr: ast.expr) -> bool: + if isinstance(expr, ast.Constant) and expr.value in (None, "", False, 0): + return True + if isinstance(expr, (ast.Tuple, ast.List, ast.Set)): + return len(expr.elts) == 0 + if isinstance(expr, ast.Dict): + return len(expr.keys) == 0 + if ( + isinstance(expr, ast.Call) + and isinstance(expr.func, ast.Name) + and expr.func.id in _EMPTY_CONTAINER_CALLS + and not expr.args + and not expr.keywords + ): + return True + return False + + +def _default_fallback_constructor_name(expr: ast.expr) -> str | None: + """Match `DefaultFoo(...)` or `DefaultFoo(...).method(...)`.""" + + current: ast.expr = expr + while isinstance(current, ast.Call): + func = current.func + if isinstance(func, ast.Name) and func.id.startswith( + _DEFAULT_CONSTRUCTOR_PREFIXES + ): + return func.id + if isinstance(func, ast.Attribute): + if func.attr.startswith(_DEFAULT_CONSTRUCTOR_PREFIXES): + return func.attr + current = func.value + continue + break + return None + + +def _caught_exception_names(handler: ast.ExceptHandler) -> tuple[str, ...]: + if handler.type is None: + return ("bare except",) + nodes = ( + handler.type.elts if isinstance(handler.type, ast.Tuple) else (handler.type,) + ) + names: list[str] = [] + for node in nodes: + if isinstance(node, ast.Name): + names.append(node.id) + elif isinstance(node, ast.Attribute): + names.append(node.attr) + return tuple(names) + + +def _is_silent_fallback_statement( + statement: ast.stmt, caught_name: str | None +) -> bool: + if isinstance(statement, (ast.Pass, ast.Continue)): + return True + if isinstance(statement, ast.Return): + value = statement.value + if value is None: + return True + if isinstance(value, ast.Name): + return value.id != caught_name + return isinstance(value, ast.Constant) or _is_empty_or_sentinel_fallback(value) + if isinstance(statement, ast.Assign): + value = statement.value + if isinstance(value, ast.Name): + return value.id != caught_name + return isinstance(value, ast.Constant) or _is_empty_or_sentinel_fallback(value) + return False + + +def _silent_exception_description(handler: ast.ExceptHandler) -> str | None: + if len(handler.body) != 1: + return None + if not _is_silent_fallback_statement(handler.body[0], handler.name): + return None + names = _caught_exception_names(handler) + if not names: + return None + rendered_action = ast.unparse(handler.body[0]) + name_set = set(names) + if "bare except" in name_set or bool(name_set & _BROAD_EXCEPTION_NAMES): + return f"broad `except ({', '.join(names)}): {rendered_action}`" + if "AttributeError" in name_set and name_set <= _EAFP_ATTRIBUTE_COMPANIONS: + return f"EAFP `except ({', '.join(names)}): {rendered_action}`" + return None + + +class EmbeddedCodeLiteralDetector(PerModuleIssueDetector): + """Detect substantial foreign code (HTML/CSS/JS, JS, shell, SQL) embedded in strings.""" + + finding_spec = finding_spec_template( + PatternId.EMBEDDED_CODE_LITERAL, + "Embedded foreign code literal belongs in an owned source file (BOUND-5)", + "Non-Python code inside a large Python string literal is invisible to linters, syntax checkers, and static analysis, and duplicates Python domain labels/contracts by hand.", + "a checked asset file (`.html`, `.mjs`, `.sh`, `.sql`) loaded from package data", + "non-docstring multiline string literal with >= 20 lines or >= 1500 characters containing foreign code markers", + (CapabilityTag.PROVENANCE,), + (ObservationTag.SEMANTIC_STRING_LITERAL,), + ) + + def _findings_for_module( + self, module: ParsedModule, config: DetectorConfig + ) -> list[RefactorFinding]: + del config + index = module.syntax_index + docstrings = _docstring_node_ids(module.module) + joined_child_ids = { + id(part) + for _node_index, joined in index.indexed_nodes_of_type(ast.JoinedStr) + for part in joined.values + } + findings: list[RefactorFinding] = [] + for node_index, node in sorted( + ( + *index.indexed_nodes_of_type(ast.Constant), + *index.indexed_nodes_of_type(ast.JoinedStr), + ) + ): + if id(node) in docstrings or id(node) in joined_child_ids: + continue + text = _string_literal_text(node) + if text is None: + continue + detected = _embedded_code_language(text) + if detected is None: + continue + language, line_count, char_count = detected + symbol = index.lexical_symbol(node_index) + findings.append( + self.build_finding( + f"`{symbol}` embeds {line_count} lines ({char_count:,} chars) of " + f"{language} code in a Python string literal (BOUND-5); move it to " + "an owned source asset.", + (SourceLocation(module.file_path, node.lineno, symbol),), + ) + ) + return findings + + +class SilentMissingPathFallbackDetector(PerModuleIssueDetector): + """Detect existence guards that silently drop missing paths or substitute defaults.""" + + finding_spec = finding_spec_template( + PatternId.SILENT_MISSING_PATH_FALLBACK, + "Path existence guard silently drops missing inputs or falls back to a default", + "Filtering a declared path list by `.is_file()`/`.exists()`/`.is_dir()`, or returning an empty container / `Default*` fallback when a path is absent, hides missing artifacts and typos behind exit code 0.", + "fail-loud path validation at the boundary, raising `FileNotFoundError` when a declared input is absent", + "comprehension filtering a non-discovery iterable by a positive existence check, or ternary/`or` falling back to empty/`Default*`", + (CapabilityTag.FAIL_LOUD_CONTRACTS,), + (ObservationTag.BRANCH_DISPATCH,), + ) + + def _findings_for_module( + self, module: ParsedModule, config: DetectorConfig + ) -> list[RefactorFinding]: + del config + index = module.syntax_index + findings: list[RefactorFinding] = [] + for node_index, node in sorted( + ( + *index.indexed_nodes_of_type(ast.ListComp), + *index.indexed_nodes_of_type(ast.SetComp), + *index.indexed_nodes_of_type(ast.GeneratorExp), + ) + ): + for generator in node.generators: + if _is_filesystem_discovery_iterable(generator.iter): + continue + existence = next( + ( + hit + for condition in generator.ifs + if (hit := _positive_existence_call(condition)) is not None + ), + None, + ) + if existence is not None: + symbol = index.lexical_symbol(node_index) + findings.append( + self.build_finding( + f"`{symbol}` filters a path collection with `{existence}`, " + "silently dropping missing inputs instead of failing loud.", + (SourceLocation(module.file_path, node.lineno, symbol),), + ) + ) + break + for node_index, ifexp in index.indexed_nodes_of_type(ast.IfExp): + existence = _positive_existence_call(ifexp.test) + if existence is None: + continue + symbol = index.lexical_symbol(node_index) + default_ctor = _default_fallback_constructor_name(ifexp.orelse) + if default_ctor is not None: + findings.append( + self.build_finding( + f"`{symbol}` guards `{existence}` with silent fallback to " + f"`{default_ctor}`, masking a missing input path.", + (SourceLocation(module.file_path, ifexp.lineno, symbol),), + ) + ) + elif isinstance(ifexp.body, ast.Call) and _is_empty_or_sentinel_fallback( + ifexp.orelse + ): + findings.append( + self.build_finding( + f"`{symbol}` wraps a call in `if {existence} else " + f"{ast.unparse(ifexp.orelse)}`, silently swallowing missing input.", + (SourceLocation(module.file_path, ifexp.lineno, symbol),), + ) + ) + for node_index, boolop in index.indexed_nodes_of_type(ast.BoolOp): + if not isinstance(boolop.op, ast.Or) or len(boolop.values) < 2: + continue + default_ctor = _default_fallback_constructor_name(boolop.values[-1]) + if default_ctor is not None: + symbol = index.lexical_symbol(node_index) + findings.append( + self.build_finding( + f"`{symbol}` falls back via `or` to `{default_ctor}`, silently " + "substituting default data when the requested input is empty.", + (SourceLocation(module.file_path, boolop.lineno, symbol),), + ) + ) + return findings + + +class SilentExceptionFallbackDetector(PerModuleIssueDetector): + """Detect EAFP `AttributeError` (`BOUND-7` bypass) and broad silent `except` swallowers.""" + + finding_spec = finding_spec_template( + PatternId.SILENT_EXCEPTION_FALLBACK, + "Exception handler silently swallows attribute or runtime errors into a default", + "Catching `AttributeError` to return/assign a default is `getattr(..., default)` (BOUND-7) in an EAFP disguise, and broad `except Exception` silent returns mask real bugs behind degraded defaults.", + "a declared nominal contract on the receiver or fail-loud error propagation", + "single-statement silent `except` handler catching `AttributeError` (non-I/O) or broad `Exception`/`BaseException`/bare `except`", + (CapabilityTag.FAIL_LOUD_CONTRACTS,), + (ObservationTag.ATTRIBUTE_PROBE,), + ) + + def _findings_for_module( + self, module: ParsedModule, config: DetectorConfig + ) -> list[RefactorFinding]: + del config + index = module.syntax_index + findings: list[RefactorFinding] = [] + for node_index, handler in index.indexed_nodes_of_type(ast.ExceptHandler): + description = _silent_exception_description(handler) + if description is None: + continue + symbol = index.lexical_symbol(node_index) + findings.append( + self.build_finding( + f"`{symbol}` silently swallows failures via {description}; declare " + "the capability or let the error fail loud.", + (SourceLocation(module.file_path, handler.lineno, symbol),), + ) + ) + return findings + + +@dataclass(frozen=True) +class ClonedDeclaredFamilyCandidate: + """Two or more unrelated `DeclaredFamily` roots sharing one family name.""" + + family_name: str + roots: tuple[CompactIndexedClass, ...] + + @property + def evidence(self) -> tuple[SourceLocation, ...]: + return tuple( + SourceLocation(root.file_path, root.line, root.qualname) + for root in self.roots + ) + + +class ClonedDeclaredFamilyDetector( + CompactClassRepositoryCandidateDetector[ClonedDeclaredFamilyCandidate] +): + """Detect parallel `DeclaredFamily` roots with the same name across modules.""" + + finding_spec = finding_spec_template( + PatternId.CLONED_DECLARED_FAMILY, + "Parallel DeclaredFamily roots with the same name split one family across modules", + "Declaring a second `DeclaredFamily` root with the same name in another module creates a drifting copy of its members and metadata (MEMB-1 / TIME-1). Import the owning family instead.", + "one `DeclaredFamily` root imported by every consumer module", + "two or more unrelated classes directly inheriting `DeclaredFamily` with the same class name", + (CapabilityTag.NOMINAL_IDENTITY, CapabilityTag.ENUMERATION), + (ObservationTag.CLASS_FAMILY,), + ) + + def _candidates_from_compact_context( + self, + context: CompactClassRepositoryContext, + config: DetectorConfig, + ) -> tuple[ClonedDeclaredFamilyCandidate, ...]: + del config + index = context.class_index + roots_by_name: dict[str, list[CompactIndexedClass]] = defaultdict(list) + for indexed in index.classes_by_symbol.values(): + if any( + base.rsplit(".", 1)[-1] == "DeclaredFamily" + for base in indexed.declared_base_names + ): + roots_by_name[indexed.simple_name].append(indexed) + candidates: list[ClonedDeclaredFamilyCandidate] = [] + for family_name, group in sorted(roots_by_name.items()): + if len(group) < 2: + continue + independent = tuple( + sorted( + ( + root + for root in group + if not any( + other.symbol in index.ancestor_symbols(root.symbol) + for other in group + if other.symbol != root.symbol + ) + ), + key=lambda root: (root.file_path, root.line), + ) + ) + if len(independent) >= 2: + candidates.append( + ClonedDeclaredFamilyCandidate( + family_name=family_name, + roots=independent, + ) + ) + return tuple(candidates) + + def _finding_for_candidate( + self, candidate: ClonedDeclaredFamilyCandidate + ) -> RefactorFinding: + locations = ", ".join( + f"`{root.symbol}` ({root.file_path}:{root.line})" + for root in candidate.roots + ) + return self.build_finding( + f"`{candidate.family_name}` is declared as {len(candidate.roots)} " + f"independent `DeclaredFamily` roots ({locations}): consolidate onto one " + "owning declaration.", + candidate.evidence, + ) + + +__all__ = ( + "ClonedDeclaredFamilyCandidate", + "ClonedDeclaredFamilyDetector", + "EmbeddedCodeLiteralDetector", + "SilentExceptionFallbackDetector", + "SilentMissingPathFallbackDetector", +) diff --git a/nominal_refactor_advisor/patterns.py b/nominal_refactor_advisor/patterns.py index 3824dd29..e1329790 100644 --- a/nominal_refactor_advisor/patterns.py +++ b/nominal_refactor_advisor/patterns.py @@ -709,6 +709,49 @@ class PatternId(IntEnum): ), witness_capabilities=(CapabilityTag.MRO_ORDERING,), ) + EMBEDDED_CODE_LITERAL = _pattern( + stable_id=60, + display_name="Embedded Code Literal", + required_relation=( + "A non-docstring multiline string literal embeds substantial foreign " + "source code (HTML/CSS/JS, JavaScript, shell, or SQL DDL) inside a " + "Python module instead of a checked source asset owned on disk." + ), + witness_capabilities=(CapabilityTag.PROVENANCE,), + ) + SILENT_MISSING_PATH_FALLBACK = _pattern( + stable_id=61, + display_name="Silent Missing Path Fallback", + required_relation=( + "A positive path-existence guard (`.is_file()`, `.exists()`, `.is_dir()`) " + "silently drops missing entries from a declared collection or substitutes " + "an empty container or default fallback object instead of failing loud." + ), + witness_capabilities=(CapabilityTag.FAIL_LOUD_CONTRACTS,), + ) + SILENT_EXCEPTION_FALLBACK = _pattern( + stable_id=62, + display_name="Silent Exception Fallback", + required_relation=( + "An exception handler catches `AttributeError` (EAFP `BOUND-7` bypass) " + "or broad `Exception`/`BaseException`/bare `except` and silently swallows " + "the failure into a constant, empty container, `None`, `pass`, or `continue`." + ), + witness_capabilities=(CapabilityTag.FAIL_LOUD_CONTRACTS,), + ) + CLONED_DECLARED_FAMILY = _pattern( + stable_id=63, + display_name="Cloned Declared Family", + required_relation=( + "Two unrelated modules declare distinct `DeclaredFamily` roots with the " + "same name, splitting one closed family's members and metadata across " + "drifting parallel registries." + ), + witness_capabilities=( + CapabilityTag.NOMINAL_IDENTITY, + CapabilityTag.ENUMERATION, + ), + ) def __new__( cls, diff --git a/tests/test_silent_fallbacks.py b/tests/test_silent_fallbacks.py new file mode 100644 index 00000000..e8cf27ab --- /dev/null +++ b/tests/test_silent_fallbacks.py @@ -0,0 +1,184 @@ +"""Regression tests for silent missing-path fallbacks, EAFP/broad exception +swallowers, BOUND-5 embedded foreign code literals, and cloned DeclaredFamily roots.""" + +from __future__ import annotations + +from pathlib import Path + +from nominal_refactor_advisor.ast_tools import ( + PythonSourcePathPolicy, + parse_python_modules, +) +from nominal_refactor_advisor.detectors._base import DetectorConfig +from nominal_refactor_advisor.detectors._silent_fallbacks import ( + ClonedDeclaredFamilyDetector, + EmbeddedCodeLiteralDetector, + SilentExceptionFallbackDetector, + SilentMissingPathFallbackDetector, +) +from nominal_refactor_advisor.patterns import PatternId +from nominal_refactor_advisor.scan_cache import ScanCache + + +def _write(root: Path, files: dict[str, str]) -> None: + for relative, text in files.items(): + path = root / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text) + + +def _detect(detector_cls, root: Path): + with ScanCache.scope(): + modules = parse_python_modules( + root, + use_parse_cache=False, + source_policy=PythonSourcePathPolicy(include_tests=False), + ) + return detector_cls().detect(modules, DetectorConfig()) + + +def test_new_pattern_ids_continue_stable_sequence() -> None: + assert PatternId.EMBEDDED_CODE_LITERAL == 60 + assert PatternId.SILENT_MISSING_PATH_FALLBACK == 61 + assert PatternId.SILENT_EXCEPTION_FALLBACK == 62 + assert PatternId.CLONED_DECLARED_FAMILY == 63 + + +def test_embedded_code_literal_flags_large_html_js_and_ignores_docstrings( + tmp_path: Path, +) -> None: + html_lines = [ + "", + "", + "", + "
", + "", + ] + blob = "\n".join(html_lines) + _write( + tmp_path, + { + "pkg/__init__.py": "", + "pkg/gallery.py": ( + f'"""\n{blob}\n"""\n\n' + f'_GALLERY_HTML = """\n{blob}\n"""\n' + 'SHORT = ""\n' + ), + }, + ) + findings = _detect(EmbeddedCodeLiteralDetector, tmp_path) + assert len(findings) == 1 + assert findings[0].pattern_id is PatternId.EMBEDDED_CODE_LITERAL + assert "HTML/CSS/JS" in findings[0].summary + assert "BOUND-5" in findings[0].summary + + +def test_silent_missing_path_fallback_catches_filters_ternaries_and_default_or( + tmp_path: Path, +) -> None: + _write( + tmp_path, + { + "pkg/__init__.py": "", + "pkg/pipeline.py": ( + "from pathlib import Path\n\n" + "def merge_shards(shards: list[Path]) -> list[Path]:\n" + " return [p for p in shards if p.is_file()]\n\n" + "def load_bench(path: Path, limit: int) -> tuple[str, ...]:\n" + " return build_cards(path) if limit > 0 and path.is_file() else ()\n\n" + "def choose_source(path: Path):\n" + " return JsonlSource(path) if path.is_file() else DefaultCalibrationTexts()\n\n" + "def load_rows(rows: list[str]):\n" + " return rows or DefaultCalibrationTexts().load_rows(10)\n\n" + "def fail_loud_missing(paths: list[Path]) -> list[Path]:\n" + " return [p for p in paths if not p.exists()]\n\n" + "def discover_files(root: Path) -> list[Path]:\n" + " return [p for p in root.iterdir() if p.is_file()]\n" + ), + }, + ) + findings = _detect(SilentMissingPathFallbackDetector, tmp_path) + symbols = {f.primary_evidence.symbol for f in findings} + assert symbols == {"merge_shards", "load_bench", "choose_source", "load_rows"} + assert all(f.pattern_id is PatternId.SILENT_MISSING_PATH_FALLBACK for f in findings) + + +def test_silent_exception_fallback_catches_eafp_attribute_and_broad_swallows( + tmp_path: Path, +) -> None: + _write( + tmp_path, + { + "pkg/__init__.py": "", + "pkg/training.py": ( + "import pickle\n\n" + "def add_loss(loss, outputs):\n" + " try:\n" + " return outputs.logits\n" + " except (AttributeError, IndexError, TypeError):\n" + " return loss\n\n" + "def get_head(trainer):\n" + " try:\n" + " conf_head = trainer.vocab_loss_policy.confidence_head\n" + " except AttributeError:\n" + " conf_head = None\n" + " return conf_head\n\n" + "def load_paper(path):\n" + " try:\n" + " return path.read_text()\n" + " except Exception:\n" + " return ''\n\n" + "def load_cache(raw: bytes):\n" + " try:\n" + " return pickle.loads(raw)\n" + " except (OSError, pickle.PickleError, EOFError, AttributeError):\n" + " return None\n\n" + "def capture_error(fn):\n" + " error = None\n" + " try:\n" + " fn()\n" + " except BaseException as caught:\n" + " error = caught\n" + " return error\n" + ), + }, + ) + findings = _detect(SilentExceptionFallbackDetector, tmp_path) + symbols = {f.primary_evidence.symbol for f in findings} + assert symbols == {"add_loss", "get_head", "load_paper"} + assert all(f.pattern_id is PatternId.SILENT_EXCEPTION_FALLBACK for f in findings) + + +def test_cloned_declared_family_flags_parallel_roots_and_allows_subclasses( + tmp_path: Path, +) -> None: + _write( + tmp_path, + { + "pkg/__init__.py": "", + "pkg/nominal.py": "class DeclaredFamily:\n pass\n", + "pkg/pipeline.py": ( + "from pkg.nominal import DeclaredFamily\n\n" + "class PipelineStage(DeclaredFamily):\n pass\n\n" + "class Stage1(PipelineStage):\n pass\n" + ), + "pkg/gallery.py": ( + "from pkg.nominal import DeclaredFamily\n\n" + "class PipelineStage(DeclaredFamily):\n pass\n\n" + "class Stage1(PipelineStage):\n pass\n" + ), + "pkg/unique.py": ( + "from pkg.nominal import DeclaredFamily\n\n" + "class UniqueFamily(DeclaredFamily):\n pass\n" + ), + }, + ) + findings = _detect(ClonedDeclaredFamilyDetector, tmp_path) + assert len(findings) == 1 + assert findings[0].pattern_id is PatternId.CLONED_DECLARED_FAMILY + assert "PipelineStage" in findings[0].summary + assert len(findings[0].evidence) == 2