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
Improve mypy
hinting on RootModel
with oneOf
#1903
Comments
For a concrete example of this failing, see this CI job. This happens because of a generated type that looks like this: class DsseSchema(RootModel[Union[DsseV001Schema1, DsseV001Schema2]]):
model_config = ConfigDict(
populate_by_name=True,
)
root: Union[DsseV001Schema1, DsseV001Schema2] = Field(
...,
description="log entry schema for dsse envelopes",
title="DSSE Schema",
) ...which then gets instantiated like this: proposed_entry = rekor_types.Dsse(
spec=rekor_types.dsse.DsseSchema(
proposed_content=rekor_types.dsse.ProposedContent(
envelope=content.to_json(),
verifiers=[b64_cert.decode()],
),
),
) ...which fails, since |
@woodruffw I would like to know, if you know, if your solution is version-constrained on the Pydantic side? |
Thanks for your response @koxudaxi! #1921 looks like a different issue -- in my case the generated code is syntactically and semantically sound, but just doesn't work with the patterns
Yes, I believe it can be used with anything in 2.x! |
Thanks for the bug report @woodruffw - I am also running into this issue. |
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 unifiedoneOf
types usingRootModel
as a base class, e.g.:This is semantically correct and supported by Pydantic. However, as noted in pydantic/pydantic#7418, some typecheckers (like
mypy
) struggle with this format.For Python 3.8+, Pydantic recommends this form instead:
...which
mypy
does understand, and is able to inferFoo(...)
's params/kwargs for.Describe the solution you'd like
When
--target-python-version
is3.8
or newer,datamodel-codegen
should emit (or be able to be configured to emit) theRootModel[...]
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
The text was updated successfully, but these errors were encountered: