models.py¶
clew/query/models.py
R2 query-library return types: frozen, JSON-serializable dataclasses.
of
OriginSplit clew.query.models.OriginSplit.of(cls, Iterable[str|None] origins)
Classify roster members by whose code they are.
Source · 40 lines
def of(cls, origins: Iterable[str | None]) -> OriginSplit:
"""ONE PLACE THAT DECIDES THE BUCKETS, because gh#352 is a contract rather than a fix to
one roster. A second roster computing its own split is how two payloads come to disagree
about the same index, and that disagreement would read as a data problem rather than as a
duplicated rule. It lives ON the type instead of in `_common` because `_common` is
stdlib-only by design and this needs the dataclass at runtime.
THE ARITHMETIC IS CHECKED, NOT ASSUMED. A caller's whole reason to trust a split is that
the buckets account for the total; a silently lossy classification would report plausible
thirds that omit rows, which is worse than no split at all because it reads as a
measurement. The check costs nothing and cannot be phrased around.
THE THREE-WAY DISTINCTION MAPS ONTO THE SCHEMA. `''` is a resolved first-party file; a
non-empty string is the external root that owns it; `None` is a member whose file is not
resolved at all — doxygen's bare-filename `#include`s — and it stays its own bucket,
because "unknown" is not "ours".
@brief Build a split from per-member origins.
@return The split.
@version 1
"""
seen = list(origins)
external = [o for o in seen if o]
unresolved = [o for o in seen if o is None]
split = cls(
total=len(seen),
first_party=len(seen) - len(external) - len(unresolved),
external=len(external),
unresolved=len(unresolved),
external_roots=tuple(sorted(set(external))),
)
if split.first_party + split.external + split.unresolved != split.total:
raise AssertionError(
f"OriginSplit lost rows: {split.first_party}+{split.external}+"
f"{split.unresolved} != {split.total} — a split that does not account for the "
f"whole roster is worse than none, because it reads as a measurement"
)
return split
Activity · 7 steps
Requirements
section
object | None clew.query.models.SubjectDossier.section(self)
The one populated section, whatever kind it is.
Source · 29 lines
def section(self) -> object | None:
"""A CONSUMER SHOULD NOT HAVE TO MAP KIND TO FIELD NAME, because two of the nine
do not match (`class` -> `compound`, `macro` -> `function`) and a consumer that
guessed would read `None` off a populated dossier. The mapping lives here, once,
beside the fields it maps.
THE HAND-WRITTEN TUPLE THAT USED TO BE HERE SHIPPED A DEFECT (gh#29's neighbour).
This docstring already claimed "a section added to this dataclass is found without a
second edit somewhere else" while listing seven sections by hand, and `enumeration`
— the eighth, added by gh#6 — was left out. `_flatten_subject` reads this property to
build the MCP payload, so `dossier` answered `found: false` for EVERY enum in every
index: a bare definitive negative on symbols carrying a brief, a body span and their
enumerators, on the one surface gh#6 was filed against.
So it now reads the dataclass's own fields, minus the envelope keys. A claim about a
mechanism is not the mechanism, and the fix is the smaller of the two: the promise was
already the right promise.
@brief Return whichever section this dossier populated.
@return The populated section, or None.
@version 2
"""
for name in (f.name for f in fields(self) if f.name not in _ENVELOPE_FIELDS):
value = getattr(self, name)
if value is not None:
return value
return None