pydantic / pydantic-settings

Settings management using pydantic
https://docs.pydantic.dev/latest/usage/pydantic_settings/
MIT License
643 stars 65 forks source link

Pydantic BaseSettings should raise when .env file specified in `env_file` does not exist #4

Open ralbertazzi opened 2 years ago

ralbertazzi commented 2 years ago

Initial Checks

Description

Thank you for adding settings management to Pydantic, it's a pleasure to use a single well-done library to manage both models and settings from .env files and environment variables! 😍

I'm not sure about its future in Pydantic V2 (👀 please don't remove it 🙏 ) since I can't find references in the main branch anymore. Still I'd like to mention a small improvement that could be done when reading .env files.

Currently, if the .env file does not exist it is simply ignored. I don't know if this is done by design but I would actually prefer a strict behaviour that raises an error asap.

What happened to me instead is that I provided a wrong path in the env_file and pydantic later raised with a ValidationError for missing fields that were specified in the .env file. I still consider this a lucky case because an error was eventually raised. What would have been worse is if my settings had defaults for every field, which would have resulted in no ValidationError being raised at all!

Let me know what do you think about it. If you have a strong preference for the current behaviour I guess I could always add some file existence check before parsing my settings. Still, it would be nice to have this done by Pydantic IMO. Could you also share an update on the planned support for settings in V2? In the online v2 plan there isn't a final answer, but I imagine you have already taken actions on it.

Thank you so much and keep making Pydantic one of the best libraries of the Python ecosystem 🚀

Affected Components

samuelcolvin commented 2 years ago

(transferred the issue to pydantic-settings)

Settings management will remain, but it'll be managed as a separate repo and pypi package.


On that issue, the idea is that you might have and .env.development (which is not available in production or on CI) and and .env.production which is only available in production.

So pydantic-settings shouldn't (by default) raise an error if an env file is missing.

Ideas to work around this:

WDYT?

samuelcolvin commented 2 years ago

We're also changing how settings works to use "sources" for all sources in V2, so it should be easy to customise the EnvFileSource to make files required.

samuelcolvin commented 2 years ago

Thank you so much and keep making Pydantic one of the best libraries of the Python ecosystem rocket

Thanks so much, comments like this mean a lot.

ralbertazzi commented 2 years ago

We're also changing how settings works to use "sources" for all sources in V2, so it should be easy to customise the EnvFileSource to make files required.

I think having an easy boolean in EnvFileSource to customize the behaviour is the best thing! At that point I'm fine with any default behaviour :)

I didn't think about the use case you mentioned. In my setup the .env files of every environment are committed in the repo and always available, while we manage secrets and extra configuration known at runtime with environment variables.

Thanks so much, comments like this mean a lot.

I truly mean it! I started being a lover of this library since the dark days of marshmallow 😛