跳转到内容

错误

Pydantic 特定的错误。

PydanticErrorMixin

PydanticErrorMixin(
    message: str, *, code: PydanticErrorCodes | None
)

一个混入类 (mixin class),用于所有 Pydantic 特定错误共享的通用功能。

属性

名称 类型 描述
message

描述错误的消息。

code

一个可选的错误代码,来自 PydanticErrorCodes 枚举。

源代码位于 pydantic/errors.py
90
91
92
def __init__(self, message: str, *, code: PydanticErrorCodes | None) -> None:
    self.message = message
    self.code = code

PydanticUserError

PydanticUserError(
    message: str, *, code: PydanticErrorCodes | None
)

基类: PydanticErrorMixin, TypeError

因不正确使用 Pydantic 而引发的错误。

源代码位于 pydantic/errors.py
90
91
92
def __init__(self, message: str, *, code: PydanticErrorCodes | None) -> None:
    self.message = message
    self.code = code

PydanticUndefinedAnnotation

PydanticUndefinedAnnotation(name: str, message: str)

基类: PydanticErrorMixin, NameError

在生成 CoreSchema 期间,处理未定义的注解时引发的 NameError 的子类。

属性

名称 类型 描述
name

错误的名称。

message

错误的描述。

源代码位于 pydantic/errors.py
113
114
115
def __init__(self, name: str, message: str) -> None:
    self.name = name
    super().__init__(message=message, code='undefined-annotation')

from_name_error classmethod

from_name_error(name_error: NameError) -> Self

NameError 转换为 PydanticUndefinedAnnotation 错误。

参数

名称 类型 描述 默认值
name_error NameError

要转换的 NameError

必需

返回

类型 描述
Self

转换后的 PydanticUndefinedAnnotation 错误。

源代码位于 pydantic/errors.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
@classmethod
def from_name_error(cls, name_error: NameError) -> Self:
    """Convert a `NameError` to a `PydanticUndefinedAnnotation` error.

    Args:
        name_error: `NameError` to be converted.

    Returns:
        Converted `PydanticUndefinedAnnotation` error.
    """
    try:
        name = name_error.name  # type: ignore  # python > 3.10
    except AttributeError:
        name = re.search(r".*'(.+?)'", str(name_error)).group(1)  # type: ignore[union-attr]
    return cls(name=name, message=str(name_error))

PydanticImportError

PydanticImportError(message: str)

基类: PydanticErrorMixin, ImportError

当由于 V1 和 V2 之间的模块变更导致导入失败时引发的错误。

属性

名称 类型 描述
message

错误的描述。

源代码位于 pydantic/errors.py
141
142
def __init__(self, message: str) -> None:
    super().__init__(message, code='import-error')

PydanticSchemaGenerationError

PydanticSchemaGenerationError(message: str)

基类: PydanticUserError

当为某个类型生成 CoreSchema 失败时引发的错误。

属性

名称 类型 描述
message

错误的描述。

源代码位于 pydantic/errors.py
152
153
def __init__(self, message: str) -> None:
    super().__init__(message, code='schema-for-unknown-type')

PydanticInvalidForJsonSchema

PydanticInvalidForJsonSchema(message: str)

基类: PydanticUserError

当为某个 CoreSchema 生成 JSON schema 失败时引发的错误。

属性

名称 类型 描述
message

错误的描述。

源代码位于 pydantic/errors.py
163
164
def __init__(self, message: str) -> None:
    super().__init__(message, code='invalid-for-json-schema')

PydanticForbiddenQualifier

PydanticForbiddenQualifier(
    qualifier: Qualifier, annotation: Any
)

基类: PydanticUserError

如果在类型注解中发现禁止使用的类型限定符时引发的错误。

源代码位于 pydantic/errors.py
179
180
181
182
183
184
185
186
def __init__(self, qualifier: Qualifier, annotation: Any) -> None:
    super().__init__(
        message=(
            f'The annotation {_repr.display_as_type(annotation)!r} contains the {self._qualifier_repr_map[qualifier]!r} '
            f'type qualifier, which is invalid in the context it is defined.'
        ),
        code=None,
    )