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

Error more eagerly for proper subclasses of generics that we don't know how to validate #5629

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions pydantic/_internal/_generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,13 @@ def _generate_schema(self, obj: Any) -> core_schema.CoreSchema: # noqa: C901

return ordered_dict_schema(self, obj)
elif issubclass(origin, typing.Sequence):
# Because typing.Sequence does not have a specified `__init__` signature, we don't validate into subclasses
return self._sequence_schema(obj)
if origin in {typing.Sequence, collections.abc.Sequence}:
return self._sequence_schema(obj)
# TODO: similarly handle other generic subclasses (like Iterable, etc.) where there's no standard __init__
raise PydanticSchemaGenerationError(
'Unable to generate pydantic-core schema for custom subclasses of Sequence.'
' Please define `__get_pydantic_core_schema__`. TODO: Add docs link.'
)
elif issubclass(origin, typing.MutableSet):
raise PydanticSchemaGenerationError('Unable to generate pydantic-core schema MutableSet TODO.')
elif issubclass(origin, (typing.Iterable, collections.abc.Iterable)):
Expand Down
26 changes: 19 additions & 7 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
Field,
Json,
PositiveInt,
PydanticSchemaGenerationError,
PydanticUserError,
ValidationError,
ValidationInfo,
Expand Down Expand Up @@ -1130,9 +1131,6 @@ class CustomMapping(Mapping[KT, VT]):
class CustomOrderedDict(OrderedDict[KT, VT]):
pass

class CustomSequence(Sequence[T]):
pass

class CustomSet(Set[T]):
pass

Expand All @@ -1149,7 +1147,6 @@ class Model(BaseModel, Generic[T, KT, VT]):
list_field: CustomList[T]
mapping_field: CustomMapping[KT, VT]
ordered_dict_field: CustomOrderedDict[KT, VT]
sequence_field: CustomSequence[T]
set_field: CustomSet[T]
tuple_field: CustomTuple[T]

Expand All @@ -1167,7 +1164,6 @@ class Model(BaseModel, Generic[T, KT, VT]):
list_field=[True, False],
mapping_field={'a': 2},
ordered_dict_field=OrderedDict([('a', 1)]),
sequence_field=[True, False],
set_field={True, False},
tuple_field=(True,),
)
Expand All @@ -1183,7 +1179,6 @@ class Model(BaseModel, Generic[T, KT, VT]):
assert type(m.list_field) is CustomList
assert type(m.mapping_field) is dict
assert type(m.ordered_dict_field) is OrderedDict.__origin__
assert type(m.sequence_field) is list
assert type(m.set_field) is CustomSet
assert type(m.tuple_field) is tuple

Expand All @@ -1200,12 +1195,29 @@ class Model(BaseModel, Generic[T, KT, VT]):
'list_field': [True, False],
'mapping_field': {'a': 2},
'ordered_dict_field': {'a': 1},
'sequence_field': [True, False],
'set_field': {False, True},
'tuple_field': (True,),
}


def test_custom_sequence_behavior():
T = TypeVar('T')

class CustomSequence(Sequence[T]):
pass

with pytest.raises(
PydanticSchemaGenerationError,
match=(
'Unable to generate pydantic-core schema for custom subclasses of Sequence.'
' Please define `__get_pydantic_core_schema__`.'
),
):

class Model(BaseModel, Generic[T]):
x: CustomSequence[T]


def test_replace_types_identity_on_unchanged():
T = TypeVar('T')
U = TypeVar('U')
Expand Down