Skip to content

Commit

Permalink
Add invalid schema placeholder (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle authored Oct 7, 2024
1 parent e3eff5c commit 0af18c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
22 changes: 22 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 @@ -3826,6 +3846,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 +3903,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
1 change: 1 addition & 0 deletions src/validators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ macro_rules! validator_match {
$(
<$validator>::EXPECTED_TYPE => build_specific_validator::<$validator>($type, $dict, $config, $definitions),
)+
"invalid" => return py_schema_err!("Cannot construct schema with `InvalidSchema` member."),
_ => return py_schema_err!(r#"Unknown schema type: "{}""#, $type),
}
};
Expand Down
8 changes: 7 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 Expand Up @@ -354,3 +355,8 @@ def test_expected_serialization_types(return_schema):
)
)
)


def test_err_on_invalid() -> None:
with pytest.raises(SchemaError, match='Cannot construct schema with `InvalidSchema` member.'):
SchemaValidator(core_schema.invalid_schema())

0 comments on commit 0af18c4

Please sign in to comment.