Skip to content

Commit 3e63e0b

Browse files
Improve res type
1 parent dbd22f0 commit 3e63e0b

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

python/pydantic_core/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import sys as _sys
44
from typing import Any as _Any
5-
from typing import NamedTuple
65

76
from ._pydantic_core import (
87
ArgsKwargs,
@@ -142,7 +141,9 @@ class MultiHostHost(_TypedDict):
142141
"""The port part of this host, or `None`."""
143142

144143

145-
class GatherResult(NamedTuple):
144+
class GatherResult(_TypedDict):
145+
"""Internal result of gathering schemas for cleaning."""
146+
146147
definition_refs: dict[str, list[DefinitionReferenceSchema]]
147148
recursive_refs: set[str]
148149
deferred_discriminators: list[tuple[CoreSchema, _Any]]

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ pub use build_tools::SchemaError;
3636
pub use errors::{
3737
list_all_errors, PydanticCustomError, PydanticKnownError, PydanticOmit, PydanticUseDefault, ValidationError,
3838
};
39+
pub use schema_traverse::gather_schemas_for_cleaning;
3940
pub use serializers::{
4041
to_json, to_jsonable_python, PydanticSerializationError, PydanticSerializationUnexpectedValue, SchemaSerializer,
4142
WarningsArg,
4243
};
43-
pub use validators::{gather_schemas_for_cleaning, validate_core_schema, PySome, SchemaValidator};
44+
pub use validators::{validate_core_schema, PySome, SchemaValidator};
4445

4546
use crate::input::Input;
4647

src/schema_traverse.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,25 @@ impl<'a, 'py> GatherCtx<'a, 'py> {
202202
}
203203
}
204204

205-
pub fn gather_schema(schema: &Bound<'_, PyDict>, ctx: &mut GatherCtx) -> PyResult<()> {
205+
fn gather_schema(schema: &Bound<'_, PyDict>, ctx: &mut GatherCtx) -> PyResult<()> {
206206
traverse_schema(schema, ctx)?;
207207
gather_node(schema, ctx)
208208
}
209+
210+
#[pyfunction(signature = (schema, definitions))]
211+
pub fn gather_schemas_for_cleaning<'py>(
212+
schema: &Bound<'py, PyAny>,
213+
definitions: &Bound<'py, PyAny>,
214+
) -> PyResult<Bound<'py, PyDict>> {
215+
let py = schema.py();
216+
let schema_dict = schema.downcast_exact::<PyDict>()?;
217+
218+
let mut ctx = GatherCtx::new(definitions.downcast_exact()?)?;
219+
gather_schema(schema_dict, &mut ctx)?;
220+
221+
let res = PyDict::new_bound(py);
222+
res.set_item(intern!(py, "definition_refs"), ctx.def_refs)?;
223+
res.set_item(intern!(py, "recursive_refs"), ctx.recursive_def_refs)?;
224+
res.set_item(intern!(py, "deferred_discriminators"), ctx.discriminators)?;
225+
Ok(res)
226+
}

src/validators/mod.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ mod validation_state;
6464
mod with_default;
6565

6666
pub use self::validation_state::{Exactness, ValidationState};
67-
use crate::schema_traverse::{gather_schema, GatherCtx};
6867
pub use with_default::DefaultType;
6968

7069
#[pyclass(module = "pydantic_core._pydantic_core", name = "Some")]
@@ -444,25 +443,6 @@ impl<'py> SelfValidator<'py> {
444443
}
445444
}
446445

447-
#[pyfunction(signature = (schema, definitions))]
448-
pub fn gather_schemas_for_cleaning<'py>(
449-
schema: &Bound<'py, PyAny>,
450-
definitions: &Bound<'py, PyAny>,
451-
) -> PyResult<Bound<'py, PyTuple>> {
452-
let py = schema.py();
453-
let schema_dict = schema.downcast_exact::<PyDict>()?;
454-
455-
let mut ctx = GatherCtx::new(definitions.downcast_exact()?)?;
456-
gather_schema(schema_dict, &mut ctx)?;
457-
458-
let res = vec![
459-
ctx.def_refs.as_any(),
460-
ctx.recursive_def_refs.as_any(),
461-
ctx.discriminators.as_any(),
462-
];
463-
return Ok(PyTuple::new_bound(py, res));
464-
}
465-
466446
#[pyfunction(signature = (schema, *, strict = None))]
467447
pub fn validate_core_schema<'py>(schema: &Bound<'py, PyAny>, strict: Option<bool>) -> PyResult<Bound<'py, PyAny>> {
468448
let self_validator = SelfValidator::new(schema.py())?;

0 commit comments

Comments
 (0)