question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated data has circular imports?

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
harshachcommented, May 23, 2021

Never mind! this is an issue on my schema side. Thanks

1reaction
koxudaxicommented, Mar 25, 2021

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

class TagReference(BaseModel):
    annotations: Optional[Dict[str, Annotations9]] = Field(
        None,
        description='Optional; if specified, annotations that are applied to images retrieved via ImageStreamTags.',
    )

I search the description from the schema.

"com.github.openshift.api.image.v1.TagReference":
  {"description":"TagReference specifies optional annotations for images using this tag and an optional reference to an ImageStreamTag, ImageStreamImage, or DockerImage this tag should track.",
   "type":"object",
   "required":["name"],
   "properties":
      {"annotations":
          {"description":
            "Optional; if specified, annotations that are applied to images retrieved via ImageStreamTags.",
             "type":"object",
             "additionalProperties":{"type":"string"}
             },

The code-generator detects the model as a modular model. "com.github.openshift.api.image.v1.TagReference": And, the model will be generated in in com.github.openshift.api.image.v1 But, a model (a.k.a Annotations9) will be generated as no-modular model on top-level. So, the relation causes the ImportError Annotations9 have to be generated in com.github.openshift.api.image.v1.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found