-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
Feature request: Add _init_ignore_none
to BaseSettings
#247
Comments
_init_ignore_empty
to BaseSettings_init_ignore_none
to BaseSettings
Thanks @jjeff07 for this feature request. You are setting explicitly the value for |
If I am allowed to set import os
import httpx
from pydantic import BaseModel
from pydantic_settings import BaseSettings
os.environ['password'] = 'world'
class Settings(BaseSettings):
url: str
username: str
password: str
class Client(Settings, BaseModel):
def __init__(self, url=None, username=None, password=None):
super().__init__(url=url, username=username, password=password)
self.client = httpx.Client(base_url=self.url, auth=(self.username, self.password))
client = Client(url='google.com', username='Hello')
"""
(<class 'pydantic_core._pydantic_core.ValidationError'>, 1 validation error for Client
password
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
For further information visit https://errors.pydantic.dev/2.6/v/string_type, <traceback object at 0x00000226F8500D80>)
""" |
You can have your desired behavior like: from typing import Any, Tuple, Type
from pydantic_settings import BaseSettings, InitSettingsSource, PydanticBaseSettingsSource, SettingsConfigDict
class MyInitSettingsSource(InitSettingsSource):
def __init__(self, settings_cls: type[BaseSettings], init_kwargs: dict[str, Any]):
init_kwargs = {k: v for k, v in init_kwargs.items() if v is not None}
super().__init__(settings_cls, init_kwargs)
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="allow")
username: str
@classmethod
def settings_customise_sources(
cls,
settings_cls: Type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:
return MyInitSettingsSource(settings_cls, init_settings.init_kwargs), env_settings, dotenv_settings, file_secret_settings
settings = Settings(username=None) |
I would like to keep the default order of
init_settings
and thenenv_settings
but if I pass None in the init then it will error.Example:
Implementation:
The text was updated successfully, but these errors were encountered: