Skip to content

feat(api): pymat.materials registry (Mapping + callable + filterable) + tags schema - #228

Merged
gerchowl merged 4 commits into
mainfrom
feature/materials-registry
Jul 1, 2026
Merged

gerchowl merged 4 commits into
mainfrom
feature/materials-registry

Conversation

@gerchowl

@gerchowl gerchowl commented May 8, 2026

Copy link
Copy Markdown
Contributor

Summary

TDD-grown surface (red → green) closing the consumer-DX cluster from Bernhard's py-mat #218 critique + the persona-spike findings.

Combines tasks #127 (pymat.materials registry + search filter extensions) and #132 (tags schema). Both ship together because tags are a natural filter axis on the new surface.

pymat.materials registry

Single object, three usage shapes — mirrors pint.UnitRegistry's call/attr/subscript trifecta:

pymat.materials                              # Mapping[str, Material]
pymat.materials["Stainless Steel 304"]      # subscript (mypy-clean)
pymat.materials("Stainless Steel 304")      # callable lookup
pymat.materials()                            # all materials → list
pymat.materials(category="metals")          # filter → list
pymat.materials(grade="316L")
pymat.materials(tags=["austenitic"])         # multi-axial
pymat.materials(with_vis=True)
"X" in pymat.materials                      # membership
for name, m in pymat.materials.items(): ...  # full Mapping API
len(pymat.materials)

Drops the earlier pymat.lookup() proposal (redundant with the callable form). Keeps pymat["X"] shorthand for 3.4-3.10 BC.

Mixed positional + filter kwargs raise TypeError (lookup-with-filter is ambiguous) — caught both by static @overload and runtime guard.

Tags schema (#132)

New Material.tags: list[str] field. Multi-axial labels orthogonal to the TOML hierarchy (chemistry / function / industry / treatment / regulation). TOML field tags = [...]; loader merges parent chain into effective tag list (children extend parent's tags, don't replace).

Backfilled stainless TOML with canonical example tags:

  • [stainless] → ["ferrous", "stainless", "corrosion-resistant"]
  • [stainless.s316L] → child extends with ["austenitic", "marine-grade", "molybdenum-bearing", "316-family"]

(Wider tag backfill across all 157 materials in a follow-up data PR — #132 phase 2.)

search() filter extensions

pymat.search() accepts the same domain filter kwargs as materials(): category, grade, tags, with_vis. Filters narrow the candidate set BEFORE the fuzzy/exact match runs.

Visual filters (source, tier, PBR scalars) stay in pymat.vis.search() where they live — different namespace, different concern.

Empty query returns [] regardless of filters (preserves 3.4-3.10 contract; pure filter/browse goes through pymat.materials() instead).

TDD provenance

  • Red: tests/test_materials_registry.py (38 tests, 7 classes) — written first, all failing
  • Green: implementation in _registry.py + core.py + loader.py + search.py until 38/38 pass

Suite: 861 passed, 69 skipped, 15 xfailed. No regressions.

Files changed

File Change
src/pymat/_registry.py (new) _Materials class — Mapping + @overload-typed __call__
src/pymat/__init__.py Wire materials = _Materials() into pymat namespace + __all__; __module__ rewrite for Sphinx / IDE / pyright
src/pymat/core.py Add tags: List[str] field to Material
src/pymat/loader.py Parse tags from TOML, merge with parent's tags (order-preserving union)
src/pymat/search.py Extend signature with category / grade / tags / with_vis filter kwargs
src/pymat/data/metals.toml Initial tag backfill on stainless + 316L (test fixtures + contributor example)
tests/test_materials_registry.py (new) 38 tests across 7 classes
tests/test_public_api_surface.py Extend MATERIAL_INIT_KWARGS frozen-set with tags

Test plan

  • uv run pytest tests/test_materials_registry.py -v — 38 passed
  • uv run pytest (full suite) — 861 passed, 0 failed
  • uv run ruff check . — clean
  • uv run ruff format --check . — clean
  • Manual REPL probe: pymat.materials("Stainless Steel 304"), pymat.materials(tags=["austenitic"]), pymat.search("polished", category="metals") all work as documented

Refs #127, #132, py-mat #218

gerchowl and others added 4 commits May 8, 2026 02:34
… + tags schema

TDD-grown surface (red → green) closing the consumer-DX cluster from
Bernhard's #218 critique + the persona-spike findings:

## pymat.materials registry surface

Single object, three usage shapes:

  pymat.materials                              # Mapping[str, Material]
  pymat.materials["Stainless Steel 304"]      # subscript (mypy-clean)
  pymat.materials("Stainless Steel 304")      # callable lookup
  pymat.materials()                            # all materials → list
  pymat.materials(category="metals")          # filter → list
  pymat.materials(grade="316L")
  pymat.materials(tags=["austenitic"])         # multi-axial
  pymat.materials(with_vis=True)
  "X" in pymat.materials                      # membership
  for name, m in pymat.materials.items(): ...  # full Mapping API
  len(pymat.materials)

Mirrors pint.UnitRegistry's call/attr/subscript trifecta — same shape
downstream Python users have already encountered.

Drops the earlier `pymat.lookup()` proposal (redundant with the
callable form). Keeps `pymat["X"]` shorthand for 3.4-3.10 BC.

Mixed positional + filter kwargs raise TypeError (ambiguous lookup-
with-filter semantics) — caught by static @overload + runtime guard.

## Tags schema

New `Material.tags: list[str]` field. Multi-axial labels orthogonal
to the TOML hierarchy (chemistry / function / industry / treatment /
regulation). TOML field `tags = [...]`; loader merges parent chain
into the effective tag list (children extend, don't replace).

Backfilled stainless TOML with the canonical tag set:
- [stainless] → ferrous, stainless, corrosion-resistant
- [stainless.s316L] → austenitic, marine-grade, molybdenum-bearing,
                      316-family
(Other materials' tags backfilled in follow-up data PRs per #132.)

## search() filter extensions

pymat.search() accepts the same domain filter kwargs as materials():
- category, grade, tags, with_vis

Visual filters (source, tier, PBR scalars) stay in pymat.vis.search()
where they belong.

Filters narrow the candidate set BEFORE the fuzzy/exact match runs;
empty query returns [] (back-compat with 3.4-3.10 contract — pure
filter/browse goes through pymat.materials() instead).

## Implementation

- src/pymat/_registry.py (new) — `_Materials` class, both Mapping and
  callable, with @overload-typed __call__ for type checkers.
- src/pymat/__init__.py — wire `materials = _Materials()` instance
  into pymat namespace + `__all__`. `__module__` rewritten to "pymat"
  for Sphinx / IDE autoimport (#98 / mat-vis #282 convention).
- src/pymat/core.py — add `tags: List[str] = field(default_factory=list)`
  to Material.
- src/pymat/loader.py — parse `tags` from TOML, merge with parent's
  effective tags (order-preserving union, dups dropped).
- src/pymat/search.py — extend signature with category/grade/tags/
  with_vis filter kwargs; preserves existing fuzzy/exact/limit
  contract for empty queries.
- tests/test_materials_registry.py (new) — 38 tests across 7 classes
  covering Mapping protocol, callable forms, filters, tags
  inheritance, mixed-args rejection, search filters, and public API
  surface (`__module__` rewrite, `__all__`).
- tests/test_public_api_surface.py — extend MATERIAL_INIT_KWARGS
  with `tags`.
- src/pymat/data/metals.toml — initial tag backfill on stainless +
  316L (test fixtures + canonical example for contributors).

## Suite

861 passed, 69 skipped, 15 xfailed (no regressions; +38 new tests).

Refs #127, #132, py-mat #218, persona-spike findings (CAD + analyst +
publishing + simulator).
Cold `pymat.materials(category="metals")` returned [] because __iter__,
__len__, and the filter path read the lazily-populated registry directly
instead of load_all()-ing like search() does. A fresh consumer — the exact
"use existing materials, don't create them" persona from #218 — would see
an empty library on their very first line.

Make all three catalog paths eager-load, mirroring search(); the lookup
paths were already correct (they route through search()'s load_all()).
Add subprocess cold-import regression tests so the module-level load_all()
fixture in the suite can't re-mask the cold path.

Refs #218, #228

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The main-merge brought a stricter ruff config that now flags N814
(camelcase imported as constant) on the local `from pymat import
Material as _M` in test_search_with_category. Material is already
imported at module top — use it directly.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@gerchowl
gerchowl merged commit 6c030bd into main Jul 1, 2026
20 checks passed
@vig-os-release-app vig-os-release-app Bot mentioned this pull request Jul 1, 2026
7 tasks
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.

1 participant