Coverage for pydantic/errors.py: 100.00%

38 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-03 19:29 +0000

1"""Pydantic-specific errors.""" 

2 

3from __future__ import annotations as _annotations 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

4 

5import re 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

6 

7from typing_extensions import Literal, Self 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

8 

9from ._migration import getattr_migration 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

10from .version import version_short 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

11 

12__all__ = ( 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

13 'PydanticUserError', 

14 'PydanticUndefinedAnnotation', 

15 'PydanticImportError', 

16 'PydanticSchemaGenerationError', 

17 'PydanticInvalidForJsonSchema', 

18 'PydanticErrorCodes', 

19) 

20 

21# We use this URL to allow for future flexibility about how we host the docs, while allowing for Pydantic 

22# code in the while with "old" URLs to still work. 

23# 'u' refers to "user errors" - e.g. errors caused by developers using pydantic, as opposed to validation errors. 

24DEV_ERROR_DOCS_URL = f'https://errors.pydantic.dev/{version_short()}/u/' 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

25PydanticErrorCodes = Literal[ 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

26 'class-not-fully-defined', 

27 'custom-json-schema', 

28 'decorator-missing-field', 

29 'discriminator-no-field', 

30 'discriminator-alias-type', 

31 'discriminator-needs-literal', 

32 'discriminator-alias', 

33 'discriminator-validator', 

34 'callable-discriminator-no-tag', 

35 'typed-dict-version', 

36 'model-field-overridden', 

37 'model-field-missing-annotation', 

38 'config-both', 

39 'removed-kwargs', 

40 'invalid-for-json-schema', 

41 'json-schema-already-used', 

42 'base-model-instantiated', 

43 'undefined-annotation', 

44 'schema-for-unknown-type', 

45 'import-error', 

46 'create-model-field-definitions', 

47 'create-model-config-base', 

48 'validator-no-fields', 

49 'validator-invalid-fields', 

50 'validator-instance-method', 

51 'root-validator-pre-skip', 

52 'model-serializer-instance-method', 

53 'validator-field-config-info', 

54 'validator-v1-signature', 

55 'validator-signature', 

56 'field-serializer-signature', 

57 'model-serializer-signature', 

58 'multiple-field-serializers', 

59 'invalid_annotated_type', 

60 'type-adapter-config-unused', 

61 'root-model-extra', 

62 'unevaluable-type-annotation', 

63 'dataclass-init-false-extra-allow', 

64 'clashing-init-and-init-var', 

65 'model-config-invalid-field-name', 

66 'with-config-on-model', 

67 'dataclass-on-model', 

68] 

69 

70 

71class PydanticErrorMixin: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

72 """A mixin class for common functionality shared by all Pydantic-specific errors. 

73 

74 Attributes: 

75 message: A message describing the error. 

76 code: An optional error code from PydanticErrorCodes enum. 

77 """ 

78 

79 def __init__(self, message: str, *, code: PydanticErrorCodes | None) -> None: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

80 self.message = message 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

81 self.code = code 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

82 

83 def __str__(self) -> str: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

84 if self.code is None: 1abcdmnopqrstuvefghwxyzABCDijklEFGHIJKL

85 return self.message 1abcdmnopqrstuvefghwxyzABCDijklEFGHIJKL

86 else: 

87 return f'{self.message}\n\nFor further information visit {DEV_ERROR_DOCS_URL}{self.code}' 1abcdmnopqrstuvefghwxyzABCDijklEFGHIJKL

88 

89 

90class PydanticUserError(PydanticErrorMixin, TypeError): 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

91 """An error raised due to incorrect use of Pydantic.""" 

92 

93 

94class PydanticUndefinedAnnotation(PydanticErrorMixin, NameError): 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

95 """A subclass of `NameError` raised when handling undefined annotations during `CoreSchema` generation. 

96 

97 Attributes: 

98 name: Name of the error. 

99 message: Description of the error. 

100 """ 

101 

102 def __init__(self, name: str, message: str) -> None: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

103 self.name = name 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

104 super().__init__(message=message, code='undefined-annotation') 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

105 

106 @classmethod 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

107 def from_name_error(cls, name_error: NameError) -> Self: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

108 """Convert a `NameError` to a `PydanticUndefinedAnnotation` error. 

109 

110 Args: 

111 name_error: `NameError` to be converted. 

112 

113 Returns: 

114 Converted `PydanticUndefinedAnnotation` error. 

115 """ 

116 try: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

117 name = name_error.name # type: ignore # python > 3.10 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

118 except AttributeError: 1abcdefghMNijkl

119 name = re.search(r".*'(.+?)'", str(name_error)).group(1) # type: ignore[union-attr] 1abcdefghMNijkl

120 return cls(name=name, message=str(name_error)) 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

121 

122 

123class PydanticImportError(PydanticErrorMixin, ImportError): 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

124 """An error raised when an import fails due to module changes between V1 and V2. 

125 

126 Attributes: 

127 message: Description of the error. 

128 """ 

129 

130 def __init__(self, message: str) -> None: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

131 super().__init__(message, code='import-error') 1abcdmnopqrstuvefghwxyzABCDijklEFGHIJKL

132 

133 

134class PydanticSchemaGenerationError(PydanticUserError): 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

135 """An error raised during failures to generate a `CoreSchema` for some type. 

136 

137 Attributes: 

138 message: Description of the error. 

139 """ 

140 

141 def __init__(self, message: str) -> None: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

142 super().__init__(message, code='schema-for-unknown-type') 1abcdmnopqrstuvefghwxyzABCDijklEFGHIJKL

143 

144 

145class PydanticInvalidForJsonSchema(PydanticUserError): 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

146 """An error raised during failures to generate a JSON schema for some `CoreSchema`. 

147 

148 Attributes: 

149 message: Description of the error. 

150 """ 

151 

152 def __init__(self, message: str) -> None: 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL

153 super().__init__(message, code='invalid-for-json-schema') 1abcdmnopqrstuvefghwxyzABCDijklEFGHIJKL

154 

155 

156__getattr__ = getattr_migration(__name__) 1abcdmnopqrstuvefghwxyzABCDMNOPQRSTUVijklEFGHIJKL