From e34dabfdb0d8f2369942af3119b15619f3ed0c69 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 13:04:39 -0700 Subject: [PATCH] Remove unused documents.traces_for_lesson, overlay.wrap_rule_body and HOUSE_WIDTH; a test fails on any new dead function. done / next: fix 4 (down --until time) --- sisi/documents.py | 4 ---- sisi/overlay.py | 25 ------------------------- tests/test_no_dead_code.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 tests/test_no_dead_code.py diff --git a/sisi/documents.py b/sisi/documents.py index 19ae699..72a8ec3 100644 --- a/sisi/documents.py +++ b/sisi/documents.py @@ -22,10 +22,6 @@ def check_rule_text(text, rid=None): return r -def traces_for_lesson(lesson_id): - return [] # the per-lesson trace regime is retired; provenance lives on the learning record - - def trace_lines(kind, entry_id): return [] diff --git a/sisi/overlay.py b/sisi/overlay.py index 5213f33..9d69109 100644 --- a/sisi/overlay.py +++ b/sisi/overlay.py @@ -28,7 +28,6 @@ from . import paths # games must not load more text. A learning round still may not grow the book without shortening. MAX_LINES = 200 MAX_CHARS = 12000 -HOUSE_WIDTH = 100 KINDS = ("rule", "verifier", "recipe") ID_TAIL = re.compile(r"\s*\{#([A-Za-z0-9][A-Za-z0-9_-]*)\}\s*$") NUM = re.compile(r"^\d+\.\s+") @@ -98,30 +97,6 @@ def rule_text(r): return "## %s\n\n%s\n" % (r["title"], r["body"]) -def wrap_rule_body(body, width=HOUSE_WIDTH): - """Re-wrap rule prose to the house width, preserving paragraphs, bullets and code fences.""" - import textwrap - out, in_fence = [], False - for line in (body or "").split("\n"): - if line.strip().startswith("```"): - in_fence = not in_fence - out.append(line) - continue - if in_fence or not line.strip(): - out.append(line) - continue - stripped = line.lstrip() - indent = "" - if stripped.startswith(("- ", "* ")): - indent = " " - elif re.match(r"^\d+\.\s", stripped): - indent = " " - wrapped = textwrap.wrap(line, width=width, subsequent_indent=indent, - break_long_words=False, break_on_hyphens=False) - out.extend(wrapped or [""]) - return "\n".join(out).strip("\n") - - def _read(p): with open(p, encoding="utf-8") as f: return f.read() diff --git a/tests/test_no_dead_code.py b/tests/test_no_dead_code.py new file mode 100644 index 0000000..62ecb8f --- /dev/null +++ b/tests/test_no_dead_code.py @@ -0,0 +1,35 @@ +"""Nothing in sisi/ is defined and never called: a function or class whose name appears nowhere else in the +product (sisi/, bin/, tools/) is dead code, and a test of it only keeps it alive.""" +import ast, collections, glob, os, re, unittest + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +# Seams that are documented and exercised by real-behaviour tests (docs/evaluation.md); keep them. +SEAMS = {"validate_check", "capture_case", "compare_for_candidate"} + + +def _files(): + out = glob.glob(os.path.join(ROOT, "sisi", "*.py")) + glob.glob(os.path.join(ROOT, "bin", "*")) + out += [f for f in glob.glob(os.path.join(ROOT, "tools", "**", "*"), recursive=True) if os.path.isfile(f)] + return out + + +class NoDeadCode(unittest.TestCase): + def test_every_function_and_class_in_sisi_is_referenced_somewhere(self): + text = "" + for f in _files(): + try: + text += open(f, encoding="utf-8").read() + "\n" + except (OSError, UnicodeDecodeError): + pass + words = collections.Counter(re.findall(r"\w+", text)) + dead = [] + for f in sorted(glob.glob(os.path.join(ROOT, "sisi", "*.py"))): + for n in ast.walk(ast.parse(open(f, encoding="utf-8").read())): + if isinstance(n, (ast.FunctionDef, ast.ClassDef)) and not n.name.startswith("__") \ + and n.name not in SEAMS and words[n.name] <= 1: + dead.append("%s:%s" % (os.path.basename(f), n.name)) + self.assertEqual(dead, []) + + +if __name__ == "__main__": + unittest.main()