Skip to content

Commit efed551

Browse files
committed
Refactor relationships
Signed-off-by: schapper <schapper@amazon.com>
1 parent 7f07d60 commit efed551

16 files changed

Lines changed: 302 additions & 185 deletions

File tree

PYDANTIC_GUIDE.md

Lines changed: 163 additions & 114 deletions
Large diffs are not rendered by default.

packages/overture-schema-buildings-theme/src/overture/schema/buildings/building_part.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ class BuildingPart(
5252
building_id: Annotated[
5353
Id,
5454
Field(description="The building to which this part belongs"),
55-
Reference(Relationship.BELONGS_TO, Building),
55+
Reference(Relationship.COMPOSITION, Building, role="part_of"),
5656
]

packages/overture-schema-codegen/src/overture/schema/codegen/extraction/field_constraints.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def describe_field_constraint(
9696
target = constraint.relatee
9797
target_id = TypeIdentity.of(target)
9898
target_str = link_fn(target_id) if link_fn else f"`{target.__name__}`"
99+
if constraint.role:
100+
role_label = constraint.role.replace("_", " ")
101+
return f"References {target_str} ({rel_label}, {role_label})"
99102
return f"References {target_str} ({rel_label})"
100103
if isinstance(constraint, Interval):
101104
desc = _describe_interval(constraint)

packages/overture-schema-codegen/tests/codegen_test_support.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ class Venue(
122122
]
123123
capacity: Annotated[int, Field(ge=1)] | None = None
124124
resident_ensemble: (
125-
Annotated[Id, Reference(Relationship.BELONGS_TO, Instrument)] | None
125+
Annotated[Id, Reference(Relationship.AGGREGATION, Instrument, role="part_of")]
126+
| None
126127
) = None
127128

128129

packages/overture-schema-codegen/tests/golden/markdown/venue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A location where musical performances take place.
1515
| `description` | `string` (optional) | *At least one of `name`, `description` must be set* |
1616
| `geometry` | `geometry` | *Allowed geometry types: Point, Polygon* |
1717
| `capacity` | `int64` (optional) | *`≥ 1`* |
18-
| `resident_ensemble` | `Id` (optional) | A unique identifier<br/><br/>*References `Instrument` (belongs to)* |
18+
| `resident_ensemble` | `Id` (optional) | A unique identifier<br/><br/>*References `Instrument` (aggregation, part of)* |
1919

2020
## Constraints
2121

packages/overture-schema-codegen/tests/test_constraint_description.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -382,22 +382,22 @@ def test_geometry_type_all_types(self) -> None:
382382
== "Allowed geometry types: LineString, Point, Polygon"
383383
)
384384

385-
def test_reference_belongs_to(self) -> None:
385+
def test_reference_composition(self) -> None:
386386
class Target(Identified):
387387
pass
388388

389-
constraint = Reference(Relationship.BELONGS_TO, Target)
389+
constraint = Reference(Relationship.COMPOSITION, Target)
390390
assert (
391-
describe_field_constraint(constraint) == "References `Target` (belongs to)"
391+
describe_field_constraint(constraint) == "References `Target` (composition)"
392392
)
393393

394-
def test_reference_connects_to(self) -> None:
394+
def test_reference_association(self) -> None:
395395
class Other(Identified):
396396
pass
397397

398-
constraint = Reference(Relationship.CONNECTS_TO, Other)
398+
constraint = Reference(Relationship.ASSOCIATION, Other)
399399
assert (
400-
describe_field_constraint(constraint) == "References `Other` (connects to)"
400+
describe_field_constraint(constraint) == "References `Other` (association)"
401401
)
402402

403403
def test_reference_link_fn_receives_type_identity(self) -> None:
@@ -412,25 +412,25 @@ def link_fn(tid: TypeIdentity) -> str:
412412
received.append(tid)
413413
return f"[`{tid.name}`](link)"
414414

415-
constraint = Reference(Relationship.BELONGS_TO, Target)
415+
constraint = Reference(Relationship.COMPOSITION, Target)
416416
result = describe_field_constraint(constraint, link_fn=link_fn)
417417

418418
assert len(received) == 1
419419
assert received[0].obj is Target
420420
assert received[0].name == "Target"
421-
assert result == "References [`Target`](link) (belongs to)"
421+
assert result == "References [`Target`](link) (composition)"
422422

423423
def test_reference_link_fn_used_in_output(self) -> None:
424424
"""link_fn return value appears verbatim in the description."""
425425

426426
class Target(Identified):
427427
pass
428428

429-
constraint = Reference(Relationship.CONNECTS_TO, Target)
429+
constraint = Reference(Relationship.ASSOCIATION, Target)
430430
result = describe_field_constraint(
431431
constraint, link_fn=lambda tid: f"[`{tid.name}`](path/to/target)"
432432
)
433-
assert result == "References [`Target`](path/to/target) (connects to)"
433+
assert result == "References [`Target`](path/to/target) (association)"
434434

435435

436436
class TestConstraintDisplayText:
@@ -442,7 +442,7 @@ def test_link_fn_forwarded_to_reference_constraint(self) -> None:
442442
class Target(Identified):
443443
pass
444444

445-
constraint = Reference(Relationship.BELONGS_TO, Target)
445+
constraint = Reference(Relationship.COMPOSITION, Target)
446446
cs = ConstraintSource(source_ref=None, source_name=None, constraint=constraint)
447447

448448
received: list[TypeIdentity] = []
@@ -455,4 +455,4 @@ def link_fn(tid: TypeIdentity) -> str:
455455

456456
assert len(received) == 1
457457
assert received[0].obj is Target
458-
assert result == "References [`Target`](link) (belongs to)"
458+
assert result == "References [`Target`](link) (composition)"

packages/overture-schema-codegen/tests/test_markdown_renderer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def test_venue_reference_links_when_context_available(self) -> None:
618618
lines = result.splitlines()
619619
ref_line = next(line for line in lines if "| `resident_ensemble` |" in line)
620620
assert "[`Instrument`](instrument.md)" in ref_line
621-
assert "belongs to" in ref_line
621+
assert "aggregation, part of" in ref_line
622622

623623
def test_venue_reference_unlinked_without_context(self) -> None:
624624
"""Reference constraint renders as plain code when no LinkContext."""
@@ -629,7 +629,7 @@ def test_venue_reference_unlinked_without_context(self) -> None:
629629
lines = result.splitlines()
630630
ref_line = next(line for line in lines if "| `resident_ensemble` |" in line)
631631
assert "References `Instrument`" in ref_line
632-
assert "belongs to" in ref_line
632+
assert "aggregation, part of" in ref_line
633633

634634

635635
class TestRenderEnumBasic:

packages/overture-schema-divisions-theme/src/overture/schema/divisions/division.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class CapitalOfDivisionItem(BaseModel):
116116
division_id: Annotated[
117117
Id,
118118
Field(description="ID of the division whose capital is the current division."),
119-
Reference(Relationship.CAPITAL_OF, Division),
119+
Reference(Relationship.HIERARCHY, Division, role="capital_of"),
120120
]
121121
subtype: DivisionSubtype
122122

@@ -139,7 +139,7 @@ class HierarchyItem(BaseModel):
139139
the division itself, and any other division that is an ancestor of the division's parent.
140140
""").strip()
141141
),
142-
Reference(Relationship.DESCENDANT_OF, Division),
142+
Reference(Relationship.HIERARCHY, Division, role="descendant_of"),
143143
]
144144
subtype: DivisionSubtype
145145
name: Annotated[
@@ -276,7 +276,7 @@ class Division(
276276
parent divisions.
277277
""").strip()
278278
),
279-
Reference(Relationship.CHILD_OF, Division),
279+
Reference(Relationship.HIERARCHY, Division, role="child_of"),
280280
] = None
281281
admin_level: AdminLevel | None = None
282282

@@ -382,7 +382,7 @@ class Division(
382382
list[
383383
Annotated[
384384
Id,
385-
Reference(Relationship.CAPITALLED_BY, Division),
385+
Reference(Relationship.HIERARCHY, Division, role="has_as_capital"),
386386
]
387387
]
388388
| None,

packages/overture-schema-divisions-theme/src/overture/schema/divisions/division_area.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ class DivisionArea(
132132
division_id: Annotated[
133133
Id,
134134
Field(
135-
description="Division ID of the division this area belongs to.",
135+
description="Division ID of the parent division of this area.",
136136
),
137-
Reference(Relationship.BELONGS_TO, Division),
137+
Reference(Relationship.HIERARCHY, Division, role="child_of"),
138138
]
139139
country: Annotated[
140140
CountryCodeAlpha2,

packages/overture-schema-divisions-theme/src/overture/schema/divisions/division_boundary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class DivisionBoundary(
125125
list[
126126
Annotated[
127127
Id,
128-
Reference(Relationship.BOUNDARY_OF, Division),
128+
Reference(Relationship.COMPOSITION, Division, role="boundary_of"),
129129
]
130130
],
131131
Field(

0 commit comments

Comments
 (0)