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 from_attributes to ConfigDict #1946

Merged
merged 2 commits into from
May 30, 2024

Conversation

michael2to3
Copy link
Contributor

Added an option to set the from_attributes in PydanticV2 models through the --extra-template-data. This lets you automatically add ConfigDict with from_attributes to your templates, making it easier to create models that work well with ORM attributes

datamodel-codegen --output-model-type pydantic_v2.BaseModel \
    --extra-template-data ./extra.json \
    --input-file-type jsonschema \
    --input ./schema.json --output ./tmp.py

extra.json:

{
  "#all#": {
    "config": {
      "from_attributes": "True"
    }
  }
}

tmp.py:

# generated by datamodel-codegen:
#   filename:  schema.json
#   timestamp: 2024-05-05T11:18:15+00:00

from __future__ import annotations

from typing import Any

from pydantic import BaseModel, ConfigDict, RootModel


class Model(RootModel[Any]):
    model_config = ConfigDict(
        from_attributes=True,
    )
    root: Any


class Example(BaseModel):
    model_config = ConfigDict(
        from_attributes=True,
    )
    value: Any

Copy link

codecov bot commented May 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@f703dff). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1946   +/-   ##
=======================================
  Coverage        ?   98.90%           
=======================================
  Files           ?       37           
  Lines           ?     4183           
  Branches        ?      972           
=======================================
  Hits            ?     4137           
  Misses          ?       29           
  Partials        ?       17           
Flag Coverage Δ
unittests 98.56% <100.00%> (?)

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

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

Copy link

codspeed-hq bot commented May 30, 2024

CodSpeed Performance Report

Merging #1946 will not alter performance

Comparing michael2to3:feat/from_attribute_pydanticv2 (5fb160f) with main (f703dff)

Summary

✅ 29 untouched benchmarks

@koxudaxi koxudaxi added the documentation Improvements or additions to documentation label May 30, 2024
@koxudaxi koxudaxi merged commit decc493 into koxudaxi:main May 30, 2024
92 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants