Skip to content

mostlyright.contracts

mr.contracts — spine and source contract shapes plus their validators.

Exports the bitemporal SourceContract, the labeled-target SpineContract, the shared contract/frame validators (validate_source_contract(), validate_source_frame(), validate_spine_frame()), and a re-export of the TimePoint temporal primitive every contract is built from.

These names are for source authors — most callers meet them through an error message rather than through autocomplete — so they live here instead of in the root namespace.

The schema-description verb describe() and the reproducibility token DataVersion are re-exported here from the private _discovery module; both are contract-shaped and author-facing.

class mostlyright.contracts.DataVersion(sdk_version, schema_ids, sources, code_sha, data_sha, token)

Section titled “class mostlyright.contracts.DataVersion(sdk_version, schema_ids, sources, code_sha, data_sha, token)”

Bases: object

Reproducibility token stamping a research() call.

Carries enough metadata to re-run the same query against the same code + same data and get byte-identical output. token is a deterministic SHA-256 hash; components keeps the inputs for debugging.

classmethod for_research(, station, from_date, to_date, sdk_version=None)

Section titled “classmethod for_research(, station, from_date, to_date, sdk_version=None)”

Build a DataVersion for a research() call.

Hashes the current SDK version + the (station, from_date, to_date) triple + a deterministic data-cache fingerprint so two callers running the same query against the same cache get the same token.

Note: the sources tuple is the SDK’s source-priority contract, not the per-call subset that actually returned rows. research() takes no source-filter argument, so the tuple lists every source the call may consult.

classmethod from_components(, sdk_version, schema_ids, sources, code_sha, data_sha)

Section titled “classmethod from_components(, sdk_version, schema_ids, sources, code_sha, data_sha)”

class mostlyright.contracts.SourceContract(id, prefix, event_time_col=”, knowledge_time_col=”, entity_key=(‘station’, ), revision_order=(), revision_tiebreak=‘first_seen’, point_in_time_fidelity=‘exact’, units=, license=‘unknown’, valid_from=, native_frequency=‘daily’, availability=(‘python’, ), schema_id=”, schema_version=1, columns=())

Section titled “class mostlyright.contracts.SourceContract(id, prefix, event_time_col=”, knowledge_time_col=”, entity_key=(‘station’, ), revision_order=(), revision_tiebreak=‘first_seen’, point_in_time_fidelity=‘exact’, units=, license=‘unknown’, valid_from=, native_frequency=‘daily’, availability=(‘python’, ), schema_id=”, schema_version=1, columns=())”

Bases: object

Immutable bitemporal contract for one source frame.

Short source identity (e.g. "weather.climate.cli"). Named in every error the source’s rows trigger, so a leak is attributed to the source that caused it.

Column namespace this source owns; must end with "_" so a collision check is an unambiguous string-prefix test. A bad prefix raises ContractError at construction.

The column holding the row’s event/observation time. When the spine already carries this column, align uses a materialized equality join (the reconstructed-label path); otherwise it uses a backward as-of on knowledge_time_col.

The column holding when THIS SOURCE ROW became known — the feature’s knowledge cutoff. The leakage guard compares it against the spine’s decision_time.

The per-entity key tuple joins group by (by= in merge_asof); default ("station",).

Tuple of (column, "asc"|"desc") authority pairs. The collapse stage sorts by this order then keeps the top row per (entity_key, event_time_col) group. Empty → collapse is identity.

Tie-break at equal authority; "first_seen" gives strict > semantics — the second equal row never overwrites the first.

"exact" (true point-in-time, leakage-audited), "reconstructed" (highest authority available and knowingly not point-in-time, so it is exempt from the audit — for example the CLI settlement label), or "latest_only", which align refuses or warns loudly about.

Per-column unit strings (frozen).

Redistribution/resale-rights flag; default "unknown".

Per-entity coverage left-edge (frozen).

Declared native cadence; default "daily".

Runtime availability — a tuple of full language names (("python",) / ("typescript",) / ("python", "typescript")); discover() returns it as a list, not a comma-joined string.

Registered schema.*.v1 id; empty when the source has no registered schema.

Schema version integer.

The declared feature columns this source emits.

class mostlyright.contracts.SpineContract(y_columns, label_source_id, entity_key=(‘station’,), decision_time_col=‘decision_time_utc’, label_available_time_col=‘label_available_time_utc’, label_version=1)

Section titled “class mostlyright.contracts.SpineContract(y_columns, label_source_id, entity_key=(‘station’,), decision_time_col=‘decision_time_utc’, label_available_time_col=‘label_available_time_utc’, label_version=1)”

Bases: object

Immutable contract for the labeled-target (y) axis of align.

  • Parameters:
    • y_columns (tuple [str , ])
    • label_source_id (str)
    • entity_key (tuple [str , ])
    • decision_time_col (str)
    • label_available_time_col (str)
    • label_version (int)

The target column(s) the spine carries (e.g. ("y_high_f", "y_low_f")). A spine with no target is meaningless — an empty tuple raises ContractError at construction.

The source identity of the label (e.g. "weather.label.cli") — named in leakage / mismatch errors.

The per-entity key tuple the spine groups by; default ("station",).

The column holding the per-row decision cutoff (the “as of” a model would have acted); default "decision_time_utc".

The column holding when the label became KNOWN (published); default "label_available_time_utc".

Monotonic label-recipe version; default 1. A bump signals the settlement label’s computation changed.

class mostlyright.contracts.TimePoint(value)

Section titled “class mostlyright.contracts.TimePoint(value)”

Bases: object

A UTC-aware timestamp normalized to UTC for storage.

Construction accepts:

  • datetime.datetime with tzinfo set (converted to UTC)
  • pandas.Timestamp with tz set (converted to UTC)
  • ISO 8601 string with a timezone (parsed and converted to UTC)

Rejects:

  • naive datetime / pd.Timestamp (tzinfo / tz is None)
  • date-only ISO strings (no time component, e.g. "YYYY-MM-DD")

Storage is a datetime with tzinfo=timezone.utc and microsecond precision preserved.

  • Parameters: value (TimePointInput)

Convert to a different IANA zone via zoneinfo.ZoneInfo.

Display helper only — the canonical storage stays UTC. Raises zoneinfo.ZoneInfoNotFoundError if tz is not a known IANA timezone.

Construct from a tz-aware pandas.Timestamp.

Explicit alternative to passing a Timestamp to TimePoint(...) — useful when the caller wants the pandas-specific dispatch path to be unambiguous.

Raises ValueError if ts is pd.NaT (missing-data sentinel).

  • Return type: TimePoint
  • Parameters: ts (Timestamp)

Return an ISO 8601 UTC string.

Includes microseconds only when non-zero, matching standard datetime.isoformat() behavior (e.g. "2026-05-21T14:30:00+00:00" or "2026-05-21T14:30:00.123456+00:00").

  • Return type: str

Return a TimePoint for the current UTC time.

Return the underlying UTC datetime (microseconds preserved).

Return a human-readable description of a registered schema.

  • Return type: str
  • Parameters: schema_id (str)

mostlyright.contracts.validate_source_contract(spec)

Section titled “mostlyright.contracts.validate_source_contract(spec)”

Validate a SourceContract spec (bitemporal + point_in_time_fidelity + prefix).

Raised as a ContractError naming the offending field. Reused by mr.registry.source() / mr.registry.label() so a nonconforming spec is rejected at registration time (the prefix end-"_" rule is already enforced in SourceContract.__post_init__(); re-checked here for the direct-validation path).

mostlyright.contracts.validate_source_frame(df, contract)

Section titled “mostlyright.contracts.validate_source_frame(df, contract)”

Validate that a source DataFrame carries its contract’s declared columns.

Checks the bitemporal timestamp columns (event_time_col / knowledge_time_col), the entity_key, and the declared feature columns. Also sanity-checks the contract’s point_in_time_fidelity is a known value. Raises ContractError naming the first missing column.

mostlyright.contracts.validate_spine_frame(df, contract)

Section titled “mostlyright.contracts.validate_spine_frame(df, contract)”

Validate that a spine DataFrame carries its contract’s declared columns.

Checks the entity_key, decision_time_col, label_available_time_col, and every y_columns entry. Raises ContractError naming the first missing column.