codekingpro/portable-devtools
114k
1from __future__ import annotations2 3import json4import warnings5from typing import TYPE_CHECKING, Any, Callable, TypeVar, Union6 7from typing_extensions import deprecated8 9from ..json_schema import DEFAULT_REF_TEMPLATE, GenerateJsonSchema10from ..type_adapter import TypeAdapter11from ..warnings import PydanticDeprecatedSince2012 13if not TYPE_CHECKING:14 # See PyCharm issues https://youtrack.jetbrains.com/issue/PY-2191515 # and https://youtrack.jetbrains.com/issue/PY-5142816 DeprecationWarning = PydanticDeprecatedSince2017 18__all__ = 'parse_obj_as', 'schema_of', 'schema_json_of'19 20NameFactory = Union[str, Callable[[type[Any]], str]]21 22 23T = TypeVar('T')24 25 26@deprecated(27 '`parse_obj_as` is deprecated. Use `pydantic.TypeAdapter.validate_python` instead.',28 category=None,29)30def parse_obj_as(type_: type[T], obj: Any, type_name: NameFactory | None = None) -> T:31 warnings.warn(32 '`parse_obj_as` is deprecated. Use `pydantic.TypeAdapter.validate_python` instead.',33 category=PydanticDeprecatedSince20,34 stacklevel=2,35 )36 if type_name is not None: # pragma: no cover37 warnings.warn(38 'The type_name parameter is deprecated. parse_obj_as no longer creates temporary models',39 DeprecationWarning,40 stacklevel=2,41 )42 return TypeAdapter(type_).validate_python(obj)43 44 45@deprecated(46 '`schema_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',47 category=None,48)49def schema_of(50 type_: Any,51 *,52 title: NameFactory | None = None,53 by_alias: bool = True,54 ref_template: str = DEFAULT_REF_TEMPLATE,55 schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,56) -> dict[str, Any]:57 """Generate a JSON schema (as dict) for the passed model or dynamically generated one."""58 warnings.warn(59 '`schema_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',60 category=PydanticDeprecatedSince20,61 stacklevel=2,62 )63 res = TypeAdapter(type_).json_schema(64 by_alias=by_alias,65 schema_generator=schema_generator,66 ref_template=ref_template,67 )68 if title is not None:69 if isinstance(title, str):70 res['title'] = title71 else:72 warnings.warn(73 'Passing a callable for the `title` parameter is deprecated and no longer supported',74 DeprecationWarning,75 stacklevel=2,76 )77 res['title'] = title(type_)78 return res79 80 81@deprecated(82 '`schema_json_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',83 category=None,84)85def schema_json_of(86 type_: Any,87 *,88 title: NameFactory | None = None,89 by_alias: bool = True,90 ref_template: str = DEFAULT_REF_TEMPLATE,91 schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,92 **dumps_kwargs: Any,93) -> str:94 """Generate a JSON schema (as JSON) for the passed model or dynamically generated one."""95 warnings.warn(96 '`schema_json_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',97 category=PydanticDeprecatedSince20,98 stacklevel=2,99 )100 return json.dumps(101 schema_of(type_, title=title, by_alias=by_alias, ref_template=ref_template, schema_generator=schema_generator),102 **dumps_kwargs,103 )104 