Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions changelog.d/python-reference-facet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- **The Python loader reads and enforces the reference facet** (`ref` on a `type: "attr"` entry
of a provider definition, `spec/metamodel-definitions.md`), matching the TypeScript and C#
loaders: the spec reader validates the facet and carries it to `AttrSchema.ref`, the lookup is
typed by the target, a dotted value resolves `System.child` and `pkg::System.child`, a bare
value is looked for in the referrer's own containers first, and a value that names nothing
(`ERR_REF_UNRESOLVED`), the wrong kind of node (`ERR_REF_WRONG_KIND`) or a node of another
system (the facet's own code) is refused. A canonical value is the path the lookup walked, and
a value whose canonical spelling would not look up the same target is kept as written. The
registry manifest writes `ref` only on an attribute that has one. No definition file uses the
facet yet, so no registry, manifest or generated output changes. A provider's own code that is
not an `ErrorCode` member now keeps its spelling in `MetaError.provider_code` (`code` stays
`ERR_UNKNOWN`; `code_name` gives either), and the CLI and `build_shared_model` report that
spelling.

Upgrading: Python `ReferenceDescriptor` is now `(attr, facet)`. A provider that sets
`TypeDefinition.references` in code builds `ReferenceDescriptor(attr, ReferenceFacet(...))`.
`target_type` and `error_code` move onto `ReferenceFacet` (where `error_code` is optional),
`target_sub_type` becomes the tuple `ReferenceFacet.target_sub_types`, and `dotted_field_path`
becomes `ReferenceFacet.field_path`.
`SymbolTable` moved out of `metaobjects.loader.registered_validation` into
`metaobjects.loader.symbol_table`; import it from there.
6 changes: 3 additions & 3 deletions server/python/src/metaobjects/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def load(strict_load: bool) -> LoadResult:
# `listMetadataFiles`: "cannot read metadata directory ...").
return None, [f"cannot read metadata directory {metadata_dir}: {e}"]
if result.errors:
msgs = [f"{e.code}: {e.message}" for e in result.errors]
msgs = [f"{e.code_name}: {e.message}" for e in result.errors]
return None, msgs
try:
# FR-043 §3.4 / §3.5 — same misuse guard the `Collection` load path runs
Expand Down Expand Up @@ -646,7 +646,7 @@ def _load_root_from_collection(
except ParseError as exc:
return None, [f"{exc.code}: {exc}"]
if result.errors:
msgs = [f"{e.code}: {e.message}" for e in result.errors]
msgs = [f"{e.code_name}: {e.message}" for e in result.errors]
return None, msgs
if unknown_attr_out is not None and not strict:
# See `_load_root`: what a strict load (`verify`) would reject, for `gen` to warn.
Expand Down Expand Up @@ -679,7 +679,7 @@ def _format_unknown_attr(err: MetaError) -> str:
where = " ".join(
p for p in (", ".join(files) if files else None, f"at {json_path}" if json_path else None) if p
)
head = f"{err.code.value if hasattr(err.code, 'value') else err.code}: {err.message}"
head = f"{err.code_name}: {err.message}"
return f"{head}\n in {where}" if where else head


Expand Down
2 changes: 1 addition & 1 deletion server/python/src/metaobjects/config/dependency_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,5 @@ def prove_locked_set_loads(config_dir: Path, specs: list[dict[str, str]]) -> str
[FileSource(Path(d.artifact_path), id=d.source_id) for d in resolved]
)
if result.errors:
return "; ".join(f"{e.code}: {e.message}" for e in result.errors)
return "; ".join(f"{e.code_name}: {e.message}" for e in result.errors)
return None
11 changes: 7 additions & 4 deletions server/python/src/metaobjects/core_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
)
from .provider import Provider
from .registry import AttrSchema, ChildRule, NodeFactory, TypeDefinition, TypeRegistry
from .validation_types import ReferenceDescriptor
from .validation_types import ReferenceDescriptor, ReferenceFacet
from .shared.base_types import (
SUBTYPE_BASE,
SUBTYPE_ROOT,
Expand Down Expand Up @@ -500,7 +500,8 @@ def _register_subtypes(
):
_def.references.append(
ReferenceDescriptor(
FIELD_ATTR_OBJECT_REF, TYPE_OBJECT, None, False, "ERR_UNRESOLVED_OBJECT_REF"
FIELD_ATTR_OBJECT_REF,
ReferenceFacet(TYPE_OBJECT, error_code="ERR_UNRESOLVED_OBJECT_REF"),
)
)

Expand Down Expand Up @@ -627,7 +628,8 @@ def _register_subtypes(
# @references is a cross-reference to a real object (FK target) — the loader's
# registry-derived validation resolves it; "Entity.field" resolves the entity head.
references=[ReferenceDescriptor(
IDENTITY_REFERENCE_ATTR_REFERENCES, TYPE_OBJECT, None, True, "ERR_INVALID_REFERENCE")],
IDENTITY_REFERENCE_ATTR_REFERENCES,
ReferenceFacet(TYPE_OBJECT, field_path=True, error_code="ERR_INVALID_REFERENCE"))],
)
)

Expand Down Expand Up @@ -689,7 +691,8 @@ def _register_subtypes(
# @objectRef is a cross-reference to a real object (the relationship target); the
# loader's registry-derived validation resolves it (dangling target = load error).
references=[ReferenceDescriptor(
RELATIONSHIP_ATTR_OBJECT_REF, TYPE_OBJECT, None, False, "ERR_INVALID_RELATIONSHIP")],
RELATIONSHIP_ATTR_OBJECT_REF,
ReferenceFacet(TYPE_OBJECT, error_code="ERR_INVALID_RELATIONSHIP"))],
)

# source.* — base (no attrs) + rdb (paradigm subtype with @table/@kind/@role/@schema).
Expand Down
14 changes: 13 additions & 1 deletion server/python/src/metaobjects/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,27 @@ def __init__(
source: str | None = None,
path: str | None = None,
envelope: Optional[ErrorSource] = None,
provider_code: str | None = None,
) -> None:
self.message = message
self.code = code
self.source = source
self.path = path
self.envelope = envelope
# A loader code a provider named that is not an ``ErrorCode`` member (a reference
# facet's ``errorCode`` or ``wrongSystemCode``, such as ``ERR_GRANT_CROSSES_SYSTEM``).
# ``code`` is then ``ERR_UNKNOWN``. ``None`` for a built-in code.
self.provider_code = provider_code

@property
def code_name(self) -> str:
"""The code as the provider spelled it: ``provider_code`` when set, else ``code``'s name."""
if self.provider_code is not None:
return self.provider_code
return str(getattr(self.code, "value", self.code))

def __repr__(self) -> str:
return f"MetaError({self.code.name}: {self.message!r})"
return f"MetaError({self.code_name}: {self.message!r})"


class ParseError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion server/python/src/metaobjects/fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def build_sources(

baseline = MetaDataLoader(providers=providers, strict=False).load(build_sources())
if baseline.errors:
detail = "\n".join(f" {e.code}: {e.message}" for e in baseline.errors)
detail = "\n".join(f" {e.code_name}: {e.message}" for e in baseline.errors)
return RunResult(
fatal=(
"this project's metadata does not currently load cleanly — fix the "
Expand Down
2 changes: 1 addition & 1 deletion server/python/src/metaobjects/loader/meta_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def load(self, sources: list[MetaDataSource]) -> LoadResult:
if roots:
# ADR-0059 R5 -- after the last validation pass, before freeze: resolve every
# reference once, record its target and rewrite it to its canonical spelling.
resolve_references(result.root)
resolve_references(result.root, self._registry)
result.root.freeze()
return result

Expand Down
Loading
Loading