I use pydantic-settings
to collect configuration parameters from a variety of sources (config.yaml, .env, environment variables, …). The settings object is stored as a module variable in a similar fashion to how it is shown for FastAPI here:
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
app_name: str = "Awesome API"
admin_email: str
items_per_user: int = 50
settings = Settings()
The only difference is that the label is capitalised and is imported by other modules (from my_app.config import SETTINGS
). This works quite well. However, when I use pdoc
to generate documentation it will use __repr__
/__str__
to document the value of SETTINGS
:
SETTINGS: Settings =
Settings(api=APIConfig(token=SecretStr('**********'), endpoint=SecretStr('**********')), logging=LoggingConfig(level=<LogLevel.DEBUG: 'debug'>), uid=501, device_info=None)
This leaks environment information and in the worst case even credentials, urls or session tokens that have been typed with str
instead of SecretStr
. I don’t want to override __repr__
as it might be useful to log settings during runtime. However, for documentation purposes SETTINGS: Settings
without a value should be sufficient as the actual value depends on the user’s environment. SETTINGS
should be documented though, so readers know it exists and can be accessed.
Is there a way to tell pdoc or pydantic(-settings) to not document the module variable’s value? Or is there a better pattern to load and share settings across modules?