Created
August 24, 2022 16:10
-
-
Save sebastianschramm/6e4b7c9395a878dcb6952b1dfaa41795 to your computer and use it in GitHub Desktop.
How to read ENVs with pydantic.BaseSettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydantic import BaseSettings, Field, SecretStr | |
class Credentials(BaseSettings): | |
password: SecretStr = Field(..., env="DB_PASSWORD") | |
""" | |
>>> Credentials() | |
pydantic.error_wrappers.ValidationError: 1 validation error for Credentials | |
password | |
field required (type=value_error.missing) | |
>>> os.environ['DB_PASSWORD'] = 'admin' | |
>>> Credentials() | |
Credentials(password=SecretStr('**********')) | |
>>> Credentials(password='adminadmin') | |
Credentials(password=SecretStr('**********')) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dependencies: python3.9, pydantic==1.9.2