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
Conversation
Deploying with Cloudflare Pages
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
tests/test_generics.py
Outdated
def test_serialize_unsubstituted_typevars_default() -> None: | ||
from typing_extensions import TypeVar | ||
|
||
class ErrorDetails(BaseModel): | ||
foo: str | ||
|
||
DataT = TypeVar('DataT', default=ErrorDetails) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice example 👍
docs/concepts/models.md
Outdated
|
||
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`: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
pydantic/types.py
Outdated
from pydantic import BaseModel, UUID3 | ||
from pydantic import UUID3, BaseModel |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay changed
Fixes #7562