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

Make detection of TypeVar defaults robust to the CPython PEP-696 implementation #9426

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pydantic/_internal/_generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,15 +1660,20 @@ def _unsubstituted_typevar_schema(self, typevar: typing.TypeVar) -> core_schema.

bound = typevar.__bound__
constraints = typevar.__constraints__
default = getattr(typevar, '__default__', None)

if (bound is not None) + (len(constraints) != 0) + (default is not None) > 1:
try:
typevar_has_default = typevar.has_default() # type: ignore
except AttributeError:
# could still have a default if it's an old version of typing_extensions.TypeVar
typevar_has_default = getattr(typevar, '__default__', None) is not None

if (bound is not None) + (len(constraints) != 0) + typevar_has_default > 1:
raise NotImplementedError(
'Pydantic does not support mixing more than one of TypeVar bounds, constraints and defaults'
)

if default is not None:
return self.generate_schema(default)
if typevar_has_default:
return self.generate_schema(typevar.__default__) # type: ignore
elif constraints:
return self._union_schema(typing.Union[constraints]) # type: ignore
elif bound:
Expand Down
Loading