Skip to content

Commit cb5fb27

Browse files
author
Vincent Kelleher
committed
raise an error when two classes have the same ambiguous attribute
Closes linkml/linkml#2403 Signed-off-by: Vincent Kelleher <[email protected]>
1 parent 34a30ee commit cb5fb27

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

linkml_runtime/utils/schemaview.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,8 @@ def get_slot(self, slot_name: SLOT_NAME, imports=True, attributes=True, strict=F
607607
for c in self.all_classes(imports=imports).values():
608608
if slot_name in c.attributes:
609609
if slot is not None:
610-
# slot name is ambiguous: return a stub slot
611-
return SlotDefinition(slot_name)
610+
raise ValueError(f'Attribute "{slot_name}" is already defined in another class, please use a '
611+
f'slot in that case')
612612
slot = copy(c.attributes[slot_name])
613613
slot.from_schema = c.from_schema
614614
slot.owner = c.name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
id: https://examples.org/get-slot-with-attribute#
2+
name: get-slot-with-attribute
3+
4+
prefixes:
5+
test: https://examples.org/get-slot-with-attribute#
6+
7+
default_prefix: test
8+
default_range: string
9+
10+
classes:
11+
ClassWithAttributes:
12+
attributes:
13+
randomAttribute:
14+
description: "A random attribute for testing purposes"
15+
range: integer
16+
minimum_value: 0
17+
maximum_value: 999
18+
19+
ImportantSecondClass:
20+
description: "Important class to reproduce the error I got as the class loop needs to have at least a
21+
second iteration"
22+
attributes:
23+
randomAttribute:
24+
description: "Now you see the ambiguity intensifying ?"
25+
range: integer
26+
minimum_value: 0
27+
maximum_value: 111

tests/test_utils/test_schemaview.py

+7
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,13 @@ def test_is_inlined(self):
945945
actual_result = sv.is_inlined(slot)
946946
self.assertEqual(actual_result, expected_result)
947947

948+
def test_ambiguous_attribute_through_get_slot(self):
949+
schema_path = os.path.join(INPUT_DIR, "get_slot_with_ambiguous_attributes.yaml")
950+
sv = SchemaView(schema_path)
951+
with self.assertRaisesRegex(ValueError, 'Attribute "randomAttribute" is already defined in another class, '
952+
'please use a slot in that case'):
953+
sv.get_slot("randomAttribute")
954+
948955

949956
if __name__ == '__main__':
950957
unittest.main()

0 commit comments

Comments
 (0)