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 dependency while generating from OGC WFS XSD #941

Closed
eLvErDe opened this issue Feb 15, 2024 · 3 comments · Fixed by #999
Closed

Circular dependency while generating from OGC WFS XSD #941

eLvErDe opened this issue Feb 15, 2024 · 3 comments · Fixed by #999

Comments

@eLvErDe
Copy link

eLvErDe commented Feb 15, 2024

Hello,

Models generated from OGC WFS XSD models cannot be used due to circular import:

xsdata generate https://raw.githubusercontent.com/highsource/ogc-schemas/master/schemas/src/main/resources/ogc/wfs/2.0/wfs.xsd
>>> from generated import WfsCapabilitiesType
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/generated/__init__.py", line 1, in <module>
    from generated.wfs import (
  File "/tmp/generated/wfs.py", line 6, in <module>
    from generated.filter.pkg_2.pkg_0.filter import (
  File "/tmp/generated/filter/pkg_2/pkg_0/__init__.py", line 8, in <module>
    from generated.filter.pkg_2.pkg_0.filter import (
  File "/tmp//generated/filter/pkg_2/pkg_0/filter.py", line 9, in <module>
    from generated.filter.pkg_2.pkg_0.query import AbstractSelectionClauseType
  File "/tmp/generated/filter/pkg_2/pkg_0/query.py", line 3, in <module>
    from generated.filter.pkg_2.pkg_0.filter import Filter
ImportError: cannot import name 'Filter' from partially initialized module 'generated.filter.pkg_2.pkg_0.filter' (most likely due to a circular import) (/tmp/generated/filter/pkg_2/pkg_0/filter.py)

Is that a bug ?

Best regards, Adam.

@tefra
Copy link
Owner

tefra commented Feb 16, 2024

Circular includes in schemas are very common which leads to circular import errors in python. There is an option though to change the file structure output. See here

The default value is to follow the filenames of the schema, which is not safe for circular imports. The safest value is clusters and single page.

xsdata generate https://raw.githubusercontent.com/highsource/ogc-schemas/master/schemas/src/main/resources/ogc/wfs/2.0/wfs.xsd -ss clusters
========= xsdata v24.1 / Python 3.11.4+ / Platform linux =========

Parsing schema https://raw.githubusercontent.com/highsource/ogc-schemas/master/schemas/src/main/resources/ogc/wfs/2.0/wfs.xsd
Parsing schema file:///home/chris/.pyenv/versions/3.11-dev/envs/xsdata/lib/python3.11/site-packages/xsdata/schemas/xml.xsd
Compiling schema file:///home/chris/.pyenv/versions/3.11-dev/envs/xsdata/lib/python3.11/site-packages/xsdata/schemas/xml.xsd
Builder: 5 main and 2 inner classes
Parsing schema file:///home/chris/.pyenv/versions/3.11-dev/envs/xsdata/lib/python3.11/site-packages/xsdata/schemas/xlink.xsd
...
...
Analyzer input: 380 main and 13 inner classes
Analyzer output: 302 main and 4 inner classes
Generating package: init
Generating package: generated.type_type
Generating package: generated.show_type
Generating package: generated.actuate_type
Generating package: generated.simple
Generating package: generated.extended
Generating package: generated.title_elt_type
Generating package: generated.resource_type
Generating package: generated.locator_type
.....python
Python 3.11.4+ (heads/3.11:ff5dd9d, Jul 30 2023, 17:40:33) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from generated import WfsCapabilitiesType
>>>

I will leave this one open, because I want to check if we can add a validation error to warn users

@tefra
Copy link
Owner

tefra commented Mar 10, 2024

Not even mypy can detect the circular imports for the the given schema...

@tefra
Copy link
Owner

tefra commented Mar 25, 2024

The cli will still write all generated packages and run the validation at the end, if it fails you will get something like this, to let you know how to proceed

=========
Error: Circular Dependencies Found
help: Try a different structure style and/or enable unnest classes. Cleanup previously generated packages and modules.

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.

2 participants