Pydantic:如何为领域选择类型的选择?

发布于 2025-01-25 00:54:44 字数 575 浏览 2 评论 0原文

我有一个FastApi应用程序,需要创建一个汽车类,其中属性 wheel speed 可以采用INT str类型。怎么做?此代码不起作用,因为 wheel speed 将只有一个整数类型(不是第二次打印中的str):

from pydantic import BaseModel


class Car(BaseModel):
    wheel: int | str
    speed: int | str


bmw = Car(wheel=4, speed=250)
mercedes = Car(wheel='4', speed='200')

print(type(bmw.wheel), type(bmw.speed))
print(type(mercedes.wheel), type(mercedes.speed))

结果是:

<class 'int'> <class 'int'>
<class 'int'> <class 'int'>

I have a FastAPI app and I need to create a Car class in which the attributes wheel and speed can take an int or str type. How to do it? This code does not work, because wheel and speed will have only an integer type (not str in second print):

from pydantic import BaseModel


class Car(BaseModel):
    wheel: int | str
    speed: int | str


bmw = Car(wheel=4, speed=250)
mercedes = Car(wheel='4', speed='200')

print(type(bmw.wheel), type(bmw.speed))
print(type(mercedes.wheel), type(mercedes.speed))

Result is:

<class 'int'> <class 'int'>
<class 'int'> <class 'int'>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一向肩并 2025-02-01 00:54:44

因此,我个人会亲自使用pydantic.strictintpydantic.stricstr在此处(实际上,我几乎在任何地方都使用这些,尤其是stricts strictstr,因为实际上任何对象都可以胁迫到字符串):

import pydantic


class Car(pydantic.BaseModel):
    wheel: pydantic.StrictInt | pydantic.StrictStr
    speed: pydantic.StrictInt | pydantic.StrictStr


bmw = Car(wheel=4, speed=250)
mercedes = Car(wheel='4', speed='200')

print(type(bmw.wheel), type(bmw.speed))
print(type(mercedes.wheel), type(mercedes.speed))

此打印:

<class 'int'> <class 'int'>
<class 'str'> <class 'str'>

So, I would personally just use pydantic.StrictInt and pydantic.StricStr here (and actually, I use those almost everywhere, particularly StrictStr because practically any object can be coerced to a string):

import pydantic


class Car(pydantic.BaseModel):
    wheel: pydantic.StrictInt | pydantic.StrictStr
    speed: pydantic.StrictInt | pydantic.StrictStr


bmw = Car(wheel=4, speed=250)
mercedes = Car(wheel='4', speed='200')

print(type(bmw.wheel), type(bmw.speed))
print(type(mercedes.wheel), type(mercedes.speed))

This prints:

<class 'int'> <class 'int'>
<class 'str'> <class 'str'>
奈何桥上唱咆哮 2025-02-01 00:54:44

类型规范中有顺序。使用int | str,该值将被视为int,否则str。逆转顺序str | int将导致值将其视为str,否则int。反向订单的问题在于,几乎所有内容都可以视为str,因此bmw值将被施加到str。示例:

from pydantic import BaseModel


class Car(BaseModel):
    wheel: str | int
    speed: str | int


bmw = Car(wheel=4, speed=250)
mercedes = Car(wheel='4', speed='200')

print(type(bmw.wheel), type(bmw.speed))
print(type(mercedes.wheel), type(mercedes.speed))
<class 'str'> <class 'str'>
<class 'str'> <class 'str'>

这里的关键是您需要选择哪种类型优先。

There is order in the type specification. With int | str, the value will be treated as an int if possible, otherwise a str. Reversing the order str | int will result in the values being treated as str if possible, otherwise int. The problem with reversing the order is that pretty much everything can be treated as a str so the bmw values will be cast to str. Example:

from pydantic import BaseModel


class Car(BaseModel):
    wheel: str | int
    speed: str | int


bmw = Car(wheel=4, speed=250)
mercedes = Car(wheel='4', speed='200')

print(type(bmw.wheel), type(bmw.speed))
print(type(mercedes.wheel), type(mercedes.speed))
<class 'str'> <class 'str'>
<class 'str'> <class 'str'>

The key here is that you need to choose which type takes precedence.

难以启齿的温柔 2025-02-01 00:54:44

您正在寻找的是键入的联合选项。一个示例在下面

from typing import Union
from pydantic import BaseModel

class Car(BaseModel):
    wheel: Union[str,int]
    speed: Union[str,int]

,而不是简单的str或int,您可以在pydantic中为这些类型编写自己的类,并根据需要添加更多属性。

What you are looking for is the Union option from typing. An example is below

from typing import Union
from pydantic import BaseModel

class Car(BaseModel):
    wheel: Union[str,int]
    speed: Union[str,int]

Further, instead of simple str or int you can write your own classes for those types in pydantic and add more attributes as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文