$33 GRAYBYTE WORDPRESS FILE MANAGER $23

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 104.21.15.130 | ADMIN IP 216.73.216.174
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/hc_python/lib/python3.12/site-packages/pydantic/_internal/

HOME
Current File : /opt/hc_python/lib/python3.12/site-packages/pydantic/_internal//_schema_generation_shared.py
"""Types and utility functions used by various other internal tools."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable

from pydantic_core import core_schema
from typing_extensions import Literal

from ..annotated_handlers import GetCoreSchemaHandler, GetJsonSchemaHandler

if TYPE_CHECKING:
    from ..json_schema import GenerateJsonSchema, JsonSchemaValue
    from ._core_utils import CoreSchemaOrField
    from ._generate_schema import GenerateSchema

    GetJsonSchemaFunction = Callable[[CoreSchemaOrField, GetJsonSchemaHandler], JsonSchemaValue]
    HandlerOverride = Callable[[CoreSchemaOrField], JsonSchemaValue]


class GenerateJsonSchemaHandler(GetJsonSchemaHandler):
    """JsonSchemaHandler implementation that doesn't do ref unwrapping by default.

    This is used for any Annotated metadata so that we don't end up with conflicting
    modifications to the definition schema.

    Used internally by Pydantic, please do not rely on this implementation.
    See `GetJsonSchemaHandler` for the handler API.
    """

    def __init__(self, generate_json_schema: GenerateJsonSchema, handler_override: HandlerOverride | None) -> None:
        self.generate_json_schema = generate_json_schema
        self.handler = handler_override or generate_json_schema.generate_inner
        self.mode = generate_json_schema.mode

    def __call__(self, core_schema: CoreSchemaOrField, /) -> JsonSchemaValue:
        return self.handler(core_schema)

    def resolve_ref_schema(self, maybe_ref_json_schema: JsonSchemaValue) -> JsonSchemaValue:
        """Resolves `$ref` in the json schema.

        This returns the input json schema if there is no `$ref` in json schema.

        Args:
            maybe_ref_json_schema: The input json schema that may contains `$ref`.

        Returns:
            Resolved json schema.

        Raises:
            LookupError: If it can't find the definition for `$ref`.
        """
        if '$ref' not in maybe_ref_json_schema:
            return maybe_ref_json_schema
        ref = maybe_ref_json_schema['$ref']
        json_schema = self.generate_json_schema.get_schema_from_definitions(ref)
        if json_schema is None:
            raise LookupError(
                f'Could not find a ref for {ref}.'
                ' Maybe you tried to call resolve_ref_schema from within a recursive model?'
            )
        return json_schema


class CallbackGetCoreSchemaHandler(GetCoreSchemaHandler):
    """Wrapper to use an arbitrary function as a `GetCoreSchemaHandler`.

    Used internally by Pydantic, please do not rely on this implementation.
    See `GetCoreSchemaHandler` for the handler API.
    """

    def __init__(
        self,
        handler: Callable[[Any], core_schema.CoreSchema],
        generate_schema: GenerateSchema,
        ref_mode: Literal['to-def', 'unpack'] = 'to-def',
    ) -> None:
        self._handler = handler
        self._generate_schema = generate_schema
        self._ref_mode = ref_mode

    def __call__(self, source_type: Any, /) -> core_schema.CoreSchema:
        schema = self._handler(source_type)
        ref = schema.get('ref')
        if self._ref_mode == 'to-def':
            if ref is not None:
                self._generate_schema.defs.definitions[ref] = schema
                return core_schema.definition_reference_schema(ref)
            return schema
        else:  # ref_mode = 'unpack
            return self.resolve_ref_schema(schema)

    def _get_types_namespace(self) -> dict[str, Any] | None:
        return self._generate_schema._types_namespace

    def generate_schema(self, source_type: Any, /) -> core_schema.CoreSchema:
        return self._generate_schema.generate_schema(source_type)

    @property
    def field_name(self) -> str | None:
        return self._generate_schema.field_name_stack.get()

    def resolve_ref_schema(self, maybe_ref_schema: core_schema.CoreSchema) -> core_schema.CoreSchema:
        """Resolves reference in the core schema.

        Args:
            maybe_ref_schema: The input core schema that may contains reference.

        Returns:
            Resolved core schema.

        Raises:
            LookupError: If it can't find the definition for reference.
        """
        if maybe_ref_schema['type'] == 'definition-ref':
            ref = maybe_ref_schema['schema_ref']
            if ref not in self._generate_schema.defs.definitions:
                raise LookupError(
                    f'Could not find a ref for {ref}.'
                    ' Maybe you tried to call resolve_ref_schema from within a recursive model?'
                )
            return self._generate_schema.defs.definitions[ref]
        elif maybe_ref_schema['type'] == 'definitions':
            return self.resolve_ref_schema(maybe_ref_schema['schema'])
        return maybe_ref_schema


Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
4 Apr 2025 8.03 AM
root / root
0755
__pycache__
--
4 Apr 2025 8.03 AM
root / root
0755
__init__.py
0 KB
4 Apr 2025 8.03 AM
root / root
0644
_config.py
12.31 KB
4 Apr 2025 8.03 AM
root / root
0644
_core_metadata.py
3.438 KB
4 Apr 2025 8.03 AM
root / root
0644
_core_utils.py
23.699 KB
4 Apr 2025 8.03 AM
root / root
0644
_dataclasses.py
8.528 KB
4 Apr 2025 8.03 AM
root / root
0644
_decorators.py
31.205 KB
4 Apr 2025 8.03 AM
root / root
0644
_decorators_v1.py
6.058 KB
4 Apr 2025 8.03 AM
root / root
0644
_discriminated_union.py
25.815 KB
4 Apr 2025 8.03 AM
root / root
0644
_docs_extraction.py
3.702 KB
4 Apr 2025 8.03 AM
root / root
0644
_fields.py
14.584 KB
4 Apr 2025 8.03 AM
root / root
0644
_forward_ref.py
0.597 KB
4 Apr 2025 8.03 AM
root / root
0644
_generate_schema.py
102.905 KB
4 Apr 2025 8.03 AM
root / root
0644
_generics.py
21.686 KB
4 Apr 2025 8.03 AM
root / root
0644
_git.py
0.766 KB
4 Apr 2025 8.03 AM
root / root
0644
_internal_dataclass.py
0.141 KB
4 Apr 2025 8.03 AM
root / root
0644
_known_annotated_metadata.py
13.856 KB
4 Apr 2025 8.03 AM
root / root
0644
_mock_val_ser.py
7.144 KB
4 Apr 2025 8.03 AM
root / root
0644
_model_construction.py
30.626 KB
4 Apr 2025 8.03 AM
root / root
0644
_repr.py
4.462 KB
4 Apr 2025 8.03 AM
root / root
0644
_schema_generation_shared.py
4.739 KB
4 Apr 2025 8.03 AM
root / root
0644
_signature.py
6.146 KB
4 Apr 2025 8.03 AM
root / root
0644
_std_types_schema.py
28.204 KB
4 Apr 2025 8.03 AM
root / root
0644
_typing_extra.py
18.975 KB
4 Apr 2025 8.03 AM
root / root
0644
_utils.py
12.364 KB
4 Apr 2025 8.03 AM
root / root
0644
_validate_call.py
3.702 KB
4 Apr 2025 8.03 AM
root / root
0644
_validators.py
10.857 KB
4 Apr 2025 8.03 AM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025 CONTACT ME
Static GIF