Skip to content
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

Fix Pydantic V2 RootModel inheritance #1477

Merged
merged 5 commits into from
Aug 8, 2023

Conversation

barreeeiroo
Copy link
Contributor

@barreeeiroo barreeeiroo commented Aug 6, 2023

In Pydantic V2, root models are defined by inheriting RootModel class and defining the root field. However, in Pydantic V1, root models were defined by inheriting the same BaseModel and declaring a __root__ field.

Developers can set the base_class parameter to override the inherited class in the generated models. For Pydantic V1, this works fine in all cases as both Normal Models and Root Models were inheriting the same class. But for Pydantic V2 Root Models need to inherit a different class, so having the same base_class for both Normal Models and Root Models no longer works (as Root Models will be detected as Normal Models with a literal root field).

Considering how Enum's are being generated (by ignoring base_class), it seems like the best approach is the same one: for Pydantic V2 Root Models, ignore if base_class is set and always inherit RootModel. This PR applies this change for Pydantic V2 Root Models only.

Fixes #1435 (kinda, see #1435 (comment))

barreeeiroo and others added 2 commits August 6, 2023 18:22
In Pydantic V2, root models are defined by inheriting RootModel class and defining the root field.
@codecov
Copy link

codecov bot commented Aug 6, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (e9b6edf) 100.00% compared to head (d8d3b13) 100.00%.
Report is 688 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #1477     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files           11        32     +21     
  Lines         1020      3569   +2549     
  Branches       201       838    +637     
===========================================
+ Hits          1020      3569   +2549     
Flag Coverage Δ
unittests 99.63% <99.58%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
datamodel_code_generator/__init__.py 100.00% <100.00%> (ø)
datamodel_code_generator/__main__.py 100.00% <100.00%> (ø)
datamodel_code_generator/format.py 100.00% <100.00%> (ø)
datamodel_code_generator/http.py 100.00% <100.00%> (ø)
datamodel_code_generator/imports.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/__init__.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/base.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/dataclass.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/enum.py 100.00% <100.00%> (ø)
datamodel_code_generator/model/imports.py 100.00% <100.00%> (ø)
... and 22 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

TypedDict is not available on pre-3.8, so changing this to Any to avoid issues...
@barreeeiroo
Copy link
Contributor Author

barreeeiroo commented Aug 8, 2023

Well, now all the tests are passing but the Codecov step is failing because the GitHub Actions tests run before code coverage was reported and a 404 error was raised from the test action.

@koxudaxi
Copy link
Owner

koxudaxi commented Aug 8, 2023

I have cleared CI chache and re-invoke actions.

@koxudaxi koxudaxi merged commit a721166 into koxudaxi:master Aug 8, 2023
73 checks passed
@koxudaxi
Copy link
Owner

koxudaxi commented Aug 8, 2023

Thank you very much!!

@barreeeiroo
Copy link
Contributor Author

Thank you very much!!

You're welcome! Thank you very much for this amazing tool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong parent class for pydantic V2 root models
2 participants