refdef: rules
These rules bind every implementation and every later change of refdef. The family rules F1-F17 are shared with elecdef and carried verbatim; the refdef rules R1-R12 add what is refdef's own and repeat no family rule. Each rule is one sentence and its GATE: the mechanical check that fails a breach, or "review" only where no mechanism can work, with the reason. A change to this file, to DESIGN.md, to docs/api-snapshot.txt, to src/refdef/tables/ or to tests/corpus/ needs a Design-review: trailer (F12, R3).
refdef rules
- R1. One value per string. For every designation, terminal or signal value
v,parse(str(v), bare=a).value == vfor any aspecta, and for every stringsthat parses without an issue,str(parse(s, bare=a).value) == s. GATE:tests/test_roundtrip.py, a property test whose stdlib generator draws levels, labels, tops, pins and signal names from the full alphabets R2 allows (pins and names including; & < > + - .and inner spaces), plustests/corpus/strings.txtof strings seen in real projects. - R2. The shapes that construct. A
Levelhas upper-case letters with a number, a number alone, or a stripped non-empty label without= - + % # : ; & < > .; a number is anintfrom 1 to 999_999_999; a label is never shaped like a code (ASCII letters, then digits worth 1 to 999_999_999, such asK1,k01or7), because-K1must read back as a coded level (K0andK1234567890are labels); aDesignation.topis empty or stripped, non-empty and without> : ; &; aTerminalhas at least one pin, each stripped, non-empty and without:; aSignal.nameis stripped and non-empty, and aSignal.ofisNoneor a designation with at least one level or a top (an emptySignal.ofis refused, because;Nmust read back to the same value); any other shape is malformed and raisesTypeErrorat construction (F3). GATE:tests/test_malformed.pyholds one fixture per clause (the labelsK1,k01and7, the numbers 0 and 1_000_000_000, and an emptySignal.ofamong them) and assertsTypeError;tests/test_level.pyasserts every value that constructs prints and parses back to itself (R1). - R3. Strict render.
str()prints every level with its sign and never pads, dots or drops a sign; every dialect (pad,dotted,bare,sign) is an explicitrenderoption, and a change to any printed form moves the golden. GATE:tests/test_render.pyassertsstr(v) == render(v)over the round-trip corpus, comparesrenderoftests/corpus/strings.txtunder each option totests/corpus/render.txtline by line, and asserts each option changes only the documented characters;scripts/design_guard.pywatchestests/corpus/(F12). - R4. Lenient parse with issues.
parseandparse_addressreturn a value and an issue tuple for any string, every lenient repair (case, leading zero, dot, missing sign, free label, unsupported suffix) is matched by an issue with its code, and no default letter or number is ever substituted; a digit run that is zero or longer than nine digits (the bound of R2) reads as a label with an issue. GATE:tests/test_parse.pyfeeds a fuzz corpus (random text, every sign alone, empty, whitespace) and asserts no exception, and asserts one issue per repair against a table of input, value and codes. - R5. Shape in parse, scheme in check.
parsetakes no scheme, every rule that needs one (aspect on, letter count, never-letters (Scheme.extend(never=)takes any iterable of single A-Z letters and refuses a barestr), unknown class bycode[:depth], duplicate) lives incheck, and no function reads a scheme or table other than through its argument. GATE:tests/test_parse.pyassertsparse's signature has noschemeparameter;tests/test_check.pyhas one case per check-familyIssueCode, assertsparsenever emits them, and an AST test in it fails the nameSTANDARDor an import fromrefdef.tablesanywhere undersrc/refdefoutsidescheme.pyand__init__.py. - R6. An issue is a code, a subject and a message.
Issuehas exactly these three fields, no severity, andIssueCodeis a closed enum of exactly 22 codes that every emitted issue uses: the parse family (16)EMPTY,NO_SIGN,EMPTY_LEVEL,EMPTY_TOP,WHITESPACE,LOWER_CASE,LEADING_ZERO,ZERO_NUMBER,NUMBER_TOO_LARGE,FREE_LABEL,BAD_LABEL,DOT_LEVEL,EMPTY_PIN,EMPTY_NAME,UNSUPPORTED_SUFFIX,NOT_ADDRESS(the last only fromparse_address), and the check family (6)ASPECT_OFF,LETTER_COUNT,NEVER_LETTER,UNKNOWN_CLASS,DUPLICATE,RESERVED_LETTER(advisory: a caller may treat it as a warning); the values are the lower-case names. GATE:tests/test_check.pyasserts the field names ofIssue, that theIssueCodenames equal these 22, and that every issue any test produces has a code inIssueCode. - R7. Per-aspect tree arithmetic.
within,relative_to(onDesignation,TerminalandSignal) andcommonfollow the rule inDESIGN.mdexactly, and every argument is withincommon(*ds). GATE:tests/test_tree.pyholds the unit-root, cable, mixed-order and top-node cases for all three types, and a property test assertinga.within(common(a, b)),len(b.relative_to(common(a, b)).levels) == len(b.levels) - len(common(a, b).levels)andTerminal(d, p).relative_to(r).of == d.relative_to(r)for random values. - R8. Numbering is pure.
allocatereturns the lowest free number fromfirstgiventakenandreserved,lowest_freedoes the same over plain ints,number_groupequalsallocateapplied in sequence with each result added totaken, and refdef holds no numbering store. GATE:tests/test_numbering.pyassertsallocateandlowest_freeagainst a brute-force search,lowest_freeagainstallocateover one class, andnumber_groupagainst the sequential fold, for random inputs. - R9. One natural order.
natural_keyorders digit runs by value and text runs as text, numbers before text, with the raw text as the last tie-break;sort_keyis one(aspect rank, natural_key(level.text))pair per level, with the aspect order a required keyword and an unlisted aspect ranked after the listed ones in enum order. GATE:tests/test_order.pyasserts the fixed cases (-W2 < -W10,X01 < X1, numbers before text), a property test thatsortedbysort_keyis a total order consistent withnatural_keyon single-level designations, and thatsort_keyhas no default foraspects. - R10. The class table is versioned.
ClassTable.versionis refdef's own line, bumps whenever the entry tuple changes, and the changelog names each moved code. GATE:tests/test_table_version.pyhashes the entry tuple and compares it with the hash stored besideversion, failing when the entries change and the version does not; the changelog line is review. - R11. One PLC grammar in the core.
refdef.plcholds only the direct-address form (%+ memory area + optional size + dotted path,*for incomplete), withas_bitthe one normalisation, and every vendor dialect is its own module behind its own type. GATE:tests/test_plc.pyround-tripstests/corpus/addresses.txtthroughparse_addressandstr, assertsas_bitchanges only a size-less dotted address, andtests/test_boundary.pyfails a vendor name (siemens,rockwell,omron,mitsubishi,beckhoff) inplc.py. - R12. Pins and signal names are opaque. refdef never reads inside a pin or a signal name: the only operations on them are the
:split inparse, the:join inrenderandnatural_key. GATE: an AST test intests/test_boundary.pyfails any attribute call, subscript or comparison onpins, a pin element orSignal.nameundersrc/refdefoutside those three sites; the F5 name list below catches a field that would hold their meaning.
refdef's lists for the family gates
- F4 allowlist of
strfields, by design:Level.code(class letters),Level.label(verbatim text),Designation.top,Terminal.pins(tuple[str, ...], opaque by R12),Signal.name(opaque by R12),ClassEntry.code,ClassEntry.title,ClassTable.name,ClassTable.version,Scheme.name,Issue.subject,Issue.message. Every other closed set is an enum:Aspect,IssueCode,plc.Memory,plc.Size. - F5 boundary names for refdef: F5's six (
rating,voltage,svg,x,y,page) plusmarking,role,kind,potential. - F6 caller conventions refdef takes only as arguments:
bare(which aspect a signless tag means, required),aspects(the sort order of aspects, required),pad,dotted,first,case_sensitive. - F15 numeric types: none; no refdef surface class defines an arithmetic or bitwise dunder, and
Designationis built by its constructor andchild()only.
Family rules
These rules bind elecdef and refdef alike, and symdef where it can (symdef has one runtime dependency, so F1 does not bind it). Both repositories carry this text verbatim in RULES.md,
and tests/test_family_rules.py in each compares it to the other's last release.
- F1. The library declares no runtime dependency and imports only the standard library and
itself; libraries of the family never import each other.
GATE:
tests/test_deps.pyassertsdependencies = []inpyproject.tomland walks every module's imports withast, allowing onlysys.stdlib_module_namesand the package. - F2. Every value is a frozen dataclass or an enum; the library performs no file, network,
clock or random access.
GATE:
tests/test_pure.pyfails a@dataclasswithoutfrozen=Trueand any import or name fromos,pathlib,io,socket,time,datetime,random,secrets,uuidor a call toopenundersrc/. - F3. Validation returns issues as values and never raises for content; only a malformed
call (a wrong type, a wrong kind of argument, or a shape the library's
RULES.mdnames as malformed) raisesTypeError. GATE:tests/test_check.pyrunscheckover every malformed fixture intests/fixtures/and asserts a non-empty issue tuple and no exception;tests/test_malformed.pyassertsTypeErrorfor each malformed-call fixture. - F4. A closed vocabulary is an enum or a typed constant, never a string.
GATE:
tests/test_vocab.pyfails any dataclass field annotatedstrthat is not in the allowlist that the test names andRULES.mdlists for that library. - F5. A fact has one home across the family:
symdefdraws,elecdefsays what a thing is,refdefsays what it is called, a consumer connects and lays out. GATE:tests/test_boundary.pyfails a field or function named for another home, from a list each library keeps in the test (for elecdef:designation,tag,svg,x,y,page; for refdef:rating,voltage,svg,x,y,page); the rest is review, because a duplicated meaning under a new name is invisible to a test. - F6. A caller's convention (a padding, an ordering, a default, a project class) arrives as an
argument or a value; no general function carries a caller's default. A fact the library itself
defines (a block's default terminal markings, the shipped scheme's signs) is not a convention.
GATE: the tests of every option-taking function pass the option explicitly, and
tests/test_boundary.pyfails a module-level default that names a consumer; what counts as a convention is review, because a test cannot tell a convention from a fact. - F7. No paid standard is cited and no standard's wording is used; open sources are used
only with the credit block in
README.md. GATE:tests/test_wording.pyfails a standards-body abbreviation followed by digits, an edition year beside a table or scheme name, or a clause reference (the pattern list lives in the test) anywhere undersrc/,docs/,README.mdorCHANGELOG.mdoutside the README credit block, and asserts the credit block names every open source atables/module header declares. - F8. The public surface is the names in
__all__of the top level and of each door, read bypython -m <library> api, and it changes only with the snapshot. GATE:tests/test_surface.pycomparespython -m <library> apitodocs/api-snapshot.txtbyte for byte, and fails an import of a non-surface name indocs/andtests/guide/. - F9. Every surface name has a docstring and a runnable example on the docs site.
GATE:
tests/test_docs.pyasserts a non-empty docstring for every surface name, one fenced Python block naming it underdocs/guide/, and runs every fenced block; the strict Zensical docs build runs in CI. - F10. Code stays lean: a function has at most 15 statements, complexity 8 and 12 branches,
no
definside adef; a module has at most 300 code lines; a docstring is one summary line plus at most two lines of why. GATE: ruffPLR0915=15,C901=8,PLR0912=12inpyproject.toml;tests/test_lean.pyfor nested defs, module lines and docstring shape; everynoqaandty: ignorenames its reason on its line, checked by the same test. - F11. Versions are semantic and every surface change carries a changelog line.
GATE:
scripts/release_guard.pyin CI fails whendocs/api-snapshot.txtdiffers from the last tag's copy andCHANGELOG.mdhas no line under the unreleased heading, and when a name disappeared without a major bump (minor before 1.0). - F12. A pull request that edits
DESIGN.mdorRULES.md, or changesdocs/api-snapshot.txt, or changes a shipped table undersrc/<library>/tables/, carries aDesign-review:trailer naming an approved review. The maintainer approves a change toDESIGN.mdorRULES.md; the design reviewer approves a surface or table change, and the maintainer sees it at the next review. GATE:scripts/design_guard.pyin CI inspects every commit in the range touching those paths and fails one without the trailer; branch protection requires the job. - F13. A consumer's missing need is met by a reviewed surface extension, never by a
workaround in the consumer, a private import, a copy of the rule or a consumer-specific
default in the library.
GATE:
tests/test_boundary.pyfails an import or a name of another family library undersrc/, andtests/test_standalone.py(F17) fails an application's name; the consumer side is review, because the library cannot see the consumer's tree. - F14. The library is MIT licensed, and the licence covers its code and its shipped tables.
GATE:
tests/test_licence.pyassertsLICENSEholds the MIT text andpyproject.tomldeclareslicense = "MIT", and fails atables/module header that declares an open source whose licence is share-alike or non-commercial. - F15. One public spelling per task: a second way to build, read or print the same value is
not added, and no operator is overloaded to build a value; arithmetic belongs only to the
numeric value types the library's
RULES.mdnames. GATE:tests/test_surface.pyfails an arithmetic or bitwise dunder (__add__,__or__,__truediv__and their kin) on a surface class thatRULES.mddoes not name as numeric; a second spelling under a new name is review, because a test cannot see that two names do one task. - F16. An agent finds everything from one place:
AGENTS.mdin the repository andllms.txtat the docs site root name the docs site, the surface command (python -m <library> api), the guide,DESIGN.mdandRULES.md, and say that a change followsRULES.md. GATE:tests/test_agents.pyasserts both files exist, name each of those five targets, and that every link in them resolves in the built site (the strict Zensical docs build). - F17. The library stands alone: no file in the repository names an application that uses it.
It is written for every electrical engineer, and its examples are general.
GATE:
tests/test_standalone.pylowercases every word in the tracked files and fails one whose SHA-256 is in the test's list of application-name hashes, so the test spells no name itself.