Coverage for /pythoncovmergedfiles/medio/medio/src/pydantic/pydantic/errors.py: 65%

37 statements  

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

1""" 

2Pydantic errors. 

3""" 

4from __future__ import annotations as _annotations 

5 

6import re 

7 

8from typing_extensions import Literal 

9 

10from ._migration import getattr_migration 

11 

12__all__ = ( 

13 'PydanticUserError', 

14 'PydanticUndefinedAnnotation', 

15 'PydanticImportError', 

16 'PydanticSchemaGenerationError', 

17 'PydanticInvalidForJsonSchema', 

18) 

19 

20# TODO set up a cloudflare worker to redirect to the correct page 

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. 

24# DEV_ERROR_DOCS_URL = f'https://errors.pydantic.dev/{VERSION}/u/' 

25DEV_ERROR_DOCS_URL = '<TODO: Set up the errors URLs>/' 

26PydanticErrorCodes = Literal[ 

27 'decorator-missing-field', 

28 'dataclass-not-fully-defined', 

29 'discriminator-no-field', 

30 'discriminator-alias-type', 

31 'discriminator-needs-literal', 

32 'discriminator-alias', 

33 'typed-dict-version', 

34 'model-field-overridden', 

35 'model-field-missing-annotation', 

36 'model-not-fully-defined', 

37 'config-both', 

38 'deprecated_kwargs', 

39 'invalid-for-json-schema', 

40 'json-schema-already-used', 

41 'base-model-instantiated', 

42 'undefined-annotation', 

43 'schema-for-unknown-type', 

44 'import-error', 

45 'create-model-field-definitions', 

46 'create-model-config-base', 

47 'validator-no-fields', 

48 'validator-invalid-fields', 

49 'validator-instance-method', 

50 'root-validator-pre-skip', 

51 'model-serializer-instance-method', 

52 'validator-field-config-info', 

53 'validator-v1-signature', 

54 'field-validator-signature', 

55 'field-serializer-signature', 

56 'model-serializer-signature', 

57 'multiple-field-serializers', 

58] 

59 

60 

61class PydanticErrorMixin: 

62 """ 

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

64 

65 Attributes: 

66 message (str): A message describing the error. 

67 code (PydanticErrorCodes | None): An optional error code from PydanticErrorCodes enum. 

68 """ 

69 

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

71 self.message = message 

72 self.code = code 

73 

74 def __str__(self) -> str: 

75 if self.code is None: 

76 return self.message 

77 else: 

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

79 

80 

81class PydanticUserError(PydanticErrorMixin, TypeError): 

82 """ 

83 Error raised due to incorrect use of Pydantic. 

84 """ 

85 

86 

87class PydanticUndefinedAnnotation(PydanticErrorMixin, NameError): 

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

89 

90 Attributes: 

91 name (str): Name of the error. 

92 message (str): Description of the error. 

93 """ 

94 

95 def __init__(self, name: str, message: str) -> None: 

96 self.name = name 

97 super().__init__(message=message, code='undefined-annotation') 

98 

99 @classmethod 

100 def from_name_error(cls, name_error: NameError) -> PydanticUndefinedAnnotation: 

101 """ 

102 Convert a `NameError` to a `PydanticUndefinedAnnotation` error. 

103 

104 Args: 

105 name_error (NameError): `NameError` to be converted. 

106 

107 Returns: 

108 PydanticUndefinedAnnotation: Converted `PydanticUndefinedAnnotation` error. 

109 """ 

110 try: 

111 name = name_error.name # type: ignore # python > 3.10 

112 except AttributeError: 

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

114 return cls(name=name, message=str(name_error)) 

115 

116 

117class PydanticImportError(PydanticErrorMixin, ImportError): 

118 """Error occurs when an import fails due to module changes between V1 and V2.""" 

119 

120 def __init__(self, message: str) -> None: 

121 super().__init__(message, code='import-error') 

122 

123 

124class PydanticSchemaGenerationError(PydanticUserError): 

125 """ 

126 Error raised during failures to generate a `CoreSchema` for some type. 

127 

128 Attributes: 

129 message (str): Description of the error. 

130 """ 

131 

132 def __init__(self, message: str) -> None: 

133 super().__init__(message, code='schema-for-unknown-type') 

134 

135 

136class PydanticInvalidForJsonSchema(PydanticUserError): 

137 """ 

138 Error raised during failures to generate a JSON schema for some `CoreSchema`. 

139 

140 Attributes: 

141 message (str): Description of the error. 

142 """ 

143 

144 def __init__(self, message: str) -> None: 

145 super().__init__(message, code='invalid-for-json-schema') 

146 

147 

148__getattr__ = getattr_migration(__name__)