UnboundElementError when trying to subclass
bavovanachte opened this issue · 3 comments
I've made a working example of a ReqIf XML output with just the classes generated by pyxb.
Now, I wanted to subclass some of these classes to hide some of the pyxb.BIND() function calls (and add more functionality), but ran into some issues when trying to call a.toxml()
later on.
Below is the way I've defined the subclass. I think I did it the same way as described here, but it gives me the following error:
Traceback (most recent call last):
File "ReqIF.py", line 95, in <module>
print(a.toxml());
File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 560, in toxml
dom = self.toDOM(bds, element_name=element_name)
File "/home/bvn/.pyenv/versions/3.6.10/lib/python3.6/site-packages/pyxb/binding/basis.py", line 528, in toDOM
raise pyxb.UnboundElementError(self)
pyxb.exceptions_.UnboundElementError: Instance of type {http://www.omg.org/spec/ReqIF/20110401/reqif.xsd}REQ-IF has no bound element for start tag
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "ReqIF.py", line 97, in <module>
print(e.details())
AttributeError: 'UnboundElementError' object has no attribute 'details'
The code:
class REQ_IF_x(REQ_IF_):
def __init__(self):
super().__init__(
THE_HEADER=pyxb.BIND(),
CORE_CONTENT=pyxb.BIND(),
TOOL_EXTENSIONS=pyxb.BIND(),
)
REQ_IF_._SetSupersedingClass(REQ_IF_x)
# Construct the header
a = REQ_IF_x()
header = REQ_IF_HEADER(
IDENTIFIER = "abcd",
COMMENT = "abcd",
CREATION_TIME = dateTime.today(),
REPOSITORY_ID = "",
REQ_IF_TOOL_ID = "",
REQ_IF_VERSION = "1.0",
SOURCE_TOOL_ID = "",
TITLE = "")
a.THE_HEADER.REQ_IF_HEADER=header
# More code for initializing fields
try:
print(a.toxml());
except Exception as e:
print(e.details())
Could you give me some pointers as to what I'm doing wrong and how I can still achieve the intended goal? I assume it's related to the super constructor somehow?
For clarity: this is based on the reqif bundle generated out of pyxb
Also, I am able to subclass other classes from the bindings. I've tried REQ_IF_CONTENT and REQ_IF_HEADER with constructors.
I guess the difficulty comes from the fact that there is both a REQ_IF_
and a REQ_IF
version? This doesn't seem to be the case for the other classes.
class REQ_IF_ (pyxb.binding.basis.complexTypeDefinition):
"""Complex type {http://www.omg.org/spec/ReqIF/20110401/reqif.xsd}REQ-IF with content type ELEMENT_ONLY"""
...
REQ_IF = pyxb.binding.basis.element(pyxb.namespace.ExpandedName(_Namespace, 'REQ-IF'), REQ_IF_, location=pyxb.utils.utility.Location('/home/bvn/Projects/git/pyxb/pyxb/bundles/reqif/schemas/reqif.xsd', 8, 2))
_Namespace.addCategoryObject('elementBinding', REQ_IF.name().localName(), REQ_IF)
The full file/project for reference: https://github.com/bavovanachte/reqif_pyxb_tryout/blob/master/reqif_pyxb/ReqIF.py