Skip to content

Commit e04627c

Browse files
Merge branch 'main' into schemaview_relationship_tests
2 parents f9145fe + 89e17c7 commit e04627c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

linkml_runtime/utils/schemaview.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,17 @@ def slot_leaves(self, imports: bool = True, mixins: bool = True) -> list[SlotDef
11511151
c for c in self.all_slots(imports=imports) if self.slot_children(c, mixins=mixins, imports=imports) == []
11521152
]
11531153

1154+
@lru_cache(None)
1155+
def type_roots(self, imports: bool = True) -> list[TypeDefinitionName]:
1156+
"""Return all types that have no parents.
1157+
1158+
:param imports: whether or not to include imports, defaults to True
1159+
:type imports: bool, optional
1160+
:return: list of all root types
1161+
:rtype: list[TypeDefinitionName]
1162+
"""
1163+
return [t for t in self.all_types(imports=imports) if not self.type_parents(t, imports=imports)]
1164+
11541165
@lru_cache(None)
11551166
def is_multivalued(self, slot_name: SlotDefinition) -> bool:
11561167
"""Return True if slot is multivalued, else returns False.

tests/test_utils/test_schemaview.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,64 @@ def test_all_types_induced_types(schema_view_with_imports: SchemaView) -> None:
11791179
assert len(view.type_ancestors("SymbolString")) == len(["SymbolString", "string"])
11801180

11811181

1182+
@pytest.mark.parametrize(
1183+
("schema", "type_roots"),
1184+
[
1185+
(
1186+
"""
1187+
string:
1188+
uri: xsd:string
1189+
base: str
1190+
integer:
1191+
uri: xsd:integer
1192+
base: int
1193+
boolean:
1194+
uri: xsd:boolean
1195+
base: Bool
1196+
""",
1197+
{"string", "integer", "boolean"},
1198+
),
1199+
(
1200+
"""
1201+
string:
1202+
uri: xsd:string
1203+
base: str
1204+
acronym:
1205+
typeof: string
1206+
TLA:
1207+
typeof: acronym
1208+
even_number:
1209+
typeof: integer
1210+
integer:
1211+
uri: xsd:integer
1212+
base: int
1213+
""",
1214+
{"string", "integer"},
1215+
),
1216+
(
1217+
"""
1218+
circular_type:
1219+
uri: xsd:Circle
1220+
typeof: type_circle
1221+
1222+
type_circle:
1223+
typeof: circular_type
1224+
1225+
semi_circular_type:
1226+
typeof: circular_type
1227+
""",
1228+
set(),
1229+
),
1230+
],
1231+
)
1232+
def test_type_roots(schema: str, type_roots: set[str]) -> None:
1233+
"""Test the retrieval of types with no parent type."""
1234+
schema_head = "id: https://w3id.org/linkml/tests/types\nname: types\ntypes:\n"
1235+
1236+
sv = SchemaView(f"{schema_head}\n{schema}")
1237+
assert set(sv.type_roots()) == type_roots
1238+
1239+
11821240
def test_all_enums(schema_view_with_imports: SchemaView) -> None:
11831241
"""Test all_enums"""
11841242
view = schema_view_with_imports

0 commit comments

Comments
 (0)