SynBioDex / pySBOL3

Native python implementation of SBOL 3.0 specification
MIT License
37 stars 16 forks source link

TypeError: Expecting string, got <class 'NoneType'> #357

Closed bbartley closed 3 years ago

bbartley commented 3 years ago

I was debugging someone else's code, and ran into the above error message. It was difficult to figure out what was causing this error. It turns out that this error occurs when trying to set a ReferencedObject property using an object that does not have an identity. Here is a minimal test case:

>>> collection = sbol3.Collection('foo_collection')
>>> subc = sbol3.SubComponent(instance_of='foo')
>>> collection.members.append(subc)

The error traces back to around here: https://github.com/SynBioDex/pySBOL3/blob/main/sbol3/refobj_property.py#L37

I recommend that the from_user method check whether the object has an identity of None and fail with a more explicit message such as "Cannot set reference to object. Object identity is uninitialized."

tcmitchell commented 3 years ago

I'm about to merge a pull request that detects this situation and provides a more helpful message.

>>> import sbol3
>>> sbol3.set_namespace('https://github.com/SynBioDex/pySBOL3')
>>> collection = sbol3.Collection('foo_collection')
>>> subc = sbol3.SubComponent(instance_of='foo')
>>> collection.members.append(subc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/bin/../../../Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/_collections_abc.py", line 962, in append
    self.insert(len(self), value)
  File "/Users/tmitchel/Projects/sd2/pySBOL3/sbol3/property_base.py", line 157, in insert
    item = self.from_user(value)
  File "/Users/tmitchel/Projects/sd2/pySBOL3/sbol3/refobj_property.py", line 40, in from_user
    raise ValueError(msg)
ValueError: Cannot set reference to <sbol3.subcomponent.SubComponent object at 0x7fea400939a0>. Object identity is uninitialized.