Documentation

Docs / Reference

ADR 0069 - Python structural claim contract: files, declarations, imports

Status: Accepted (delegated review, 2026-11-18)

Context

T020 (ADR 0066) labels a language's structural scope precise only through a rule-table row whose evidence includes a claim contract with a 1:1 test, goldens, corpus checks, an oracle where cheap and an independent review. ADR 0068 did this for Go (row PL-L1). The T020 investigation (the T020 task record (internal)) inventoried every Python file, declaration and import form internal/lang/python emits at extractors-39 and compared it with an oracle built on CPython's own ast, tokenize and importlib rules over the raw bytes (scripts/oracle/python_structure/oracle.py).

Kinds, qualified names, decorators and owners agreed everywhere. The investigation found five precision stops: declaration spans and hashes that include the comments tree-sitter keeps after a body (PS-S2-1: fastapi 6, langflow 869); declarations and edges recovered from files CPython rejects (PS-S2-2); edges for relative imports beyond the top-level package (PS-S2-3) and to a .py file an extension module shadows (PS-S2-4), both on the probe only; and external:.#pypi:<name> nodes for names that are in-repository packages (PS-S2-5: langflow 34). It also left open points O-S2-1..O-S2-9, decided with the developer before and during the implementation.

Options considered

  1. Run CPython at index time (parse with ast, resolve with importlib). Exact, but Graphvein is an offline Go binary with no Python dependency; it would tie results to whatever interpreter the user has. Rejected.
  2. Keep tree-sitter, make the claims follow CPython where tree-sitter reads the same text, refuse the rest, and check everything with an oracle that reads the raw bytes with CPython itself. Chosen.
  3. Narrow the scope (no import edges). Loses the most useful fact.

For imports: (a) change the shared ModuleResolver.Resolve (calls, routes, SQLModel and table access read it: other rows' outputs would move), or (b) a separate structural resolution for import edges only. (b) chosen.

For files Python would load but Graphvein does not read (PS-S2-4): (a) let the linker read the disk (breaks "the scan is the input" and the cache model); (b) have the scanner report the entries it skipped and refuse an import when one of them comes first in FileFinder's order. (b) chosen.

Decision

Option 2 with import option (b) and shadowing option (b). The contract is docs/lang/python-structure.md: rows PS-M1, PS-F1..PS-F3, PS-D1..PS-D7, PS-I1..PS-I7 and the default branch D, mirrored by structureRows (internal/lang/python/structure.go), kept 1:1 by TestPythonStructureContract (rows, rejection forms, default-branch forms, the reference version, and a fixture case reaching each).

No rule-table row is added and no label changes: PL-L2 follows the independent review (added, see "Review notes").

Results

TestPythonStructureOracle with CPython 3.14.10, 0 gate records on each:

corpus Python files (rejected) declarations: oracle = agree import statements file edges = agree external edges = agree
fixture (with the written cases) 55 (13) 80 65 45 10
fastapi 1762adac 43 (0) 174 192 90 117
langflow 504c02fc 4,143 (0) 50,033 31,557 16,570 14,659
self-index (this branch's worktree) 186 (14) 530 387 88 298

Compared: per file, presence in the inventory, size and hash against the raw bytes; per declaration (ast def, class and module constant), kind, qualified name, name, all four span numbers, content hash, decorators and the one contains edge (owner and evidence); per import statement (matched by evidence span = the ast statement span), the set of file targets and of external targets (ecosystem and name); every Graphvein import edge's evidence matches a statement. A file Python rejects must claim nothing. Not compared: identity strings (compared through kind, file and qualified name), provenance and confidence, diagnostic texts, the default-branch counts, calls, routes and models.

Omissions the contract explains (no claim, not a disagreement), langflow: from __future__ 1,243, undecided names 943, in-repository top-level names 110, I-5 submodules 40, self edges 17. A first run found 12 langflow files refused although Python accepts them (*args: T / **kwargs: T read as positional parameters by the parameter_order check); fixed before these numbers, and the oracle now reports any such refusal (O-refused-but-ast-accepts).

Snapshot diff, main (extractors-39) against this branch, fastapi, langflow and a git archive of main for the self-index: the only differences are the intended classes (counts in the unit report): spans and hashes trimmed (fastapi 6, langflow 869, self 1), standard-library externals moved to python-stdlib (edges 41 / 8,013 / 138), in-repository externals replaced by unresolved_import warnings (langflow 43 edges, 88 warnings), self edges dropped (langflow 15), one undecided from-name edge dropped (langflow, inventory I-4), the rejected broken.py node (self), and python_structure_unread diagnostics.

Review fixes (S2R)

The independent review (the T020 task record (internal)) found one class A finding (A1), nine class B and eight class C. Each is fixed as a class rule in the code, the contract table and TestPythonStructureContract together, and the oracle now checks each class from CPython's own behaviour instead of sharing the blind spot. Extractor version stays extractors-40 (S2B is not on main; S2B and S2F ship as one change).

Oracle after the fixes (CPython 3.14.10, 0 gate records on each): fixture 81 Python files / 84 import statements (and a 64-byte parse-limit run), fastapi 43 / 192, langflow 4,143 / 31,557 (omissions it explains, new: script_directory_module 3, undecided_name 943 to 1,317), self-index 213 / 420. No file Python accepts is refused on fastapi or langflow (O-refused-but-ast-accepts 0). With the S2B binary the new oracle reports 32 gates on a probe of the review's 30 rejection forms, 3 on a symlink and data-directory probe, and 32 on this fixture; with this branch 0.

Snapshot diff against S2B (21fd647): fastapi none; langflow 3 pypi edges removed (A1), 22 file edges removed (undecided from-names, mostly packages with a module __getattr__), counts script_directory_module +12 and undecided_name +387, calls -116/+37 (B9: calls through refused or undecided imports go; calls through a from . import loading submodule come); routes, models and table access unchanged.

Review notes (S2R, pending acceptance)

Independent review (the T020 task record (internal), at 21fd647): The PL-L2 flip was blocked by one class A finding (A1), nine class B and eight class C findings. Step S2F (section "Review fixes" above): Every class A and B finding is fixed as a class rule in the code, the contract table and TestPythonStructureContract together, and the oracle checks each class from CPython's own behaviour. No class A finding is open. S2F open point 2 (a script in a directory that is an implicit root) was decided and fixed: the import is refused (script_directory_module) only when that directory alone answers differently from the PS-M1 search path (both candidates exist and differ); the oracle applies the same rule with CPython's PathFinder, and with the rule removed from the Go code it reports the fixture case as a gate (I-wrong-edge).

Corpus and oracle (extractors-40, CPython 3.14.10 built from tag v3.14.10): TestPythonStructureOracle (CPython 3.14.10): 0 gate records on the fixture, fastapi, langflow and the self-index (fixture 84 Python files, 86 import statements, and a 64-byte parse-limit run; fastapi 43 / 192; langflow 4,143 / 31,557; self-index 216 / 422, and 217 / 426 after the merge of main). It runs in CI's postgres job, which sets up CPython 3.14 with actions/setup-python and passes it as GRAPHVEIN_PYTHON_ORACLE (the fixture; the corpora and the self-index are opt-in).

Label flip (row PL-L2): snapshot diff of the S2F binary (c7efe76) against S2L, fastapi 1762adac, langflow 504c02fc and a git archive of the S2L branch for the self-index: the only differences are precision values of Python file records and file nodes (best_effort to precise: fastapi 43 of 43 Python files, langflow 4,143 of 4,143, self-index 193 of 216) and, on the self-index, the new fixture case of the implicit-root rule (one imports edge removed, one python_structure_unread diagnostic added). The 23 self-index files that stay best_effort are all rejected under PS-F2 (21 listed rejection forms and 2 tree-sitter errors: syntax_bad.py and testdata/repos/python's broken.py). Calls stay row D (code_calls).

Consequences

Known gaps

Review notes (acceptance, 2026-11-18)

Accepted after delegated review (2026-11-17). Steps: the investigation (the T020 task record (internal)), S2B implementation, one independent review (the T020 task record (internal): one class A, nine class B, eight class C), S2F fixes (every A and B finding fixed as a class rule and mutation-checked; C1, C2, C4, C5 fixed; C3, C6-C8 documented), S2L (implicit-root script-directory refinement, row PL-L2, label flip).

Revision 1 (2026-11-24): Unicode 17 tables

Status: Accepted (delegated review, 2026-11-25)

Context

golang.org/x/text v0.29.0 has two known vulnerabilities (GO-2026-5970, GO-2026-6629; ADR 0086), fixed in v0.41.0 (developer decision 2026-11-24: bump now). Under the Go 1.27 toolchain, v0.41.0 selects its Unicode 17.0.0 tables, so the extractor's NFKC normalisation of identifiers (identifier) and its \N{...} name check (unicodeName, rune names) move from Unicode 15.0.0 to 17.0.0; Go's own unicode package (the constant rule's IsLetter/IsUpper/IsDigit, row PS-D7) is 17.0.0 since the go1.27.4 toolchain. The reference Python, CPython 3.14, has Unicode 16.0.0 (every 3.14 build: checked on 3.14.5 and 3.14.6). The contract's rows that depend on Unicode data: PS-D1..PS-D7 and PS-I1..PS-I7 (names and module names are NFKC-normalised identifiers), PS-D7 (letter and upper-case classes), and PS-F2 (which files are rejected: identifier validity and \N{...} names).

Options considered

  1. An oracle CPython whose Unicode version matches the tables. No CPython 3.14 has Unicode 17; the first is 3.15 (3.15.0b4 is the newest build uv offers in the container, unicodedata 17.0.0). Moving the reference to 3.15 changes the developer decision O-S2-1 (rejection forms, standard-library and meta-path lists) and a beta is no reference. Rejected for this revision.
  2. Keep v0.29.0's Unicode 15 tables (vendor or pin them). Leaves the vulnerable module or forks it. Rejected.
  3. Refuse what the versions disagree on. List, from both versions' own data, every code point whose identifier-relevant properties or name differ between the reference's Unicode 16.0.0 and the tables' 17.0.0, and refuse a file whose claims would depend on one. Chosen.

Decision

Review fixes

The review of this revision found three pre-existing gaps that read Unicode data the lists do not cover, and one oracle difference; each is fixed as a class rule (CPython 3.14.6 as the oracle):

So the Python extractor's Unicode reads are: NFKC of identifiers, the character-name table, and the letter / upper-case / Nd classes of PS-D7, all covered by the lists and TestPythonUnicodeVersion.

Consequences

Results

TestPythonStructureOracle with CPython 3.14.6 (uv's python-build-standalone build, unicodedata 16.0.0), 0 gate records on each: fixture 96 Python files / 94 import statements (and the 64-byte parse-limit run), fastapi 1762adac 43 / 192, langflow 504c02fc 4,143 / 31,557 (the omissions the contract explains are unchanged), self-index 336 / 681. Compared as in "Results" above (declarations, spans, hashes, decorators, owners, import targets; a rejected file claims nothing). TestPythonUnicodeVersion, TestPythonStdlibNames and TestPythonMetaPathNames pass with the same interpreter.

Snapshot diff, x/text v0.29.0 binary (extractors-59) against this revision, every record of the emitted stream with the version string masked, on the corpora with Python files: fastapi (43 Python files), langflow (4,143), kftray (5), crates.io (3), dozzle (1), miniflux (1) and RealWorld (1): no record added or removed. The fixture golden gains only rejected/unicode_version.py (its file node, python_syntax_rejected naming unicode_version and the python_structure_unread count); the other goldens change only in the version string.

Known gaps

Revision 1 review notes (2026-11-25): one independent review (no A; B1 \N{} case folding, B2 identifiers normalising to True/False/None, B3 non-ASCII hatch project names, C1/C2 wording and oracle; all fixed); oracle CPython 3.14.6 0 gate records on the fixture, fastapi and langflow; Unicode 17 lists generated from CPython 3.15.0b4 and checked against unicodedata2 17.0.1; no corpus change; gate run 45481900427 green. Known gap: the lists are regenerated when CPython 3.15 is final; __debug__ bindings stay PS-F3 (decision O-S2-3 (a)).

Report a problem with this pageSource: docs/adr/0069-python-structure-contract.md