Mac*_*ekS 5 validation python-3.x pydantic
confloat如果为其提供的值超出指定范围,则无法初始化具有字段的Pydantic 类。但在稍后阶段 ( my_object.constrained_field = <big_value>) 设置此字段时,新值不会被验证。
from pydantic import BaseModel, confloat
class testClass(BaseModel):
a: confloat(gt=0.0, lt=10.0)
x = testClass(a=2.0) # OK
# x = testClass(a=20.0) # ERROR: outside limits
x.a = 40.0 # OK despite outside limits
print(x.a) # prints '40.0'
Run Code Online (Sandbox Code Playgroud)
设置成员时是否可以验证新值,以便x.a = 40.0在上面的示例中调用会引发异常?
如果没有 - 是否有任何解决方法,例如从类字段中获取验证器方法?
| 归档时间: |
|
| 查看次数: |
4961 次 |
| 最近记录: |