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.