25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
3.3 KiB

  1. from __future__ import annotations
  2. import json
  3. import warnings
  4. from typing import TYPE_CHECKING, Any, Callable, TypeVar, Union
  5. from typing_extensions import deprecated
  6. from ..json_schema import DEFAULT_REF_TEMPLATE, GenerateJsonSchema
  7. from ..type_adapter import TypeAdapter
  8. from ..warnings import PydanticDeprecatedSince20
  9. if not TYPE_CHECKING:
  10. # See PyCharm issues https://youtrack.jetbrains.com/issue/PY-21915
  11. # and https://youtrack.jetbrains.com/issue/PY-51428
  12. DeprecationWarning = PydanticDeprecatedSince20
  13. __all__ = 'parse_obj_as', 'schema_of', 'schema_json_of'
  14. NameFactory = Union[str, Callable[[type[Any]], str]]
  15. T = TypeVar('T')
  16. @deprecated(
  17. '`parse_obj_as` is deprecated. Use `pydantic.TypeAdapter.validate_python` instead.',
  18. category=None,
  19. )
  20. def parse_obj_as(type_: type[T], obj: Any, type_name: NameFactory | None = None) -> T:
  21. warnings.warn(
  22. '`parse_obj_as` is deprecated. Use `pydantic.TypeAdapter.validate_python` instead.',
  23. category=PydanticDeprecatedSince20,
  24. stacklevel=2,
  25. )
  26. if type_name is not None: # pragma: no cover
  27. warnings.warn(
  28. 'The type_name parameter is deprecated. parse_obj_as no longer creates temporary models',
  29. DeprecationWarning,
  30. stacklevel=2,
  31. )
  32. return TypeAdapter(type_).validate_python(obj)
  33. @deprecated(
  34. '`schema_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',
  35. category=None,
  36. )
  37. def schema_of(
  38. type_: Any,
  39. *,
  40. title: NameFactory | None = None,
  41. by_alias: bool = True,
  42. ref_template: str = DEFAULT_REF_TEMPLATE,
  43. schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
  44. ) -> dict[str, Any]:
  45. """Generate a JSON schema (as dict) for the passed model or dynamically generated one."""
  46. warnings.warn(
  47. '`schema_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',
  48. category=PydanticDeprecatedSince20,
  49. stacklevel=2,
  50. )
  51. res = TypeAdapter(type_).json_schema(
  52. by_alias=by_alias,
  53. schema_generator=schema_generator,
  54. ref_template=ref_template,
  55. )
  56. if title is not None:
  57. if isinstance(title, str):
  58. res['title'] = title
  59. else:
  60. warnings.warn(
  61. 'Passing a callable for the `title` parameter is deprecated and no longer supported',
  62. DeprecationWarning,
  63. stacklevel=2,
  64. )
  65. res['title'] = title(type_)
  66. return res
  67. @deprecated(
  68. '`schema_json_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',
  69. category=None,
  70. )
  71. def schema_json_of(
  72. type_: Any,
  73. *,
  74. title: NameFactory | None = None,
  75. by_alias: bool = True,
  76. ref_template: str = DEFAULT_REF_TEMPLATE,
  77. schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
  78. **dumps_kwargs: Any,
  79. ) -> str:
  80. """Generate a JSON schema (as JSON) for the passed model or dynamically generated one."""
  81. warnings.warn(
  82. '`schema_json_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',
  83. category=PydanticDeprecatedSince20,
  84. stacklevel=2,
  85. )
  86. return json.dumps(
  87. schema_of(type_, title=title, by_alias=by_alias, ref_template=ref_template, schema_generator=schema_generator),
  88. **dumps_kwargs,
  89. )