From 53684419f84d99cbd9b7728746c9a6add7860f94 Mon Sep 17 00:00:00 2001 From: Tom Mitchell Date: Tue, 30 Nov 2021 06:56:34 -0500 Subject: [PATCH] Add check for uninitialized identity When an object is passed to a referenced object property and its identity has not yet been initialized then raise an error with a helpful error message. --- sbol3/refobj_property.py | 6 ++++++ test/test_referenced_object.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/sbol3/refobj_property.py b/sbol3/refobj_property.py index 9ba37eb..9aa7a20 100644 --- a/sbol3/refobj_property.py +++ b/sbol3/refobj_property.py @@ -32,6 +32,12 @@ def to_user(self, value: Any) -> str: @staticmethod def from_user(value: Any) -> rdflib.URIRef: if isinstance(value, SBOLObject): + # see https://github.com/SynBioDex/pySBOL3/issues/357 + if value.identity is None: + # The SBOLObject has an uninitialized identity + msg = f'Cannot set reference to {value}.' + msg += ' Object identity is uninitialized.' + raise ValueError(msg) value = value.identity if not isinstance(value, str): raise TypeError(f'Expecting string, got {type(value)}') diff --git a/test/test_referenced_object.py b/test/test_referenced_object.py index f5ce5ef..fd1e153 100644 --- a/test/test_referenced_object.py +++ b/test/test_referenced_object.py @@ -125,6 +125,15 @@ def test_adding_referenced_objects(self): # to get back to the original instance via document lookup self.assertEqual(execution.identity, foo.members[0].lookup().identity) + def test_no_identity_exception(self): + # See https://github.com/SynBioDex/pySBOL3/issues/357 + sbol3.set_namespace('https://github.com/SynBioDex/pySBOL3') + collection = sbol3.Collection('foo_collection') + subc = sbol3.SubComponent(instance_of='https://github.com/SynBioDex/pySBOL3/c1') + exc_regex = r'Object identity is uninitialized\.$' + with self.assertRaisesRegex(ValueError, exc_regex): + collection.members.append(subc) + if __name__ == '__main__': unittest.main()