You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched Google & GitHub for similar requests and couldn't find anything
I have read and followed the docs and still think this feature is missing
Description
The Problem
Currently Pydantic does not supportTypeVar with more than one of a bound, default, or constraints declared. This is a problem because users are forced to choose between better usability or greater correctness. As described in the motivations for the PEP which added TypeVar defaults declaring both a bound a default is relatively common:
fromtyping_extensionsimportTypeVarT=TypeVar("T", bound=int, default=int)
deff(x: T=1) ->T:
returnxf("hello") # error because str not subtype of int
If we're forced to only declare a default we won't get appropriate type warnings:
fromtyping_extensionsimportTypeVarT=TypeVar("T", default=int)
deff(x: T=1) ->T:
returnxf("hello") # no error
Partial Solution
After inspecting the downstream code which handles bounds and defaults, it appears that it would relatively easy to at least support the case of a TypeVar with an identical bound and default declared. From my anecdotal experience, this likely covers the majority of cases where someone might want to specify both a bound and a default.
Supporting this would involve rewriting:
if (boundisnotNone) + (len(constraints) !=0) + (defaultisnotNone) >1:
raiseNotImplementedError(
'Pydantic does not support mixing more than one of TypeVar bounds, constraints and defaults'
)
ifdefaultisnotNone:
returnself.generate_schema(default)
elifconstraints:
returnself._union_schema(typing.Union[constraints]) # type: ignoreelifbound:
schema=self.generate_schema(bound)
schema['serialization'] =core_schema.wrap_serializer_function_ser_schema(
lambdax, h: h(x), schema=core_schema.any_schema()
)
returnschemaelse:
returncore_schema.any_schema()
Thanks for the feature request. I've added the label change here as well.
At a first glance, your approach sounds good. Feel free to open a PR with the changes, and I can do a more thorough review! We'll want to test this pretty thoroughly before we introduce the change.
Initial Checks
Description
The Problem
Currently Pydantic does not support
TypeVar
with more than one of a bound, default, or constraints declared. This is a problem because users are forced to choose between better usability or greater correctness. As described in the motivations for the PEP which addedTypeVar
defaults declaring both a bound a default is relatively common:If we're forced to only declare a
default
we won't get appropriate type warnings:Partial Solution
After inspecting the downstream code which handles bounds and defaults, it appears that it would relatively easy to at least support the case of a
TypeVar
with an identical bound and default declared. From my anecdotal experience, this likely covers the majority of cases where someone might want to specify both a bound and a default.Supporting this would involve rewriting:
To look more like:
The main question I have is how feasible it is to actually check
bound != default
. Besides that, this seems fairly sound.Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: