Coverage for pydantic-core / python / pydantic_core / __init__.py: 100.00%

59 statements  

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

1from __future__ import annotations 1abcdefghijklmnopqrs

2 

3import sys as _sys 1abcdefghijklmnopqrs

4from collections.abc import MutableSet 1abcdefghijklmnopqrs

5from typing import Any as _Any 1abcdefghijklmnopqrs

6from typing import NotRequired as _NotRequired 1abcdefghijklmnopqrs

7 

8from typing_extensions import Sentinel 1abcdefghijklmnopqrs

9 

10from ._pydantic_core import ( 1abcdefghijklmnopqrs

11 ArgsKwargs, 

12 ModelFieldsSet, 

13 MultiHostUrl, 

14 PydanticCustomError, 

15 PydanticKnownError, 

16 PydanticOmit, 

17 PydanticSerializationError, 

18 PydanticSerializationUnexpectedValue, 

19 PydanticUndefined, 

20 PydanticUndefinedType, 

21 PydanticUseDefault, 

22 SchemaError, 

23 SchemaSerializer, 

24 SchemaValidator, 

25 Some, 

26 TzInfo, 

27 Url, 

28 ValidationError, 

29 __version__, 

30 from_json, 

31 to_json, 

32 to_jsonable_python, 

33) 

34from .core_schema import CoreConfig, CoreSchema, CoreSchemaType, ErrorType 1abcdefghijklmnopqrs

35 

36if _sys.version_info < (3, 12): 1abcdefghijklmnopqrs

37 from typing_extensions import TypedDict as _TypedDict 1agn

38else: 

39 from typing import TypedDict as _TypedDict 1bcdefhijklmopqrs

40 

41__all__ = [ 1abcdefghijklmnopqrs

42 'UNSET', 

43 'ArgsKwargs', 

44 'CoreConfig', 

45 'CoreSchema', 

46 'CoreSchemaType', 

47 'ErrorDetails', 

48 'InitErrorDetails', 

49 'ModelFieldsSet', 

50 'MultiHostUrl', 

51 'PydanticCustomError', 

52 'PydanticKnownError', 

53 'PydanticOmit', 

54 'PydanticSerializationError', 

55 'PydanticSerializationUnexpectedValue', 

56 'PydanticUndefined', 

57 'PydanticUndefinedType', 

58 'PydanticUseDefault', 

59 'SchemaError', 

60 'SchemaSerializer', 

61 'SchemaValidator', 

62 'Some', 

63 'TzInfo', 

64 'Url', 

65 'ValidationError', 

66 '__version__', 

67 'from_json', 

68 'to_json', 

69 'to_jsonable_python', 

70] 

71 

72 

73class ErrorDetails(_TypedDict): 1abcdefghijklmnopqrs

74 type: str 1abcdefghijklmnopqrs

75 """ 1abcdefghijklmnopqrs

76 The type of error that occurred, this is an identifier designed for 

77 programmatic use that will change rarely or never. 

78 

79 `type` is unique for each error message, and can hence be used as an identifier to build custom error messages. 

80 """ 

81 loc: tuple[int | str, ...] 1abcdefghijklmnopqrs

82 """Tuple of strings and ints identifying where in the schema the error occurred.""" 1abcdefghijklmnopqrs

83 msg: str 1abcdefghijklmnopqrs

84 """A human readable error message.""" 1abcdefghijklmnopqrs

85 input: _Any 1abcdefghijklmnopqrs

86 """The input data at this `loc` that caused the error.""" 1abcdefghijklmnopqrs

87 ctx: _NotRequired[dict[str, _Any]] 1abcdefghijklmnopqrs

88 """ 1abcdefghijklmnopqrs

89 Values which are required to render the error message, and could hence be useful in rendering custom error messages. 

90 Also useful for passing custom error data forward. 

91 """ 

92 url: _NotRequired[str] 1abcdefghijklmnopqrs

93 """ 1abcdefghijklmnopqrs

94 The documentation URL giving information about the error. No URL is available if 

95 a [`PydanticCustomError`][pydantic_core.PydanticCustomError] is used. 

96 """ 

97 

98 

99class InitErrorDetails(_TypedDict): 1abcdefghijklmnopqrs

100 type: str | PydanticCustomError 1abcdefghijklmnopqrs

101 """The type of error that occurred, this should be a "slug" identifier that changes rarely or never.""" 1abcdefghijklmnopqrs

102 loc: _NotRequired[tuple[int | str, ...]] 1abcdefghijklmnopqrs

103 """Tuple of strings and ints identifying where in the schema the error occurred.""" 1abcdefghijklmnopqrs

104 input: _Any 1abcdefghijklmnopqrs

105 """The input data at this `loc` that caused the error.""" 1abcdefghijklmnopqrs

106 ctx: _NotRequired[dict[str, _Any]] 1abcdefghijklmnopqrs

107 """ 1abcdefghijklmnopqrs

108 Values which are required to render the error message, and could hence be useful in rendering custom error messages. 

109 Also useful for passing custom error data forward. 

110 """ 

111 

112 

113class ErrorTypeInfo(_TypedDict): 1abcdefghijklmnopqrs

114 """ 

115 Gives information about errors. 

116 """ 

117 

118 type: ErrorType 1abcdefghijklmnopqrs

119 """The type of error that occurred, this should be a "slug" identifier that changes rarely or never.""" 1abcdefghijklmnopqrs

120 message_template_python: str 1abcdefghijklmnopqrs

121 """String template to render a human readable error message from using context, when the input is Python.""" 1abcdefghijklmnopqrs

122 example_message_python: str 1abcdefghijklmnopqrs

123 """Example of a human readable error message, when the input is Python.""" 1abcdefghijklmnopqrs

124 message_template_json: _NotRequired[str] 1abcdefghijklmnopqrs

125 """String template to render a human readable error message from using context, when the input is JSON data.""" 1abcdefghijklmnopqrs

126 example_message_json: _NotRequired[str] 1abcdefghijklmnopqrs

127 """Example of a human readable error message, when the input is JSON data.""" 1abcdefghijklmnopqrs

128 example_context: dict[str, _Any] | None 1abcdefghijklmnopqrs

129 """Example of context values.""" 1abcdefghijklmnopqrs

130 

131 

132class MultiHostHost(_TypedDict): 1abcdefghijklmnopqrs

133 """ 

134 A host part of a multi-host URL. 

135 """ 

136 

137 username: str | None 1abcdefghijklmnopqrs

138 """The username part of this host, or `None`.""" 1abcdefghijklmnopqrs

139 password: str | None 1abcdefghijklmnopqrs

140 """The password part of this host, or `None`.""" 1abcdefghijklmnopqrs

141 host: str | None 1abcdefghijklmnopqrs

142 """The host part of this host, or `None`.""" 1abcdefghijklmnopqrs

143 port: int | None 1abcdefghijklmnopqrs

144 """The port part of this host, or `None`.""" 1abcdefghijklmnopqrs

145 

146 

147MISSING = Sentinel('MISSING') 1abcdefghijklmnopqrs

148""" 1abcdefghijklmnopqrs

149/// version-added | v2.12 

150/// 

151 

152/// version-changed | v2.14 

153The `MISSING` sentinel is no longer experimental. 

154/// 

155 

156The `MISSING` sentinel is a singleton indicating a field value was not provided during validation. 

157 

158This singleton can be used as a default value, as an alternative to `None` when it has an explicit 

159meaning. During serialization, any field with `MISSING` as a value is excluded from the output. 

160 

161At runtime, the `MISSING` sentinel is implemented as a [`sentinel`](https://docs.python.org/3.15/library/functions.html#sentinel) instance. 

162 

163```python 

164from pydantic import MISSING, BaseModel 

165 

166 

167class Configuration(BaseModel): 

168 timeout: int | None | MISSING = MISSING 

169 

170 

171# configuration defaults, stored somewhere else: 

172defaults = {'timeout': 200} 

173 

174conf = Configuration() 

175 

176# `timeout` is excluded from the serialization output: 

177conf.model_dump() 

178# {} 

179 

180# The `MISSING` value doesn't appear in the JSON Schema: 

181Configuration.model_json_schema()['properties']['timeout'] 

182#> {'anyOf': [{'type': 'integer'}, {'type': 'null'}], 'title': 'Timeout'} 

183 

184 

185# `is` can be used to discriminate between the sentinel and other values: 

186timeout = conf.timeout if conf.timeout is not MISSING else defaults['timeout'] 

187``` 

188 

189!!! note 

190 When [applying constraints](./fields.md#field-constraints) to a union containing the `MISSING` sentinel, 

191 such constraints are automatically applied to the remaining type(s) of the union. 

192""" 

193 

194 

195MutableSet.register(ModelFieldsSet) 1abcdefghijklmnopqrs