Coverage for /pythoncovmergedfiles/medio/medio/src/pydantic/pydantic/deprecated/tools.py: 57%

28 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-27 07:38 +0000

1from __future__ import annotations 

2 

3import json 

4import warnings 

5from typing import Any, Callable, Type, TypeVar, Union 

6 

7from typing_extensions import deprecated 

8 

9from ..analyzed_type import AnalyzedType 

10from ..json_schema import DEFAULT_REF_TEMPLATE, GenerateJsonSchema 

11 

12__all__ = 'parse_obj_as', 'schema_of', 'schema_json_of' 

13 

14NameFactory = Union[str, Callable[[Type[Any]], str]] 

15 

16 

17T = TypeVar('T') 

18 

19 

20@deprecated('parse_obj_as is deprecated. Use pydantic.AnalyzedType.validate_python instead.') 

21def parse_obj_as(type_: type[T], obj: Any, type_name: NameFactory | None = None) -> T: 

22 warnings.warn( 

23 'parse_obj_as is deprecated. Use pydantic.AnalyzedType.validate_python instead.', 

24 DeprecationWarning, 

25 stacklevel=2, 

26 ) 

27 if type_name is not None: # pragma: no cover 

28 warnings.warn( 

29 'The type_name parameter is deprecated. parse_obj_as no longer creates temporary models', 

30 DeprecationWarning, 

31 stacklevel=2, 

32 ) 

33 return AnalyzedType(type_).validate_python(obj) 

34 

35 

36@deprecated('schema_of is deprecated. Use pydantic.AnalyzedType.json_schema instead.') 

37def 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.AnalyzedType.json_schema instead.', DeprecationWarning, stacklevel=2 

48 ) 

49 res = AnalyzedType(type_).json_schema( 

50 by_alias=by_alias, 

51 schema_generator=schema_generator, 

52 ref_template=ref_template, 

53 ) 

54 if title is not None: 

55 if isinstance(title, str): 

56 res['title'] = title 

57 else: 

58 warnings.warn( 

59 'Passing a callable for the `title` parameter is deprecated and no longer supported', 

60 DeprecationWarning, 

61 stacklevel=2, 

62 ) 

63 res['title'] = title(type_) 

64 return res 

65 

66 

67@deprecated('schema_json_of is deprecated. Use pydantic.AnalyzedType.json_schema instead.') 

68def schema_json_of( 

69 type_: Any, 

70 *, 

71 title: NameFactory | None = None, 

72 by_alias: bool = True, 

73 ref_template: str = DEFAULT_REF_TEMPLATE, 

74 schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema, 

75 **dumps_kwargs: Any, 

76) -> str: 

77 """Generate a JSON schema (as JSON) for the passed model or dynamically generated one""" 

78 warnings.warn( 

79 'schema_json_of is deprecated. Use pydantic.AnalyzedType.json_schema instead.', DeprecationWarning, stacklevel=2 

80 ) 

81 return json.dumps( 

82 schema_of(type_, title=title, by_alias=by_alias, ref_template=ref_template, schema_generator=schema_generator), 

83 **dumps_kwargs, 

84 )