Skip to content

Commit

Permalink
Allow disabling CoreSchema validation for faster startup times (#7565)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Sep 22, 2023
1 parent 56df0cf commit b3c6380
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
8 changes: 8 additions & 0 deletions pydantic/_internal/_core_utils.py
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from collections import defaultdict
from typing import (
Any,
Expand All @@ -12,6 +13,7 @@
)

from pydantic_core import CoreSchema, core_schema
from pydantic_core import validate_core_schema as _validate_core_schema
from typing_extensions import TypeAliasType, TypedDict, TypeGuard, get_args

from . import _repr
Expand Down Expand Up @@ -621,3 +623,9 @@ def strip_metadata(s: CoreSchema, recurse: Recurse) -> CoreSchema:
schema = walk_core_schema(schema, strip_metadata)

return print(schema)


def validate_core_schema(schema: CoreSchema) -> CoreSchema:
if 'PYDANTIC_SKIP_VALIDATING_CORE_SCHEMAS' in os.environ:
return schema
return _validate_core_schema(schema)
3 changes: 1 addition & 2 deletions pydantic/_internal/_dataclasses.py
Expand Up @@ -15,15 +15,14 @@
SchemaSerializer,
SchemaValidator,
core_schema,
validate_core_schema,
)
from typing_extensions import TypeGuard

from ..errors import PydanticUndefinedAnnotation
from ..fields import FieldInfo
from ..warnings import PydanticDeprecatedSince20
from . import _config, _decorators, _discriminated_union, _typing_extra
from ._core_utils import collect_invalid_schemas, simplify_schema_references
from ._core_utils import collect_invalid_schemas, simplify_schema_references, validate_core_schema
from ._fields import collect_dataclass_fields
from ._generate_schema import GenerateSchema
from ._generics import get_standard_typevars_map
Expand Down
4 changes: 2 additions & 2 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -9,14 +9,14 @@
from types import FunctionType
from typing import Any, Callable, Generic, Mapping

from pydantic_core import PydanticUndefined, SchemaSerializer, SchemaValidator, validate_core_schema
from pydantic_core import PydanticUndefined, SchemaSerializer, SchemaValidator
from typing_extensions import dataclass_transform, deprecated

from ..errors import PydanticUndefinedAnnotation, PydanticUserError
from ..fields import Field, FieldInfo, ModelPrivateAttr, PrivateAttr
from ..warnings import PydanticDeprecatedSince20
from ._config import ConfigWrapper
from ._core_utils import collect_invalid_schemas, simplify_schema_references
from ._core_utils import collect_invalid_schemas, simplify_schema_references, validate_core_schema
from ._decorators import (
ComputedFieldInfo,
DecoratorInfos,
Expand Down
4 changes: 2 additions & 2 deletions pydantic/_internal/_validate_call.py
Expand Up @@ -10,7 +10,7 @@
from ..config import ConfigDict
from . import _discriminated_union, _generate_schema, _typing_extra
from ._config import ConfigWrapper
from ._core_utils import simplify_schema_references
from ._core_utils import simplify_schema_references, validate_core_schema


@dataclass
Expand Down Expand Up @@ -79,7 +79,7 @@ def __init__(self, function: Callable[..., Any], config: ConfigDict | None, vali
schema = _discriminated_union.apply_discriminators(simplify_schema_references(schema))
self.__return_pydantic_core_schema__ = schema
core_config = config_wrapper.core_config(self)
schema = pydantic_core.validate_core_schema(schema)
schema = validate_core_schema(schema)
validator = pydantic_core.SchemaValidator(schema, core_config)
if inspect.iscoroutinefunction(self.raw_function):

Expand Down
4 changes: 2 additions & 2 deletions pydantic/type_adapter.py
Expand Up @@ -5,7 +5,7 @@
from dataclasses import is_dataclass
from typing import TYPE_CHECKING, Any, Dict, Generic, Iterable, Set, TypeVar, Union, overload

from pydantic_core import CoreSchema, SchemaSerializer, SchemaValidator, Some, validate_core_schema
from pydantic_core import CoreSchema, SchemaSerializer, SchemaValidator, Some
from typing_extensions import Literal, is_typeddict

from pydantic.errors import PydanticUserError
Expand Down Expand Up @@ -168,7 +168,7 @@ def __init__(self, type: Any, *, config: ConfigDict | None = None, _parent_depth

core_schema = _discriminated_union.apply_discriminators(_core_utils.simplify_schema_references(core_schema))

core_schema = validate_core_schema(core_schema)
core_schema = _core_utils.validate_core_schema(core_schema)

core_config = config_wrapper.core_config(None)
validator: SchemaValidator
Expand Down

0 comments on commit b3c6380

Please sign in to comment.