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

Support for serialize_as_any runtime setting #8830

Merged
merged 10 commits into from Mar 25, 2024
Merged

Conversation

sydney-runkle
Copy link
Member

@sydney-runkle sydney-runkle commented Feb 15, 2024

Closes #6423
Counterpart PR: pydantic/pydantic-core#1194

Requires new pydantic-core release in order to support the new runtime flag 👍. Leaving WIP tag until we release a new pydantic-core version with the necessary changes!

TODO:

  • Add support for dataclasses, typed dicts
  • Add support for TypeAdapter
  • Experiment with how to set this at the project level
  • Add a what is duck typing section to the docs
  • Add more recursive / nested model testing

Selected Reviewer: @davidhewitt

Copy link

cloudflare-pages bot commented Feb 15, 2024

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3183633
Status: ✅  Deploy successful!
Preview URL: https://0f66733c.pydantic-docs2.pages.dev
Branch Preview URL: https://ser-as-any-support.pydantic-docs2.pages.dev

View logs

Copy link

codspeed-hq bot commented Feb 15, 2024

CodSpeed Performance Report

Merging #8830 will not alter performance

Comparing ser_as_any_support (3183633) with main (df5be31)

Summary

✅ 10 untouched benchmarks

@sydney-runkle
Copy link
Member Author

Note, I wanted to experiment a bit with how a user of pydantic could configure the serialize_as_any default within his/her project. The following is one approach to this:

from pydantic import BaseModel, SecretStr

class MyBaseModel(BaseModel):
    def model_dump(self, **kwargs):
        # use super's model_dump, with serialize_as_any set to True by default
        return super().model_dump(serialize_as_any=True, **kwargs)
    
    def model_dump_json(self, **kwargs):
        # use super's model_dump_json, with serialize_as_any set to True by default
        return super().model_dump_json(serialize_as_any=True, **kwargs)


class User(BaseModel):
    name: str

class UserInfo(User):
    password: SecretStr

class OuterModel(MyBaseModel):
    user: User

u = OuterModel(user=UserInfo(name='John', password='secret_pw'))
print(u.model_dump_json())
#> {"user":{"name":"John","password":"**********"}}

@sydney-runkle sydney-runkle marked this pull request as ready for review February 23, 2024 14:28
@sydney-runkle
Copy link
Member Author

Please review

@sydney-runkle sydney-runkle changed the title WIP: Support for serialize_as_any config and runtime settings WIP: Support for serialize_as_any runtime setting Mar 12, 2024
Copy link
Contributor

@dmontagu dmontagu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good other than the docs suggestions (which you can take or leave), assuming the pydantic-core PR is good etc.

@sydney-runkle sydney-runkle enabled auto-merge (squash) March 25, 2024 21:13
@sydney-runkle sydney-runkle merged commit 33a275a into main Mar 25, 2024
53 checks passed
@sydney-runkle sydney-runkle deleted the ser_as_any_support branch March 25, 2024 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Config / runtime option for SerializeAsAny
3 participants