X7ROOT File Manager
Current Path:
/opt/cloudlinux/venv/lib/python3.11/site-packages/pydantic/_internal
opt
/
cloudlinux
/
venv
/
lib
/
python3.11
/
site-packages
/
pydantic
/
_internal
/
ðŸ“
..
📄
__init__.py
(0 B)
ðŸ“
__pycache__
📄
_config.py
(11.04 KB)
📄
_core_metadata.py
(3.44 KB)
📄
_core_utils.py
(24.24 KB)
📄
_dataclasses.py
(10.46 KB)
📄
_decorators.py
(30.05 KB)
📄
_decorators_v1.py
(6.12 KB)
📄
_discriminated_union.py
(25.61 KB)
📄
_fields.py
(11.62 KB)
📄
_forward_ref.py
(425 B)
📄
_generate_schema.py
(87.89 KB)
📄
_generics.py
(21.65 KB)
📄
_internal_dataclass.py
(207 B)
📄
_known_annotated_metadata.py
(15.89 KB)
📄
_mock_val_ser.py
(4.2 KB)
📄
_model_construction.py
(26.41 KB)
📄
_repr.py
(4.27 KB)
📄
_schema_generation_shared.py
(4.74 KB)
📄
_std_types_schema.py
(28.27 KB)
📄
_typing_extra.py
(16.15 KB)
📄
_utils.py
(11.42 KB)
📄
_validate_call.py
(5.36 KB)
📄
_validators.py
(9.81 KB)
Editing: _mock_val_ser.py
from __future__ import annotations from typing import TYPE_CHECKING, Callable, Generic, TypeVar from pydantic_core import SchemaSerializer, SchemaValidator from typing_extensions import Literal from ..errors import PydanticErrorCodes, PydanticUserError if TYPE_CHECKING: from ..dataclasses import PydanticDataclass from ..main import BaseModel ValSer = TypeVar('ValSer', SchemaValidator, SchemaSerializer) class MockValSer(Generic[ValSer]): """Mocker for `pydantic_core.SchemaValidator` or `pydantic_core.SchemaSerializer` which optionally attempts to rebuild the thing it's mocking when one of its methods is accessed and raises an error if that fails. """ __slots__ = '_error_message', '_code', '_val_or_ser', '_attempt_rebuild' def __init__( self, error_message: str, *, code: PydanticErrorCodes, val_or_ser: Literal['validator', 'serializer'], attempt_rebuild: Callable[[], ValSer | None] | None = None, ) -> None: self._error_message = error_message self._val_or_ser = SchemaValidator if val_or_ser == 'validator' else SchemaSerializer self._code: PydanticErrorCodes = code self._attempt_rebuild = attempt_rebuild def __getattr__(self, item: str) -> None: __tracebackhide__ = True if self._attempt_rebuild: val_ser = self._attempt_rebuild() if val_ser is not None: return getattr(val_ser, item) # raise an AttributeError if `item` doesn't exist getattr(self._val_or_ser, item) raise PydanticUserError(self._error_message, code=self._code) def rebuild(self) -> ValSer | None: if self._attempt_rebuild: val_ser = self._attempt_rebuild() if val_ser is not None: return val_ser else: raise PydanticUserError(self._error_message, code=self._code) return None def set_model_mocks(cls: type[BaseModel], cls_name: str, undefined_name: str = 'all referenced types') -> None: """Set `__pydantic_validator__` and `__pydantic_serializer__` to `MockValSer`s on a model. Args: cls: The model class to set the mocks on cls_name: Name of the model class, used in error messages undefined_name: Name of the undefined thing, used in error messages """ undefined_type_error_message = ( f'`{cls_name}` is not fully defined; you should define {undefined_name},' f' then call `{cls_name}.model_rebuild()`.' ) def attempt_rebuild_validator() -> SchemaValidator | None: if cls.model_rebuild(raise_errors=False, _parent_namespace_depth=5): return cls.__pydantic_validator__ else: return None cls.__pydantic_validator__ = MockValSer( # type: ignore[assignment] undefined_type_error_message, code='class-not-fully-defined', val_or_ser='validator', attempt_rebuild=attempt_rebuild_validator, ) def attempt_rebuild_serializer() -> SchemaSerializer | None: if cls.model_rebuild(raise_errors=False, _parent_namespace_depth=5): return cls.__pydantic_serializer__ else: return None cls.__pydantic_serializer__ = MockValSer( # type: ignore[assignment] undefined_type_error_message, code='class-not-fully-defined', val_or_ser='serializer', attempt_rebuild=attempt_rebuild_serializer, ) def set_dataclass_mock_validator(cls: type[PydanticDataclass], cls_name: str, undefined_name: str) -> None: undefined_type_error_message = ( f'`{cls_name}` is not fully defined; you should define {undefined_name},' f' then call `pydantic.dataclasses.rebuild_dataclass({cls_name})`.' ) def attempt_rebuild() -> SchemaValidator | None: from ..dataclasses import rebuild_dataclass if rebuild_dataclass(cls, raise_errors=False, _parent_namespace_depth=5): return cls.__pydantic_validator__ else: return None cls.__pydantic_validator__ = MockValSer( # type: ignore[assignment] undefined_type_error_message, code='class-not-fully-defined', val_or_ser='validator', attempt_rebuild=attempt_rebuild, )
Upload File
Create Folder