Skip to content

Add a WalkCoreSchema #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions python/pydantic_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
TzInfo,
Url,
ValidationError,
WalkCoreSchema,
WalkCoreSchemaFilterBuilder,
__version__,
from_json,
to_json,
Expand Down Expand Up @@ -67,6 +69,8 @@
'from_json',
'to_jsonable_python',
'validate_core_schema',
'WalkCoreSchema',
'WalkCoreSchemaFilterBuilder',
]


Expand Down
28 changes: 27 additions & 1 deletion python/pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sys
from typing import Any, Callable, Generic, Optional, Type, TypeVar

from pydantic_core import ErrorDetails, ErrorTypeInfo, InitErrorDetails, MultiHostHost
from pydantic_core.core_schema import CoreConfig, CoreSchema, ErrorType
from pydantic_core.core_schema import CoreConfig, CoreSchema, ErrorType, SerSchema

if sys.version_info < (3, 8):
from typing_extensions import final
Expand Down Expand Up @@ -46,6 +46,8 @@ __all__ = [
'list_all_errors',
'TzInfo',
'validate_core_schema',
'WalkCoreSchema',
'WalkCoreSchemaFilterBuilder',
]
__version__: str
build_profile: str
Expand Down Expand Up @@ -864,3 +866,27 @@ def validate_core_schema(schema: CoreSchema, *, strict: bool | None = None) -> C
We may also remove this function altogether, do not rely on it being present if you are
using pydantic-core directly.
"""

class _WalkCoreSchemaFilter(Generic[_T]):
pass

@final
class WalkCoreSchemaFilterBuilder(Generic[_T]):
def __and__(self, other: WalkCoreSchemaFilterBuilder) -> WalkCoreSchemaFilterBuilder: ...
def __or__(self, other: WalkCoreSchemaFilterBuilder) -> WalkCoreSchemaFilterBuilder: ...
@staticmethod
def has_ref() -> WalkCoreSchemaFilterBuilder: ...
@staticmethod
def has_type(type: str) -> WalkCoreSchemaFilterBuilder: ...
@staticmethod
def predicate(predicate: Callable[[_T], bool]) -> WalkCoreSchemaFilterBuilder: ...
def build(self, func: Callable[[_T, Callable[[_T], _T]], _T]) -> _WalkCoreSchemaFilter[_T]: ...

@final
class WalkCoreSchema:
def __init__(
self,
visit_core_schema: _WalkCoreSchemaFilter[CoreSchema] | None = None,
visit_ser_schema: _WalkCoreSchemaFilter[SerSchema] | None = None,
) -> None: ...
def walk(self, schema: CoreSchema) -> CoreSchema: ...
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod serializers;
mod tools;
mod url;
mod validators;
mod walk_core_schema;

// required for benchmarks
pub use self::input::TzInfo;
Expand Down Expand Up @@ -111,5 +112,7 @@ fn _pydantic_core(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(to_jsonable_python, m)?)?;
m.add_function(wrap_pyfunction!(list_all_errors, m)?)?;
m.add_function(wrap_pyfunction!(validate_core_schema, m)?)?;
m.add_class::<walk_core_schema::WalkCoreSchema>()?;
m.add_class::<walk_core_schema::WalkCoreSchemaFilterBuilder>()?;
Ok(())
}
Loading