Coverage for pydantic / annotated_handlers.py: 100.00%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-11-21 11:56 +0000

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

2 

3from __future__ import annotations as _annotations 1abcdefghijklmnopqrs

4 

5from typing import TYPE_CHECKING, Any, TypeAlias 1abcdefghijklmnopqrs

6 

7from pydantic_core import core_schema 1abcdefghijklmnopqrs

8 

9if TYPE_CHECKING: 1abcdefghijklmnopqrs

10 from ._internal._namespace_utils import NamespacesTuple 

11 from .json_schema import JsonSchemaMode, JsonSchemaValue 

12 

13 CoreSchemaOrField: TypeAlias = ( 

14 core_schema.CoreSchema 

15 | core_schema.ModelField 

16 | core_schema.DataclassField 

17 | core_schema.TypedDictField 

18 | core_schema.ComputedField 

19 ) 

20 

21 

22__all__ = 'GetJsonSchemaHandler', 'GetCoreSchemaHandler' 1abcdefghijklmnopqrs

23 

24 

25class GetJsonSchemaHandler: 1abcdefghijklmnopqrs

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

27 

28 Attributes: 

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

30 """ 

31 

32 mode: JsonSchemaMode 1abcdefghijklmnopqrs

33 

34 def __call__(self, core_schema: CoreSchemaOrField, /) -> JsonSchemaValue: 1abcdefghijklmnopqrs

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

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

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

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

39 a JSON schema. 

40 

41 Args: 

42 core_schema: A `pydantic_core.core_schema.CoreSchema`. 

43 

44 Returns: 

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

46 functions. 

47 """ 

48 raise NotImplementedError 

49 

50 def resolve_ref_schema(self, maybe_ref_json_schema: JsonSchemaValue, /) -> JsonSchemaValue: 1abcdefghijklmnopqrs

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

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

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

54 

55 Args: 

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

57 

58 Raises: 

59 LookupError: If the ref is not found. 

60 

61 Returns: 

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

63 """ 

64 raise NotImplementedError 

65 

66 

67class GetCoreSchemaHandler: 1abcdefghijklmnopqrs

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

69 

70 def __call__(self, source_type: Any, /) -> core_schema.CoreSchema: 1abcdefghijklmnopqrs

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

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

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

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

75 a CoreSchema for the given source type. 

76 

77 Args: 

78 source_type: The input type. 

79 

80 Returns: 

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

82 """ 

83 raise NotImplementedError 

84 

85 def generate_schema(self, source_type: Any, /) -> core_schema.CoreSchema: 1abcdefghijklmnopqrs

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

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

88 and want to generate a schema for its items. 

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

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

91 

92 Args: 

93 source_type: The input type. 

94 

95 Returns: 

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

97 """ 

98 raise NotImplementedError 

99 

100 def resolve_ref_schema(self, maybe_ref_schema: core_schema.CoreSchema, /) -> core_schema.CoreSchema: 1abcdefghijklmnopqrs

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

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

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

104 

105 Args: 

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

107 

108 Raises: 

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

110 

111 Returns: 

112 A concrete `CoreSchema`. 

113 """ 

114 raise NotImplementedError 

115 

116 @property 1abcdefghijklmnopqrs

117 def field_name(self) -> str | None: 1abcdefghijklmnopqrs

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

119 raise NotImplementedError 

120 

121 def _get_types_namespace(self) -> NamespacesTuple: 1abcdefghijklmnopqrs

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

123 raise NotImplementedError