Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 23 additions & 0 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,26 @@ def model_ser_schema(cls: Type[Any], schema: CoreSchema) -> ModelSerSchema:
]


class InvalidSchema(TypedDict, total=False):
type: Required[Literal['invalid']]
ref: str
metadata: Dict[str, Any]


def invalid_schema(ref: str | None = None, metadata: Dict[str, Any] | None = None) -> InvalidSchema:
"""
Returns an invalid schema, used to indicate that a schema is invalid.

Returns a schema that matches any value, e.g.:

Args:
ref: optional unique identifier of the schema, used to reference the schema in other places
metadata: Any other information you want to include with the schema, not used by pydantic-core
"""

return _dict_not_none(type='invalid', ref=ref, metadata=metadata)


class ComputedField(TypedDict, total=False):
type: Required[Literal['computed-field']]
property_name: Required[str]
Expand Down Expand Up @@ -2458,6 +2478,7 @@ class UnionSchema(TypedDict, total=False):
type: Required[Literal['union']]
choices: Required[List[Union[CoreSchema, Tuple[CoreSchema, str]]]]
# default true, whether to automatically collapse unions with one element to the inner validator
tagged_union_tag: str
auto_collapse: bool
custom_error_type: str
custom_error_message: str
Expand Down Expand Up @@ -3826,6 +3847,7 @@ def definition_reference_schema(
# union which kills performance not just for pydantic, but even for code using pydantic
if not MYPY:
CoreSchema = Union[
InvalidSchema,
AnySchema,
NoneSchema,
BoolSchema,
Expand Down Expand Up @@ -3882,6 +3904,7 @@ def definition_reference_schema(

# to update this, call `pytest -k test_core_schema_type_literal` and copy the output
CoreSchemaType = Literal[
'invalid',
'any',
'none',
'bool',
Expand Down
3 changes: 2 additions & 1 deletion tests/test_schema_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def args(*args, **kwargs):
(core_schema.decimal_schema, args(), {'type': 'decimal'}),
(core_schema.decimal_schema, args(multiple_of=5, gt=1.2), {'type': 'decimal', 'multiple_of': 5, 'gt': 1.2}),
(core_schema.complex_schema, args(), {'type': 'complex'}),
(core_schema.invalid_schema, args(), {'type': 'invalid'}),
]


Expand All @@ -299,7 +300,7 @@ def test_schema_functions(function, args_kwargs, expected_schema):
args, kwargs = args_kwargs
schema = function(*args, **kwargs)
assert schema == expected_schema
if schema.get('type') in {None, 'definition-ref', 'typed-dict-field', 'model-field'}:
if schema.get('type') in {None, 'definition-ref', 'typed-dict-field', 'model-field', 'invalid'}:
return

v = SchemaValidator(schema)
Expand Down
Loading