First of all, thank you for this project! It's been invaluable in a few of my libraries.
Is your feature request related to a problem? Please describe.
Per #1779 and similar issues, datamodel-codegen
currently produces unified oneOf
types using RootModel
as a base class, e.g.:
class Foo(RootModel[Union[Bar, Baz]]):
root: Union[Bar, Baz]
This is semantically correct and supported by Pydantic. However, as noted in https://github.com/pydantic/pydantic/discussions/7418, some typecheckers (like mypy
) struggle with this format.
For Python 3.8+, Pydantic recommends this form instead:
Foo = RootModel[Union[Bar, Baz]]
...which mypy
does understand, and is able to infer Foo(...)
's params/kwargs for.
Describe the solution you'd like
When --target-python-version
is 3.8
or newer, datamodel-codegen
should emit (or be able to be configured to emit) the RootModel[...]
annotation format, rather than the inheritance format.
Describe alternatives you've considered
I could just bushwack around this in my codebases by ignoring the mypy
errors. But it'd be nice to have a generalized fix upstream here π
Additional context
N/A
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