Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UPath does not play with pydantic-settings like it used to #112

Closed
jorenretel opened this issue Jul 4, 2023 · 2 comments · Fixed by #127
Closed

UPath does not play with pydantic-settings like it used to #112

jorenretel opened this issue Jul 4, 2023 · 2 comments · Fixed by #127

Comments

@jorenretel
Copy link
Contributor

jorenretel commented Jul 4, 2023

Hi,

I noticed that a pattern I was using before is not working well anymore. I am using pydantic-settings (version 2.0.0), which is now a separate package, for settings management. Possibly I should create this issue at pydantic-settings instead of here. My excuses if that is the case, I can also do that of course.

What I observed is the following. It used to be possible to do this:

from dotenv import find_dotenv
from pydantic import BaseConfig
from pydantic_settings import BaseSettings
from upath import UPath


class Settings(BaseSettings):

    example_path: UPath = UPath(__file__).parent.parent / "example_data"

    class Config(BaseConfig):
        env_prefix = "my_env_"
        env_file = find_dotenv(".env")
        env_file_encoding = 'utf-8'

and I created a .env file with:

MY_ENV_EXAMPLE_PATH=/some/other/path

That worked well in the past. The string in the .env file was converted to a UPath object by pydantic.

Now it gives me this error:

ImportError while loading conftest '..../tests/conftest.py'.
tests/conftest.py:6: in <module>
    from bigwig_loader.collection import BigWigCollection
bigwig_loader/__init__.py:15: in <module>
    config = _Settings()
mambaforge/envs/bigwig/lib/python3.11/site-packages/pydantic_settings/main.py:61: in __init__
    super().__init__(
E   pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
E   example_data_dir
E     Input should be an instance of UPath [type=is_instance_of, input_value=PosixPath('/bla/bla/r.../example_data'), input_type=PosixPath]
E       For further information visit https://errors.pydantic.dev/2.0.1/v/is_instance_of

It seems like pydantic-settings is more strict than it used to be or something changed with the types of universal_pathlib?

universal_pathlib version: 0.0.23
operating system: RHEL 7.9 and macOS

@jorenretel
Copy link
Contributor Author

jorenretel commented Jul 4, 2023

I found out that this is not just happening with the .env file. It actually is just variable assignment that already causes the problem. If I make the minimal change to:

from dotenv import find_dotenv
from pydantic import BaseConfig
from pydantic_settings import BaseSettings
from upath import UPath

class Settings(BaseSettings):

    example_path: Path = UPath(__file__).parent.parent / "example_data"

    class Config(BaseConfig):
        env_prefix = "my_env_"
        env_file = find_dotenv(".env")
        env_file_encoding = 'utf-8'

the error is gone. But that is a pity as it reduces flexibility, because when I now have "s3:/some/path" in my .env file it is converted to a pathlib.Path object instead of a UPath object.

@ap-- ap-- changed the title UPath does not play with pedantic-settings like it used to UPath does not play with pydantic-settings like it used to Jul 5, 2023
@ap--
Copy link
Collaborator

ap-- commented Jul 6, 2023

This should be resolved once #90 is fixed.
I'm currently working on a big refactor. So we should be able to have this working soon.

This was referenced Aug 2, 2023
@ap-- ap-- closed this as completed in #127 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants