AttributeError: type object 'FilePath' has no attribute '_flavour' [pydantic]

AttributeError: type object 'FilePath' has no attribute '_flavour' [pydantic]

我使用 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。

我认为您需要以字符串形式提供默认值:

from pydantic import BaseModel, FilePath


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


model = Model()

编辑: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:这是 intended behavior,如果你想要这个检查,你可以使用 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)