Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pydantic/annotated_handlers.py: 63%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

27 statements  

1"""Type annotations to use with `__get_pydantic_core_schema__` and `__get_pydantic_json_schema__`.""" 

2 

3from __future__ import annotations as _annotations 

4 

5from typing import TYPE_CHECKING, Any, Union 

6 

7from pydantic_core import core_schema 

8 

9if TYPE_CHECKING: 

10 from ._internal._namespace_utils import NamespacesTuple 

11 from .json_schema import JsonSchemaMode, JsonSchemaValue 

12 

13 CoreSchemaOrField = Union[ 

14 core_schema.CoreSchema, 

15 core_schema.ModelField, 

16 core_schema.DataclassField, 

17 core_schema.TypedDictField, 

18 core_schema.ComputedField, 

19 ] 

20 

21__all__ = 'GetJsonSchemaHandler', 'GetCoreSchemaHandler' 

22 

23 

24class GetJsonSchemaHandler: 

25 """Handler to call into the next JSON schema generation function. 

26 

27 Attributes: 

28 mode: Json schema mode, can be `validation` or `serialization`. 

29 """ 

30 

31 mode: JsonSchemaMode 

32 

33 def __call__(self, core_schema: CoreSchemaOrField, /) -> JsonSchemaValue: 

34 """Call the inner handler and get the JsonSchemaValue it returns. 

35 This will call the next JSON schema modifying function up until it calls 

36 into `pydantic.json_schema.GenerateJsonSchema`, which will raise a 

37 `pydantic.errors.PydanticInvalidForJsonSchema` error if it cannot generate 

38 a JSON schema. 

39 

40 Args: 

41 core_schema: A `pydantic_core.core_schema.CoreSchema`. 

42 

43 Returns: 

44 JsonSchemaValue: The JSON schema generated by the inner JSON schema modify 

45 functions. 

46 """ 

47 raise NotImplementedError 

48 

49 def resolve_ref_schema(self, maybe_ref_json_schema: JsonSchemaValue, /) -> JsonSchemaValue: 

50 """Get the real schema for a `{"$ref": ...}` schema. 

51 If the schema given is not a `$ref` schema, it will be returned as is. 

52 This means you don't have to check before calling this function. 

53 

54 Args: 

55 maybe_ref_json_schema: A JsonSchemaValue which may be a `$ref` schema. 

56 

57 Raises: 

58 LookupError: If the ref is not found. 

59 

60 Returns: 

61 JsonSchemaValue: A JsonSchemaValue that has no `$ref`. 

62 """ 

63 raise NotImplementedError 

64 

65 

66class GetCoreSchemaHandler: 

67 """Handler to call into the next CoreSchema schema generation function.""" 

68 

69 def __call__(self, source_type: Any, /) -> core_schema.CoreSchema: 

70 """Call the inner handler and get the CoreSchema it returns. 

71 This will call the next CoreSchema modifying function up until it calls 

72 into Pydantic's internal schema generation machinery, which will raise a 

73 `pydantic.errors.PydanticSchemaGenerationError` error if it cannot generate 

74 a CoreSchema for the given source type. 

75 

76 Args: 

77 source_type: The input type. 

78 

79 Returns: 

80 CoreSchema: The `pydantic-core` CoreSchema generated. 

81 """ 

82 raise NotImplementedError 

83 

84 def generate_schema(self, source_type: Any, /) -> core_schema.CoreSchema: 

85 """Generate a schema unrelated to the current context. 

86 Use this function if e.g. you are handling schema generation for a sequence 

87 and want to generate a schema for its items. 

88 Otherwise, you may end up doing something like applying a `min_length` constraint 

89 that was intended for the sequence itself to its items! 

90 

91 Args: 

92 source_type: The input type. 

93 

94 Returns: 

95 CoreSchema: The `pydantic-core` CoreSchema generated. 

96 """ 

97 raise NotImplementedError 

98 

99 def resolve_ref_schema(self, maybe_ref_schema: core_schema.CoreSchema, /) -> core_schema.CoreSchema: 

100 """Get the real schema for a `definition-ref` schema. 

101 If the schema given is not a `definition-ref` schema, it will be returned as is. 

102 This means you don't have to check before calling this function. 

103 

104 Args: 

105 maybe_ref_schema: A `CoreSchema`, `ref`-based or not. 

106 

107 Raises: 

108 LookupError: If the `ref` is not found. 

109 

110 Returns: 

111 A concrete `CoreSchema`. 

112 """ 

113 raise NotImplementedError 

114 

115 @property 

116 def field_name(self) -> str | None: 

117 """Get the name of the closest field to this validator.""" 

118 raise NotImplementedError 

119 

120 def _get_types_namespace(self) -> NamespacesTuple: 

121 """Internal method used during type resolution for serializer annotations.""" 

122 raise NotImplementedError