Skip to content

Commit 0af18c4

Browse files
Add invalid schema placeholder (#1469)
1 parent e3eff5c commit 0af18c4

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

python/pydantic_core/core_schema.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,26 @@ def model_ser_schema(cls: Type[Any], schema: CoreSchema) -> ModelSerSchema:
435435
]
436436

437437

438+
class InvalidSchema(TypedDict, total=False):
439+
type: Required[Literal['invalid']]
440+
ref: str
441+
metadata: Dict[str, Any]
442+
443+
444+
def invalid_schema(ref: str | None = None, metadata: Dict[str, Any] | None = None) -> InvalidSchema:
445+
"""
446+
Returns an invalid schema, used to indicate that a schema is invalid.
447+
448+
Returns a schema that matches any value, e.g.:
449+
450+
Args:
451+
ref: optional unique identifier of the schema, used to reference the schema in other places
452+
metadata: Any other information you want to include with the schema, not used by pydantic-core
453+
"""
454+
455+
return _dict_not_none(type='invalid', ref=ref, metadata=metadata)
456+
457+
438458
class ComputedField(TypedDict, total=False):
439459
type: Required[Literal['computed-field']]
440460
property_name: Required[str]
@@ -3826,6 +3846,7 @@ def definition_reference_schema(
38263846
# union which kills performance not just for pydantic, but even for code using pydantic
38273847
if not MYPY:
38283848
CoreSchema = Union[
3849+
InvalidSchema,
38293850
AnySchema,
38303851
NoneSchema,
38313852
BoolSchema,
@@ -3882,6 +3903,7 @@ def definition_reference_schema(
38823903

38833904
# to update this, call `pytest -k test_core_schema_type_literal` and copy the output
38843905
CoreSchemaType = Literal[
3906+
'invalid',
38853907
'any',
38863908
'none',
38873909
'bool',

src/validators/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ macro_rules! validator_match {
479479
$(
480480
<$validator>::EXPECTED_TYPE => build_specific_validator::<$validator>($type, $dict, $config, $definitions),
481481
)+
482+
"invalid" => return py_schema_err!("Cannot construct schema with `InvalidSchema` member."),
482483
_ => return py_schema_err!(r#"Unknown schema type: "{}""#, $type),
483484
}
484485
};

tests/test_schema_functions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ def args(*args, **kwargs):
291291
(core_schema.decimal_schema, args(), {'type': 'decimal'}),
292292
(core_schema.decimal_schema, args(multiple_of=5, gt=1.2), {'type': 'decimal', 'multiple_of': 5, 'gt': 1.2}),
293293
(core_schema.complex_schema, args(), {'type': 'complex'}),
294+
(core_schema.invalid_schema, args(), {'type': 'invalid'}),
294295
]
295296

296297

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

305306
v = SchemaValidator(schema)
@@ -354,3 +355,8 @@ def test_expected_serialization_types(return_schema):
354355
)
355356
)
356357
)
358+
359+
360+
def test_err_on_invalid() -> None:
361+
with pytest.raises(SchemaError, match='Cannot construct schema with `InvalidSchema` member.'):
362+
SchemaValidator(core_schema.invalid_schema())

0 commit comments

Comments
 (0)