GRAYBYTE WORDPRESS FILE MANAGER8061

Server IP : 198.54.121.189 / Your IP : 216.73.216.112
System : Linux premium69.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
PHP Version : 7.4.33
Disable Function : NONE
cURL : ON | WGET : ON | Sudo : OFF | Pkexec : OFF
Directory : /opt/cloudlinux/venv/lib/python3.11/site-packages/pydantic/_internal/
Upload Files :
Current_dir [ Not Writeable ] Document_root [ Writeable ]

Command :


Current File : /opt/cloudlinux/venv/lib/python3.11/site-packages/pydantic/_internal//_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,
    )

[ Back ]
Name
Size
Last Modified
Owner / Group
Permissions
Options
..
--
March 06 2024 00:27:04
root / root
0755
__pycache__
--
March 06 2024 00:27:04
root / root
0755
__init__.py
0 KB
March 06 2024 00:27:04
root / root
0644
_config.py
11.042 KB
March 06 2024 00:27:04
root / root
0644
_core_metadata.py
3.438 KB
March 06 2024 00:27:04
root / root
0644
_core_utils.py
24.238 KB
March 06 2024 00:27:04
root / root
0644
_dataclasses.py
10.456 KB
March 06 2024 00:27:04
root / root
0644
_decorators.py
30.054 KB
March 06 2024 00:27:04
root / root
0644
_decorators_v1.py
6.119 KB
March 06 2024 00:27:04
root / root
0644
_discriminated_union.py
25.613 KB
March 06 2024 00:27:04
root / root
0644
_fields.py
11.624 KB
March 06 2024 00:27:04
root / root
0644
_forward_ref.py
0.415 KB
March 06 2024 00:27:04
root / root
0644
_generate_schema.py
87.887 KB
March 06 2024 00:27:04
root / root
0644
_generics.py
21.653 KB
March 06 2024 00:27:04
root / root
0644
_internal_dataclass.py
0.202 KB
March 06 2024 00:27:04
root / root
0644
_known_annotated_metadata.py
15.894 KB
March 06 2024 00:27:04
root / root
0644
_mock_val_ser.py
4.204 KB
March 06 2024 00:27:04
root / root
0644
_model_construction.py
26.409 KB
March 06 2024 00:27:04
root / root
0644
_repr.py
4.273 KB
March 06 2024 00:27:04
root / root
0644
_schema_generation_shared.py
4.741 KB
March 06 2024 00:27:04
root / root
0644
_std_types_schema.py
28.271 KB
March 06 2024 00:27:04
root / root
0644
_typing_extra.py
16.15 KB
March 06 2024 00:27:04
root / root
0644
_utils.py
11.424 KB
March 06 2024 00:27:04
root / root
0644
_validate_call.py
5.362 KB
March 06 2024 00:27:04
root / root
0644
_validators.py
9.812 KB
March 06 2024 00:27:04
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025
CONTACT ME
Static GIF