attributeError:键入对象' filepath'没有属性' _flavour' [pydantic]

发布于 2025-01-18 15:07:37 字数 1016 浏览 2 评论 0原文

我使用pydantic进行数据验证。我想做的是创建一个具有可选字段的模型,该模型指向现有文件。问题是,下面的代码不起作用。

from pydantic import BaseModel, FilePath


class Model(BaseModel):
    # Assuming I have file.txt in working directory.
    file: FilePath = FilePath('./file.txt')


model = Model()
Traceback (most recent call last):
  File "C:\Users\mikheenkov\temp.py", line 4, in <module>
    class Model(BaseModel):
  File "C:\Users\mikheenkov\temp.py", line 5, in Model
    ssh_server_host_key: FilePath = FilePath('.')
  File "C:\Program Files\Python39\lib\pathlib.py", line 1082, in __new__
    self = cls._from_parts(args, init=False)
  File "C:\Program Files\Python39\lib\pathlib.py", line 707, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "C:\Program Files\Python39\lib\pathlib.py", line 700, in _parse_args
    return cls._flavour.parse_parts(parts)
AttributeError: type object 'FilePath' has no attribute '_flavour'

有解决方法吗?

编辑:Python版本为3.9.8,Pydantic版本为1.9.0。

I use pydantic for data validation. What I want to do is to create a model with an optional field, which points to the existing file. The problem is, the code below does not work.

from pydantic import BaseModel, FilePath


class Model(BaseModel):
    # Assuming I have file.txt in working directory.
    file: FilePath = FilePath('./file.txt')


model = Model()
Traceback (most recent call last):
  File "C:\Users\mikheenkov\temp.py", line 4, in <module>
    class Model(BaseModel):
  File "C:\Users\mikheenkov\temp.py", line 5, in Model
    ssh_server_host_key: FilePath = FilePath('.')
  File "C:\Program Files\Python39\lib\pathlib.py", line 1082, in __new__
    self = cls._from_parts(args, init=False)
  File "C:\Program Files\Python39\lib\pathlib.py", line 707, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "C:\Program Files\Python39\lib\pathlib.py", line 700, in _parse_args
    return cls._flavour.parse_parts(parts)
AttributeError: type object 'FilePath' has no attribute '_flavour'

Is there a workaround?

EDIT: Python version is 3.9.8 and pydantic version is 1.9.0.

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

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

发布评论

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

评论(1

眼前雾蒙蒙 2025-01-25 15:07:37

我认为您需要将默认值作为字符串提供:

from pydantic import BaseModel, FilePath


class Model(BaseModel):
    # Assuming I have file.txt in working directory.
    file: FilePath = './file.txt'


model = Model()

edit:pydantic似乎没有检查默认值是存在的文件:

In [18]: class Model(BaseModel):
    ...:     # Assuming no_such_file.txt does not exist
    ...:     file: FilePath = './no_such_file.txt'
    ...: 

In [19]: Model()
Out[19]: Model(file='./no_such_file.txt')

但是它确实检查了是否提供:

In [20]: Model(file="./no_such_file.txt")
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-20-e6e0f3b40736> in <module>
----> 1 Model(file="./no_such_file.txt")

~/.pyenv/versions/3.8.12/envs/uc09/lib/python3.8/site-packages/pydantic/main.cpython-38-x86_64-linux-gnu.so in pydantic.main.BaseModel.__init__()

ValidationError: 1 validation error for Model
file
  file or directory at path "no_such_file.txt" does not exist (type=value_error.path.not_exists; path=no_such_file.txt)

edit2:这是预期的行为,如果您想要此检查,可以使用config.validate_all = true = true :


In [29]: class Model(BaseModel):
    ...:     # Assuming no_such_file.txt does not exist
    ...:     file: FilePath = './no_such_file.txt'
    ...:     class Config:
    ...:         validate_all = True
    ...: 

In [30]: model = Model()
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-30-e3d397de7fe1> in <module>
----> 1 model = Model()

~/.pyenv/versions/3.8.12/envs/uc09/lib/python3.8/site-packages/pydantic/main.cpython-38-x86_64-linux-gnu.so in pydantic.main.BaseModel.__init__()

ValidationError: 1 validation error for Model
file
  file or directory at path "no_such_file.txt" does not exist (type=value_error.path.not_exists; path=no_such_file.txt)

I think that you need to provide the default value as a string:

from pydantic import BaseModel, FilePath


class Model(BaseModel):
    # Assuming I have file.txt in working directory.
    file: FilePath = './file.txt'


model = Model()

EDIT: pydantic does not seem to check that the default value is a file that exists:

In [18]: class Model(BaseModel):
    ...:     # Assuming no_such_file.txt does not exist
    ...:     file: FilePath = './no_such_file.txt'
    ...: 

In [19]: Model()
Out[19]: Model(file='./no_such_file.txt')

But it does check if you supply it:

In [20]: Model(file="./no_such_file.txt")
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-20-e6e0f3b40736> in <module>
----> 1 Model(file="./no_such_file.txt")

~/.pyenv/versions/3.8.12/envs/uc09/lib/python3.8/site-packages/pydantic/main.cpython-38-x86_64-linux-gnu.so in pydantic.main.BaseModel.__init__()

ValidationError: 1 validation error for Model
file
  file or directory at path "no_such_file.txt" does not exist (type=value_error.path.not_exists; path=no_such_file.txt)

EDIT2: This is the intended behavior, and if you want this check you can use Config.validate_all = True:


In [29]: class Model(BaseModel):
    ...:     # Assuming no_such_file.txt does not exist
    ...:     file: FilePath = './no_such_file.txt'
    ...:     class Config:
    ...:         validate_all = True
    ...: 

In [30]: model = Model()
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-30-e3d397de7fe1> in <module>
----> 1 model = Model()

~/.pyenv/versions/3.8.12/envs/uc09/lib/python3.8/site-packages/pydantic/main.cpython-38-x86_64-linux-gnu.so in pydantic.main.BaseModel.__init__()

ValidationError: 1 validation error for Model
file
  file or directory at path "no_such_file.txt" does not exist (type=value_error.path.not_exists; path=no_such_file.txt)

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