Skip to content

feat(schema): _stddev sibling sugar + ufloat boundary helper (#149) - #183

Merged
gerchowl merged 1 commit into
mainfrom
feature/149-stddev-sugar
May 5, 2026
Merged

gerchowl merged 1 commit into
mainfrom
feature/149-stddev-sugar

Conversation

@gerchowl

@gerchowl gerchowl commented May 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Per ADR-0003, _stddev lands as loader sugar, not as parallel dataclass fields. The existing in-value {nominal, stddev} form is now also routed through _parse_value for property scalars (it already worked for compositions). Sibling sugar exists for hand-edited TOML ergonomics. Both forms work; double-specification is a hard error.

# Canonical (works everywhere now)
density = { nominal = 7.85, stddev = 0.05 }

# Sibling sugar (folded into a ufloat at parse time)
density_value = 7.85
density_unit = "g/cm^3"
density_stddev = 0.05

# Hard error — double-specification
density = { nominal = 7.85, stddev = 0.05 }
density_stddev = 0.10        # ValueError("double-specification ...")

Loader changes (src/pymat/loader.py)

Two-pass update_properties:

  1. Pass 1: collect <base>_stddev siblings into {base_key: stddev}.
  2. Pass 2: route each property value through _parse_value (so dict forms work for scalars too), then fold sibling stddev into a ufloat at assignment.

Hard errors:

  • Double-specification (in-value AND sibling) → ValueError
  • Orphan _stddev (no matching base value) → ValueError

Boundary helpers (per ADR-0003 §6)

Once contributors actually populate uncertainties, downstream callers crossing JSON or build123d boundaries will see ufloat where they expect float:

Site Fix
Material.density_g_mm3 Coerce ufloat → nominal float (build123d can't handle ufloats)
pymat-mcp _props_dict _nominal() helper applied after the existing Pint-magnitude unwrap
pymat-mcp _brief Same
pymat-mcp compute_mass Same — density_g_per_cm3 and mass_g both become plain floats

Test plan

  • tests/test_stddev_sugar.py (11 tests): sibling sugar + value form + double-spec + orphan + zero stddev + multiple fields + density_g_mm3 boundary + mass_from_volume_mm3 boundary
  • clients/python/pymat-mcp/tests/test_tools.py::TestComputeMass::test_ufloat_density_returns_plain_floats: end-to-end JSON round-trip with a ufloat density
  • Full suite: 499 passed, 11 skipped, zero regressions
  • ruff check clean
  • ruff format no diff

Risks / regressions considered

  1. Existing TOMLs that already use {nominal, stddev} for property scalars — there are none in the corpus today (the form only worked for compositions until now). New routing is purely additive for them.
  2. Existing TOMLs with <prop>_stddev-named legacy fields that aren't intended as siblings — none exist; verified by full suite + test_toml_integrity passing.
  3. uncertainties import on cold path — already imported by loader for _parse_value; no new dependency surface.
  4. Pint *_qty properties when value is ufloat — Pint+ufloat interop is intact (Pint accepts ufloat as magnitude); density_qty.to('kg/m^3') works, .magnitude is a ufloat. Lossy coercion only at JSON/build123d boundaries.

Closes #149
Part of the schema foundation series: #150 (merged) → #149 → #148 → #174.

Per ADR-0003, `_stddev` is loader sugar — not a parallel set of
dataclass fields. The existing in-value `{nominal, stddev}` form
remains canonical (now also routed through `_parse_value` for
property scalars, not just compositions). Sibling sugar exists
for hand-edited TOML ergonomics.

Loader (src/pymat/loader.py):
- Two-pass `update_properties`: collect `<base>_stddev` siblings,
  then route each value through `_parse_value` and fold sibling
  stddev into a ufloat at assignment time.
- Hard error on double-specification (in-value `{nominal, stddev}`
  AND sibling `_stddev`) — silent precedence is worse than failure.
- Hard error on orphan stddev (`density_stddev` with no base value).

Boundary helpers (per ADR-0003 #6):
- `Material.density_g_mm3` coerces ufloat → nominal float for
  build123d (which can't accept ufloats in mass calculations).
- pymat-mcp `_nominal()` helper applied at JSON boundaries
  (`_props_dict`, `_brief`, `compute_mass`) so agents never see
  a ufloat that `json.dumps` can't serialize.

Tests:
- `tests/test_stddev_sugar.py` (11 tests): sibling sugar + value form
  + double-spec + orphan + zero stddev + multiple fields per group +
  density_g_mm3 boundary + mass_from_volume_mm3 boundary.
- `clients/python/pymat-mcp/tests/test_tools.py::TestComputeMass::
  test_ufloat_density_returns_plain_floats`: end-to-end JSON round-trip
  with a ufloat density.

Full suite: 499 passed, 11 skipped, no regressions.

Closes #149
@gerchowl
gerchowl merged commit 01dc2b4 into main May 5, 2026
18 checks passed
@gerchowl
gerchowl deleted the feature/149-stddev-sugar branch May 5, 2026 11:19
@vig-os-release-app vig-os-release-app Bot mentioned this pull request May 5, 2026
6 tasks
gerchowl added a commit that referenced this pull request May 6, 2026
Bundles four schema-foundation feats merged after 3.9.0 plus the Vis
public-API clarification into a single release:

- feat(schema): _sources provenance table (#150 / #182)
- feat(schema): _stddev sibling sugar + ufloat boundary helper
  (#149 / #183)
- feat(policy): data licensing policy + CI license gate (#174 / #184)
- feat(schema): T-dependent property curves (#148 / #185)
- fix(vis): declare Vis as public API (#98 / #186)

Three feats × one fix → minor version bump per semver. The version
3.9.1 originally proposed for the Vis public-API change alone became
inconsistent once the four schema feats had landed on main without
their own release cut, so they're rolled into 3.10.0 honestly rather
than shipped as a misleading patch.

CHANGELOG entry for 3.10.0 covers all five items with PR + issue
links. .release-please-manifest.json synced to 3.10.0 so the open
release-please PR (#80) recomputes correctly going forward.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Schema: per-property uncertainty (_stddev)

1 participant