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

Add pydantic v2 as output model type #1391

Merged
merged 35 commits into from
Jul 3, 2023

Conversation

koxudaxi
Copy link
Owner

@koxudaxi koxudaxi commented Jun 29, 2023

Related Issues: #803

Closes: #1255

from document

What is the difference between pydantic v1 and v2 output model?

Summary

datamodel-code-generator supports Pydantic v1 and v2 as output model type.

Pydantic v2 is a major release with many breaking changes. See the migration guide for more information:
https://docs.pydantic.dev/2.0/migration/

What's changes in v2 output?

__root__ field (a.k.a Custom Root Types)

__root__ field (a.k.a Custom Root Types) is removed in pydantic v2.
The model is changed to RootModel

pydantic.Field

https://docs.pydantic.dev/2.0/migration/#changes-to-pydanticfield

  • const -> removed
  • min_items (use min_length instead)
  • max_items (use max_length instead)
  • unique_items -> removed and the list type will be replaced by typing.Set. this feature is discussed in Add unique constraint to list? pydantic/pydantic-core#296
  • allow_mutation (use frozen instead)
  • regex (use pattern instead)

Model Config

  • pydantic.Config -> pydantic.ConfigDict
  • allow_mutation —> frozen (inverse value for getting same behavior).
  • allow_population_by_field_name → populate_by_name

@koxudaxi koxudaxi changed the title Add pydantic_v2 model module Add pydantic v2 as output model type Jun 29, 2023
@codecov
Copy link

codecov bot commented Jun 29, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (e9b6edf) 100.00% compared to head (5c82128) 100.00%.

Additional details and impacted files
@@             Coverage Diff             @@
##            master     #1391     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files           11        32     +21     
  Lines         1020      3538   +2518     
  Branches       201       832    +631     
===========================================
+ Hits          1020      3538   +2518     
Flag Coverage Δ
unittests 99.63% <99.57%> (?)

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

Impacted Files 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/improts.py 100.00% <100.00%> (ø)
... and 22 more

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

datamodel_code_generator/parser/base.py Fixed Show fixed Hide fixed
datamodel_code_generator/imports.py Fixed Show fixed Hide fixed
datamodel_code_generator/parser/base.py Fixed Show fixed Hide fixed
datamodel_code_generator/parser/base.py Fixed Show fixed Hide fixed
datamodel_code_generator/types.py Fixed Show fixed Hide fixed
@koxudaxi koxudaxi merged commit dfb601b into master Jul 3, 2023
73 checks passed
@koxudaxi koxudaxi deleted the add_pydantic_v2_as_output_model_type branch July 3, 2023 01:54
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.

Suport Pydantic v2 as --output-model-type
1 participant