tools.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from __future__ import annotations
  2. import json
  3. import warnings
  4. from typing import TYPE_CHECKING, Any, Callable, Type, 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.', category=PydanticDeprecatedSince20
  18. )
  19. def parse_obj_as(type_: type[T], obj: Any, type_name: NameFactory | None = None) -> T:
  20. warnings.warn(
  21. 'parse_obj_as is deprecated. Use pydantic.TypeAdapter.validate_python instead.',
  22. DeprecationWarning,
  23. stacklevel=2,
  24. )
  25. if type_name is not None: # pragma: no cover
  26. warnings.warn(
  27. 'The type_name parameter is deprecated. parse_obj_as no longer creates temporary models',
  28. DeprecationWarning,
  29. stacklevel=2,
  30. )
  31. return TypeAdapter(type_).validate_python(obj)
  32. @deprecated(
  33. 'schema_of is deprecated. Use pydantic.TypeAdapter.json_schema instead.', category=PydanticDeprecatedSince20
  34. )
  35. def schema_of(
  36. type_: Any,
  37. *,
  38. title: NameFactory | None = None,
  39. by_alias: bool = True,
  40. ref_template: str = DEFAULT_REF_TEMPLATE,
  41. schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
  42. ) -> dict[str, Any]:
  43. """Generate a JSON schema (as dict) for the passed model or dynamically generated one."""
  44. warnings.warn(
  45. 'schema_of is deprecated. Use pydantic.TypeAdapter.json_schema instead.', DeprecationWarning, stacklevel=2
  46. )
  47. res = TypeAdapter(type_).json_schema(
  48. by_alias=by_alias,
  49. schema_generator=schema_generator,
  50. ref_template=ref_template,
  51. )
  52. if title is not None:
  53. if isinstance(title, str):
  54. res['title'] = title
  55. else:
  56. warnings.warn(
  57. 'Passing a callable for the `title` parameter is deprecated and no longer supported',
  58. DeprecationWarning,
  59. stacklevel=2,
  60. )
  61. res['title'] = title(type_)
  62. return res
  63. @deprecated(
  64. 'schema_json_of is deprecated. Use pydantic.TypeAdapter.json_schema instead.', category=PydanticDeprecatedSince20
  65. )
  66. def schema_json_of(
  67. type_: Any,
  68. *,
  69. title: NameFactory | None = None,
  70. by_alias: bool = True,
  71. ref_template: str = DEFAULT_REF_TEMPLATE,
  72. schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
  73. **dumps_kwargs: Any,
  74. ) -> str:
  75. """Generate a JSON schema (as JSON) for the passed model or dynamically generated one."""
  76. warnings.warn(
  77. 'schema_json_of is deprecated. Use pydantic.TypeAdapter.json_schema instead.', DeprecationWarning, stacklevel=2
  78. )
  79. return json.dumps(
  80. schema_of(type_, title=title, by_alias=by_alias, ref_template=ref_template, schema_generator=schema_generator),
  81. **dumps_kwargs,
  82. )