Skip to content

Commit

Permalink
Error more eagerly for proper subclasses of generics that we don't kn…
Browse files Browse the repository at this point in the history
…ow how to validate (#5629)
  • Loading branch information
dmontagu committed May 1, 2023
1 parent 34a1c02 commit 0346ddb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
9 changes: 7 additions & 2 deletions pydantic/_internal/_generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,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

0 comments on commit 0346ddb

Please sign in to comment.