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:
In [1]: import stripe_api
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import stripe_api
File .../stripe_api/__init__.py:13, in <module>
9 from typing import Any, Dict, List, Optional, Union
11 from pydantic import BaseModel, Field, constr
---> 13 from . import issuing, test_helpers, treasury
16 class BusinessType(Enum):
17 company = 'company'
File .../stripe_api/issuing.py:12, in <module>
8 from typing import Dict, List, Optional, Union
10 from pydantic import BaseModel, Field, constr
---> 12 from . import (
13 BalanceTransaction,
14 IssuingAuthorizationAmountDetails,
15 IssuingAuthorizationMerchantData,
16 IssuingAuthorizationPendingRequest,
17 IssuingAuthorizationRequest,
18 IssuingAuthorizationTreasury,
19 IssuingAuthorizationVerificationData,
20 IssuingCardAuthorizationControls,
21 IssuingCardholderAddress,
22 IssuingCardholderAuthorizationControls,
23 IssuingCardholderCompany,
24 IssuingCardholderIndividual,
25 IssuingCardholderRequirements,
26 IssuingCardShipping,
27 IssuingCardWallets,
28 IssuingDisputeEvidence,
29 IssuingDisputeTreasury,
30 IssuingTransactionAmountDetails,
31 IssuingTransactionPurchaseDetails,
32 IssuingTransactionTreasury,
33 )
36 class AuthorizationMethod(Enum):
37 chip = 'chip'
ImportError: cannot import name 'BalanceTransaction' from partially initialized module 'stripe_api' (most likely due to a circular import) (.../stripe_api/__init__.py)
It looks like the generation puts every model that includes a .
for example issuing.authorization
in its own file, in this example issuing.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:
$ datamodel-codegen --input stripe-openapi/openapi/spec3.yaml --output stripe_api
Expected behavior
I'd expect to be able to import the generated code
Version:
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too