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

Circular imports? #469

Closed
skinkie opened this issue Apr 25, 2021 · 8 comments · Fixed by #470
Closed

Circular imports? #469

skinkie opened this issue Apr 25, 2021 · 8 comments · Fixed by #470

Comments

@skinkie
Copy link
Contributor

skinkie commented Apr 25, 2021

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
Copy link
Contributor Author

skinkie commented Apr 25, 2021

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
Copy link
Owner

tefra commented Apr 26, 2021

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 🤔

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

@skinkie
Copy link
Contributor Author

skinkie commented Apr 26, 2021

@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
Copy link
Owner

tefra commented May 3, 2021

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.

@tefra tefra closed this as completed May 3, 2021
@reidsneo
Copy link

reidsneo commented May 4, 2021

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
Copy link
Contributor Author

skinkie commented May 4, 2021

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
Copy link
Owner

tefra commented May 4, 2021

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
Copy link

reidsneo commented May 5, 2021

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!

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

Successfully merging a pull request may close this issue.

3 participants