-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Importing generated code results in a circular dependency #836
Comments
@gal-gocredd did you find a workaround for this? |
If someone has an idea to resolve the circular dependency structure then could please post it? |
I just wrote a post-processing script to get around this by moving the imports of other models to the bottom of the script before any calls to I've still got some testing to do, but it at least allows me to import modules with circular imports without errors. It did require me to "trick" dataclass-codegen into putting each model in its own file to reliably move all of the imports before that method call. I would propose:
Things I still need to look into before I confidently declare this a viable solution:
|
@noddycode would you care to share your post-processing script? Your quick mockup link has expired. Unfortunately running into the same issue here after generating models based off the ConnectWise OpenAPI spec. |
Unfortunately the code is proprietary, but I can write up a summary of what it does later today. In the meantime, one thing I would/will eventually change about my script is using the TYPE_CHECKING boolean along with annotations from future instead of moving imports to the bottom. See here: https://stackoverflow.com/a/72667915 Given some time in the next few weeks, I'd like to try and contribute something that does this during generation. |
Hey, I'm trying to the create models for ms adaptive cards (schema here) since the python sdk is not available yet. It causes circular imports for this schema too. Any solutions for this? I ended up creating a thin wrapper by hand for models which I use. |
I might have another test case (feel free to create another issue if needed).
The generated code has clear circular dependencies. |
- Resolve circular imports (koxudaxi/datamodel-code-generator#836) - Use kw_only=True (koxudaxi/datamodel-code-generator#1919)
Describe the bug
I'm trying to use
datamodel-codegen
to generate Pydantic models from Stripe's openapi. The generation itself succeeds but trying to import the generated package results in an import error due to circular dependency:It looks like the generation puts every model that includes a
.
for exampleissuing.authorization
in its own file, in this exampleissuing.py
, and every model that doesn't - in the__init__.py
file, resulting in some models from the init trying to import files from other files, and those other files trying to import from the init.Trying to generate everything into one file fails with
Modular references require an output directory, not a file
.To Reproduce
Example schema:
I tried using both the
.yaml
and.json
just in case, both result in the same error.Used commandline:
Expected behavior
I'd expect to be able to import the generated code
Version:
The text was updated successfully, but these errors were encountered: