Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ValueError for empty string namespace prefix #142

Closed
ghost opened this issue Mar 31, 2021 · 3 comments
Closed

Raise ValueError for empty string namespace prefix #142

ghost opened this issue Mar 31, 2021 · 3 comments

Comments

@ghost
Copy link

ghost commented Mar 31, 2021

Hiya, I'm getting an invalid namespace prefix error. While I understand the error entirely, it is being raised at the point of serialisation, and in the circumstance that I have defined several namespaces programmatically, I do not know which one isn't being filled properly.

Would it be better/possible to check for namespace validity within the add_namespace() method, to follow the principle of failing fast/early?

@trungdong
Copy link
Owner

Hello, could you provide an example where it failed? I'm not sure what issue you are having at the moment.

@NathanCummings
Copy link

NathanCummings commented Apr 6, 2021

I'm filling a ProvDocument programmatically by reading the metadata. In the event that someone hasn't filled a field, I am trying to define a namespace with an empty string. When this happens, I don't get an exception thrown until I come to serialise the document. Now of course, I have encountered the problem and built a check into my code, but in a more complex pipeline, the error I received and the point in the code that it was thrown did not make it clear where the problem was. An earlier check on the call to add_namespace() would have told me which namespace was invalid.

Traceback (most recent call last):
  File "/afs/eufus.eu/g2itmdev/user/g2ncummi/fusionprov/venv/bin/imasprov", line 11, in <module>
    load_entry_point('fusionprov==0.1.4', 'console_scripts', 'imasprov')()
  File "/afs/eufus.eu/g2itmdev/user/g2ncummi/fusionprov/venv/lib/python3.6/site-packages/fusionprov/imasprov.py", line 225, in main
    ids_prov.prov_from_data_ids(graph=args.graph)
  File "/afs/eufus.eu/g2itmdev/user/g2ncummi/fusionprov/venv/lib/python3.6/site-packages/fusionprov/imasprov.py", line 197, in prov_from_data_ids
    self.serialize_provenance(graph=graph)
  File "/afs/eufus.eu/g2itmdev/user/g2ncummi/fusionprov/venv/lib/python3.6/site-packages/fusionprov/imasprov.py", line 139, in serialize_provenance
    self.prov_doc.serialize(xml_path, format="xml")
  File "/afs/eufus.eu/g2itmdev/user/g2ncummi/fusionprov/venv/lib/python3.6/site-packages/prov/model.py", line 2494, in serialize
    serializer.serialize(stream, **args)
  File "/afs/eufus.eu/g2itmdev/user/g2ncummi/fusionprov/venv/lib/python3.6/site-packages/prov/serializers/provxml.py", line 52, in serialize
    xml_root = self.serialize_bundle(bundle=self.document, force_types=force_types)
  File "/afs/eufus.eu/g2itmdev/user/g2ncummi/fusionprov/venv/lib/python3.6/site-packages/prov/serializers/provxml.py", line 110, in serialize_bundle
    xml_bundle_root = etree.Element(_ns_prov("document"), nsmap=nsmap)
  File "src/lxml/etree.pyx", line 3022, in lxml.etree.Element
  File "src/lxml/apihelpers.pxi", line 131, in lxml.etree._makeElement
  File "src/lxml/apihelpers.pxi", line 118, in lxml.etree._makeElement
  File "src/lxml/apihelpers.pxi", line 219, in lxml.etree._setNodeNamespaces
  File "src/lxml/apihelpers.pxi", line 1749, in lxml.etree._prefixValidOrRaise
ValueError: Invalid namespace prefix ''

In a script with multiple namespaces defined, the above traceback does not indicate where the problem occurred, as the error isn't caught until much later.

@trungdong
Copy link
Owner

Thanks for the information, Nathan. I'll pop this test in the code next time.

@trungdong trungdong changed the title ValueError for invalid namespace prefixed raised too late Raise ValueError for empty string namespace prefix Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants