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()