Generated data has circular imports?
See original GitHub issueDescribe the bug
I’m trying to use datamodel-codegen
to produce Pydantic models from OpenShift resources. While the command completes successfully and generates a hierarchy of Python modules, attempting to import anything results in:
>>> import generated
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lars/projects/operate-first/acm-clusterdeployment-generator/generated/__init__.py", line 13, in <module>
from .com.github.openshift.api.image import v1
File "/home/lars/projects/operate-first/acm-clusterdeployment-generator/generated/com/github/openshift/api/image/v1.py", line 11, in <module>
from ...... import Annotations9, Blobs, Images, SignedClaims
ImportError: cannot import name 'Annotations9' from partially initialized module 'generated' (most likely due to a circular import) (/home/lars/projects/operate-first/acm-clusterdeployment-generator/generated/__init__.py)
To Reproduce
I’ve started with the OpenShift schema available from https://<your_openshift_server>/openapi/v2
. This produces a 17MB output file (here if you want to see it). I save this output to openshift.json
and then run:
datamodel-codegen --input openshift.json --input-file-type jsonschema --output generated/
Example schema:
Used commandline:
$ datamodel-codegen --input openshift.json --input-file-type jsonschema --output generated/
Expected behavior
I expect to be able to do something like:
from generated.io.k8s.api.core.v1 import Secret
Version:
- OS: Linux (fedora 33)
- Python version: 3.9.2
- datamodel-code-generator version: 0.9.4
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Avoiding Circular Imports in Python | by André Menck - Medium
The reason why circular imports in Python result in errors has been explained by others much more eloquently ... Not all Circular Imports...
Read more >Python Circular Imports - Stack Abuse
Circular importing is a form of circular dependency that is created with the import statement in Python. For example, let's analyze the ...
Read more >Python Circular Imports Module: Solving Circular Import Problem
Circular importing is a conceptual error that is formed due to circular dependency created with the import statement in our Python program. Python...
Read more >How to avoid circular imports in Python? - Stack Overflow
Circular import dependencies typically fall into two categories depending on what you're trying to import and where you're using it inside each module....
Read more >Python Circular Import Problem and Solutions
Tip: Circular import is only raised when you import object from module. It is not raised when you try to import module itself....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Never mind! this is an issue on my schema side. Thanks
Thank you for creating this issue. I have found a bug. This problem is related to the issue https://github.com/koxudaxi/datamodel-code-generator/issues/244 I will fix it.
deep dive into the problem
Annotations9 is referenced from
generated.com.github.openshift.api.image.TagReference
I search the description from the schema.
The code-generator detects the model as a modular model.
"com.github.openshift.api.image.v1.TagReference":
And, the model will be generated in incom.github.openshift.api.image.v1
But, a model (a.k.aAnnotations9
) will be generated as no-modular model on top-level. So, the relation causes theImportError
Annotations9
have to be generated incom.github.openshift.api.image.v1
.