tefra / xsdata

Naive XML & JSON Bindings for python
https://xsdata.readthedocs.io
MIT License
325 stars 59 forks source link

Circular imports? #469

Closed skinkie closed 3 years ago

skinkie commented 3 years ago

I am trying to make this XSD https://github.com/NeTEx-CEN/NeTEx/blob/master/xsd/NeTEx_publication.xsd working with xsdata. Code generation looks good, without errors. When trying to import, I end up with:

"ImportError: cannot import name 'CommonFrame' from partially initialized module 'netex.netex_framework.netex_frames.netex_common_frame_version' (most likely due to a circular import) (/tmp/netex/netex/netex_framework/netex_frames/netex_common_frame_version.py)"

skinkie commented 3 years ago

With the "-ns" option, I was able to avoid many, but still required manual labor to strip things. Some hints are appreciated to get it working out of the box. I needed to remove a significant number of SIRI imports.

tefra commented 3 years ago

Unfortunately python is vulnerable to xsd circular imports, the --ns-struct is a workaround when types declared in the same namespace are spread into multiple files. Your case is even more complicated because two namespaces depend on each other.

Thankfully It's not a common practice, I was thinking at some point creating a mode that would write all classes into a single file... we could try that again :thinking:

I am open to any other suggestions, or solutions you have seen in any other library/language.

skinkie commented 3 years ago

@tefra thanks for your reply. While I am very content with JAXB, I have also experienced that it is the only package actually capable of providing an out of the box experience. PyXB is able to generate a binding (fast), but cannot handle the polymorphism, generateDS fails in different ways, both provided huge python bindings (77MB+ source code). Xsdata at this moment already feels like magic, it works after cutting some dependencies. At least you have a case now to test the code for...

tefra commented 3 years ago

I added the netex collection in the samples repo with an initial test suite, in order to monitor this suite more closely.

There are a few mypy issues that will be resolved in #466 but other than that bindings seems to work and validate correctly as well.

reidsneo commented 3 years ago

How did you manage to generate the netex model? I saw in the new sample repo has single model too instead separated item model I did manually as the makefile due but resulting an error message

xsdata xsd/NeTEx_publication.xsd --config .xsdata.xml

Traceback (most recent call last): File "c:\python38\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "c:\python38\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Python38\Scripts\xsdata.exe\__main__.py", line 7, in <module> File "c:\python38\lib\site-packages\xsdata\__main__.py", line 8, in main cli() File "c:\python38\lib\site-packages\click\core.py", line 829, in __call__ return self.main(*args, **kwargs) File "c:\python38\lib\site-packages\click\core.py", line 782, in main rv = self.invoke(ctx) File "c:\python38\lib\site-packages\click\core.py", line 1259, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "c:\python38\lib\site-packages\click\core.py", line 1066, in invoke return ctx.invoke(self.callback, **ctx.params) File "c:\python38\lib\site-packages\click\core.py", line 610, in invoke return callback(*args, **kwargs) File "c:\python38\lib\site-packages\xsdata\cli.py", line 172, in generate transformer.process(list(uris)) File "c:\python38\lib\site-packages\xsdata\codegen\transformer.py", line 63, in process self.process_classes() File "c:\python38\lib\site-packages\xsdata\codegen\transformer.py", line 126, in process_classes writer.write(classes) File "c:\python38\lib\site-packages\xsdata\codegen\writer.py", line 33, in write for result in self.generator.render(classes): File "c:\python38\lib\site-packages\xsdata\formats\dataclass\generator.py", line 57, in render source=self.render_module(resolver, cluster), File "c:\python38\lib\site-packages\xsdata\formats\dataclass\generator.py", line 90, in render_module output = self.render_classes(resolver.sorted_classes()) File "c:\python38\lib\site-packages\xsdata\formats\dataclass\generator.py", line 120, in render_classes return "\n\n\n".join(map(render_class, classes)) + "\n" File "c:\python38\lib\site-packages\xsdata\formats\dataclass\generator.py", line 115, in render_class obj=obj, docstring_style=config.output.docstring_style.name.lower() AttributeError: 'str' object has no attribute 'name'

skinkie commented 3 years ago

How did you manage to generate the netex model? I saw in the new sample repo has single model too instead separated item model I did manually as the makefile due but resulting an error message

Take a peak at the commit message, "--structure-style" is introduced as new option.

tefra commented 3 years ago

Hi @reidsneo

This feature is not released yet, you will need to install xsdata from the repo, judging from your traceback you are using the config from the sample repo that includes new options.

Other that, If you are using a config

<Config xmlns="http://pypi.org/project/xsdata" version="21.4">
  <Output maxLineLength="79">
    <Package>netex.models.generated</Package>
    <Format>dataclasses</Format>
    <Structure>single-package</Structure> <!-- set the structure to single package -->

Otherwise from the cli xdata schema.xsd --structure-style single-package --package models.generated

reidsneo commented 3 years ago

Thank you it seem I use older tool instead the latest through the version is same 21.4 Cloning from master manualy solved the issue thanks!