Source code for cadrumo.domain.calculations.registry.binding_terminal_audit
"""The effective terminal-origin expectation of one binding.:mod:`.binding_terminal_origin` defines what an expectation says;:mod:`.binding_provider_registration` declares which terminal origin classes aprovider kind is able to rest on at all. Neither on its own answers the questiona runtime audit has to ask: for THIS binding, what should the resolvedprovenance graph look like?A binding may author :attr:`~.schema.BindingDefinition.terminal_origins`explicitly, and when it does that authored claim is the whole answer -- it isnarrower than the registration by construction, because the compiler alreadyrefuses an authored class the registration cannot produce. Where a bindingauthors none (the ordinary case across the corpus), the expectation falls backto the one its registration implies, so that "the author wrote nothing" and "noexpectation exists" stay different statements. A derived default is stillfalsifiable: it is read off declared registration data, not assumed."""from__future__importannotationsfromtypingimportTYPE_CHECKINGfrom....core.aggregationimportBindingAggregationOp,BindingSourceKind,CalculationSourceLineageRolefrom.binding_provider_registrationimportBindingProviderRegistration,registration_forfrom.binding_terminal_originimport(TerminalOriginCardinality,TerminalOriginExpectation,TerminalOriginFingerprint,)ifTYPE_CHECKING:from.schemaimportBindingDefinition__all__=["effective_terminal_origins","expected_terminal_origins",]_CARRY_ONLY_OPS=frozenset({BindingAggregationOp.COPY})"""The operation set of a family that carries ONE terminal fact into a value."""_FINGERPRINTED_ORIGIN_KINDS=frozenset({BindingSourceKind.PROFILE,BindingSourceKind.ATRIBUCION_MEMBER,BindingSourceKind.FOREIGN_ASSET,BindingSourceKind.INVENTORY,BindingSourceKind.PAYABLE_INVOICE,BindingSourceKind.COLLECTIBLE_INVOICE,BindingSourceKind.M347_THIRD_PARTY_OPERATION,},)"""Filing-grade families whose terminal fact is an individually addressable record.A fingerprint is demanded where the terminal fact is ONE persisted record thatcan be hashed on its own -- a profile field, an invoice, a detail row. A foldover classified ledger transactions or over an already-filed modelo's casillashas no single record to fingerprint, so demanding one there would produce astanding advisory that no resolver could ever satisfy, which trains an operatorto stop reading the channel. Absence of a demand is not permission to drop afingerprint that IS produced; it is the statement that this family's terminalfact is a fold."""def_default_cardinality(registration:BindingProviderRegistration)->TerminalOriginCardinality:"""Return how many terminal nodes one value of this kind must rest on. ``exactly_one`` is claimed only for a scalar family that carries a single fact -- its permitted operations are the carry alone. A family permitted to fold (``sum``, ``count_distinct``) or to emit rows rests on as many terminal nodes as the taxpayer has facts, so its floor is ``at_least_one``: emptiness stays a violation, plurality does not. """ifregistration.output=="scalar"andregistration.permitted_aggregation_ops<=_CARRY_ONLY_OPS:return"exactly_one"return"at_least_one"def_default_fingerprint(registration:BindingProviderRegistration)->TerminalOriginFingerprint:"""Return whether each terminal node of this kind must carry a fingerprint."""ifregistration.disposition=="filing_grade"andregistration.kindin_FINGERPRINTED_ORIGIN_KINDS:return"required"return"optional"
[docs]defexpected_terminal_origins(kind:BindingSourceKind)->tuple[TerminalOriginExpectation,...]:"""Return the expectation a binding of ``kind`` carries when it authors none. One expectation per terminal origin class the registration permits, in stable class order, each in the primary lineage role: the provenance node a resolver names as the terminal fact behind the value is primary by definition, and a contributor node hangs off one of those. """registration=registration_for(kind)cardinality=_default_cardinality(registration)fingerprint=_default_fingerprint(registration)returntuple(TerminalOriginExpectation(source_class=source_class,role=CalculationSourceLineageRole.PRIMARY,cardinality=cardinality,fingerprint=fingerprint,)forsource_classinsorted(registration.permitted_terminal_origins,key=lambdamember:member.value))
[docs]defeffective_terminal_origins(binding:BindingDefinition)->tuple[TerminalOriginExpectation,...]:"""Return the expectation a resolved value of ``binding`` is audited against."""ifbinding.terminal_origins:returnbinding.terminal_originsreturnexpected_terminal_origins(binding.source)