FastAPI错误:响应字段无效的参数!提示:检查<class 'typing._UnionGenericAlias'>是否是有效的pydantic字段类型。

5
我有以下用于FastAPI应用程序的Pydantic模式。
在以下架构中,每当我将ParameterSchema用作params的模式验证器时,它会给我以下错误:
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'typing._GenericAlias'> is a valid pydantic field type

我完全不知道发生了什么事情!

class ParameterSchema(BaseModel):
    expiryDate = Optional[datetime]

    class Config:
        arbitrary_types_allowed = True


class RequestProvisioningEventData(BaseModel):
    some_attribute: List[str]
    other_attribute: Optional[List[str]] = []
    bool_attribute: bool
    params: ParameterSchema

    class Config:
        use_enum_values = True

7
你的 ParameterSchema 中那个赋值符号不应该是等号吗?expiryDate: Optional[datetime] - Arne
1个回答

5
这是因为expiryDate被赋予了一个值。
class ParameterSchema(BaseModel):
    expiryDate = Optional[datetime]

应该使用类型提示 (:):

class ParameterSchema(BaseModel):
    expiryDate: Optional[datetime]

注意在类型提示中使用冒号:,而不是等于号=


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接