pydantic / pydantic-settings

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

Values printed in ValidationError stack traces for BaseSettings #256

Closed sjking closed 6 months ago

sjking commented 6 months ago

I have some value stored in an environment variable that I am reading into a custom BaseSettings subclass. I am using a Field of type SecretStr, with repr=False for this value in the model class. If there is a validation error, and the first value is present, but some other value in the model is missing, then the stack trace includes the value that was present. Should this be default behaviour? Please see below for a reproducible example:

from pydantic import Field, SecretStr
from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    first_value: SecretStr = Field(validation_alias="MY_FIRST_VALUE", repr=False)
    other_value: str = Field(validation_alias="MY_OTHER_VALUE")

settings = Settings()
$ export MY_FIRST_VALUE=test123
$ python main.py 
...
pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
other_value
  Field required [type=missing, input_value={'MY_FIRST_VALUE: 'test123'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.6/v/missing
samuelcolvin commented 6 months ago

I think this is the same as https://github.com/pydantic/pydantic/issues/7461

samuelcolvin commented 6 months ago

but let's discuss an alternative.