Skip to content

Add support for conversion from str in pydantic-settings #296

Open
@BartSchuurmans

Description

@BartSchuurmans
from pathlib import Path
from upath import UPath
from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    my_path: Path
    my_upath: UPath

settings = Settings(my_path="/tmp", my_upath="/tmp")
❯ python example.py
Traceback (most recent call last):
  File "/home/bart/src/proj/example.py", line 9, in <module>
    settings = Settings(my_path="/tmp", my_upath="/tmp")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/bart/src/proj/.venv/lib/python3.12/site-packages/pydantic_settings/main.py", line 152, in __init__
    super().__init__(
  File "/home/bart/src/proj/.venv/lib/python3.12/site-packages/pydantic/main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
my_upath
  Input should be an instance of UPath [type=is_instance_of, input_value='/tmp', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/is_instance_of

When reading through some older (solved) issues, I got the impression that this used to work. Should this work out of the box?

pydantic                  2.9.2          Data validation using Python type ...
pydantic-core             2.23.4         Core functionality for Pydantic va...
pydantic-settings         2.5.2          Settings management using Pydantic
universal-pathlib         0.2.5          pathlib api extended to use fsspec...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions