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

Serialize unsubstituted type vars as Any #7606

Merged
merged 5 commits into from Sep 25, 2023

Conversation

adriangb
Copy link
Member

Fixes #7562

@cloudflare-pages
Copy link

cloudflare-pages bot commented Sep 25, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8aa4039
Status: ✅  Deploy successful!
Preview URL: https://24a51c27.pydantic-docs2.pages.dev
Branch Preview URL: https://serialize-unsubstituted-type.pydantic-docs2.pages.dev

View logs

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@sydney-runkle sydney-runkle added the relnotes-fix Used for bugfixes. label Sep 25, 2023
Comment on lines 2661 to 2667
def test_serialize_unsubstituted_typevars_default() -> None:
from typing_extensions import TypeVar

class ErrorDetails(BaseModel):
foo: str

DataT = TypeVar('DataT', default=ErrorDetails)
Copy link
Member

Choose a reason for hiding this comment

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

Nice example 👍


If you use a `default=...` for a `TypeVar` (available in Python >= 3.13 or via `typing-extensions`) the default value will be used for both validation and serialization if the type variable is not parametrized. You can override this behavior using `pydantic.SerializeAsAny`:
Copy link
Member Author

Choose a reason for hiding this comment

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

@dmontagu I think this is perhaps the controversial part of this PR. My reasoning was that a bound and default are different semantically and this distinction led me to implement other behavior. This is also an opportunity to preserve the SerializeAsAny example that was here before. Let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with your interpretation of the semantics. (And in particular, this choice.)

Comment on lines 1097 to 1098
from pydantic import BaseModel, UUID3
from pydantic import UUID3, BaseModel
Copy link
Member Author

Choose a reason for hiding this comment

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

I reverted this change but looks like something in our docstring formatting stuff is sorting in reverse

elif typevar.__constraints__:
return self._union_schema(typing.Union[typevar.__constraints__]) # type: ignore
schema = self._union_schema(typing.Union[typevar.__constraints__]) # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

Constraints feel closer to default than to a bound to me, I would personally make them function the old way. I’ll leave it up to you though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay changed

@adriangb adriangb enabled auto-merge (squash) September 25, 2023 15:19
@adriangb adriangb merged commit 095ff12 into main Sep 25, 2023
58 checks passed
@adriangb adriangb deleted the serialize-unsubstituted-typevars-as-any branch September 25, 2023 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Instances of generic types with bound=BaseModel are not serialised
3 participants