-
Notifications
You must be signed in to change notification settings - Fork 34
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
Refactor Use pydantic-settings
for env vars
#28
Refactor Use pydantic-settings
for env vars
#28
Conversation
…ure/introduce-ruff-and-test-runner
This is awesome. I didn't know about pydantic-settings but it looks great. For Nice job on this PR! Article was interesting too :D Merging |
Just to make sure, we can get rid of the |
@ryansurf Also, thanks for checking out my article! :D |
Thanks for the follow-up question. |
Exactly, from my understanding the |
No, that's not quite right! |
# settings.py
class CommonSettings(BaseSettings):
"""
Base class for defining common settings.
model_config (SettingsConfigDict)Configuration dictionary
for specifying the settings file and encoding.
"""
model_config = SettingsConfigDict(
env_file=f"{Path(__file__).parent.parent}/.env",
env_file_encoding="utf-8",
extra="ignore",
) The |
Apologies for the lack of clarity in my explanation! To summarize:
Sorry for the confusion😵💫 |
Please point out if I'm missing the point of your response, as I may not have understood the exact intent of your question.🙂 |
Ah, that makes sense. Sorry, I was being a bit lame, you answered what I was asking 👍 |
@ryansurf
Sorry for bombarding you with pull requests one after another!
I refactored the environment variable loading process (
os.getenv()
) and usedpydantic-settings
to make it type-safe!Maybe you know, The benefits of
pydantic-settings
are:You can define the expected type for each environment variable using
pydantic-settings
. When the Python script runs, it automatically validates the loaded values against the specified types. This helps catch errors in the environment variable values early on, preventing the process from proceeding with incorrect settings.With
pydantic-settings
, you can define all the environment variables in a centralized configuration class(settings.py
). This makes it easier to manage and maintain the configuration settings in one place, improving code organization and readability.Thanks to this,
send_email.py
now throws an error if the email address isn't set, for example.This changed commit: c2f5bf5
I'd be stoked if you could merge this in!!
Also, I know this article is in Japanese, but I wanted to share a piece I wrote about
pydantic-settings
:)https://qiita.com/inetcpl/items/b4146b9e8e1adad239d8