Coverage for pydantic/errors.py: 100.00%
39 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-13 19:35 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-13 19:35 +0000
1"""Pydantic-specific errors."""
3from __future__ import annotations as _annotations 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
5import re 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
6from typing import Literal 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
8from typing_extensions import Self 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
10from ._migration import getattr_migration 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
11from .version import version_short 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
13__all__ = ( 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
14 'PydanticUserError',
15 'PydanticUndefinedAnnotation',
16 'PydanticImportError',
17 'PydanticSchemaGenerationError',
18 'PydanticInvalidForJsonSchema',
19 'PydanticErrorCodes',
20)
22# We use this URL to allow for future flexibility about how we host the docs, while allowing for Pydantic
23# code in the while with "old" URLs to still work.
24# 'u' refers to "user errors" - e.g. errors caused by developers using pydantic, as opposed to validation errors.
25DEV_ERROR_DOCS_URL = f'https://errors.pydantic.dev/{version_short()}/u/' 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
26PydanticErrorCodes = Literal[ 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
27 'class-not-fully-defined',
28 'custom-json-schema',
29 'decorator-missing-field',
30 'discriminator-no-field',
31 'discriminator-alias-type',
32 'discriminator-needs-literal',
33 'discriminator-alias',
34 'discriminator-validator',
35 'callable-discriminator-no-tag',
36 'typed-dict-version',
37 'model-field-overridden',
38 'model-field-missing-annotation',
39 'config-both',
40 'removed-kwargs',
41 'circular-reference-schema',
42 'invalid-for-json-schema',
43 'json-schema-already-used',
44 'base-model-instantiated',
45 'undefined-annotation',
46 'schema-for-unknown-type',
47 'import-error',
48 'create-model-field-definitions',
49 'create-model-config-base',
50 'validator-no-fields',
51 'validator-invalid-fields',
52 'validator-instance-method',
53 'validator-input-type',
54 'root-validator-pre-skip',
55 'model-serializer-instance-method',
56 'validator-field-config-info',
57 'validator-v1-signature',
58 'validator-signature',
59 'field-serializer-signature',
60 'model-serializer-signature',
61 'multiple-field-serializers',
62 'invalid-annotated-type',
63 'type-adapter-config-unused',
64 'root-model-extra',
65 'unevaluable-type-annotation',
66 'dataclass-init-false-extra-allow',
67 'clashing-init-and-init-var',
68 'model-config-invalid-field-name',
69 'with-config-on-model',
70 'dataclass-on-model',
71 'validate-call-type',
72 'unpack-typed-dict',
73 'overlapping-unpack-typed-dict',
74 'invalid-self-type',
75]
78class PydanticErrorMixin: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
79 """A mixin class for common functionality shared by all Pydantic-specific errors.
81 Attributes:
82 message: A message describing the error.
83 code: An optional error code from PydanticErrorCodes enum.
84 """
86 def __init__(self, message: str, *, code: PydanticErrorCodes | None) -> None: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
87 self.message = message 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
88 self.code = code 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
90 def __str__(self) -> str: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
91 if self.code is None: 1abghijklmnopcdqrstuvwxefyzABCDEF
92 return self.message 1abghijklmnopcdqrstuvwxefyzABCDEF
93 else:
94 return f'{self.message}\n\nFor further information visit {DEV_ERROR_DOCS_URL}{self.code}' 1abghijklmnopcdqrstuvwxefyzABCDEF
97class PydanticUserError(PydanticErrorMixin, TypeError): 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
98 """An error raised due to incorrect use of Pydantic."""
101class PydanticUndefinedAnnotation(PydanticErrorMixin, NameError): 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
102 """A subclass of `NameError` raised when handling undefined annotations during `CoreSchema` generation.
104 Attributes:
105 name: Name of the error.
106 message: Description of the error.
107 """
109 def __init__(self, name: str, message: str) -> None: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
110 self.name = name 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
111 super().__init__(message=message, code='undefined-annotation') 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
113 @classmethod 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
114 def from_name_error(cls, name_error: NameError) -> Self: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
115 """Convert a `NameError` to a `PydanticUndefinedAnnotation` error.
117 Args:
118 name_error: `NameError` to be converted.
120 Returns:
121 Converted `PydanticUndefinedAnnotation` error.
122 """
123 try: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
124 name = name_error.name # type: ignore # python > 3.10 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
125 except AttributeError: 1abcdGef
126 name = re.search(r".*'(.+?)'", str(name_error)).group(1) # type: ignore[union-attr] 1abcdGef
127 return cls(name=name, message=str(name_error)) 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
130class PydanticImportError(PydanticErrorMixin, ImportError): 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
131 """An error raised when an import fails due to module changes between V1 and V2.
133 Attributes:
134 message: Description of the error.
135 """
137 def __init__(self, message: str) -> None: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
138 super().__init__(message, code='import-error') 1abghijklmnopcdqrstuvwxefyzABCDEF
141class PydanticSchemaGenerationError(PydanticUserError): 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
142 """An error raised during failures to generate a `CoreSchema` for some type.
144 Attributes:
145 message: Description of the error.
146 """
148 def __init__(self, message: str) -> None: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
149 super().__init__(message, code='schema-for-unknown-type') 1abghijklmnopcdqrstuvwxefyzABCDEF
152class PydanticInvalidForJsonSchema(PydanticUserError): 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
153 """An error raised during failures to generate a JSON schema for some `CoreSchema`.
155 Attributes:
156 message: Description of the error.
157 """
159 def __init__(self, message: str) -> None: 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF
160 super().__init__(message, code='invalid-for-json-schema') 1abghijklmnopcdqrstuvwxefyzABCDEF
163__getattr__ = getattr_migration(__name__) 1abghijklmnopcdqrstuvwxGHIJKLMefyzABCDEF