Comments (2)
Thanks @rusmux for reporting this.
Your example code has some problems:
- You have to use
BaseModel
instead ofBaseSettings
in sub-models https://docs.pydantic.dev/latest/concepts/pydantic_settings/#parsing-environment-variable-values - The
env_prefix
in sub-model does not have any effect, because pydantic-settings only considers the config of root model - Better to use
env_nested_delimiter
config for sub models
So, at the end you can collect values like you want by changing your code to:
from pydantic import Field, BaseModel
from pydantic_settings import (
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
YamlConfigSettingsSource,
)
class PydanticBaseSettings(BaseSettings):
@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 (init_settings, env_settings, dotenv_settings,
YamlConfigSettingsSource(settings_cls), file_secret_settings)
class SubSettings(BaseModel):
value: str
class Settings(PydanticBaseSettings):
model_config = SettingsConfigDict(yaml_file="config.yaml", env_nested_delimiter="__")
value: str
sub: SubSettings
print(Settings())
You need to call your script by value=b sub__value=c python ...
from pydantic-settings.
I see, thank you!
from pydantic-settings.
Related Issues (20)
- Field validators broken for nested models since v2.3.2 HOT 7
- Unable to Override Environment Variable in Pydantic Settings HOT 2
- Unable to have file secret take precedence even with env_prefix present. HOT 2
- Question about the parameter inference for the constructor in the class inherited from BaseSettings HOT 2
- No arguments to CLI subparser fails to raise an error HOT 11
- Remove SystemExit usage HOT 10
- Feature Request: Add short args to CLI HOT 1
- RootModel[str] cannot be parsed/requires double quotation. HOT 7
- Nested Settings with instances of models as defaults HOT 1
- More ``__init__`` Overwrite Support HOT 2
- Environment Parsing with Default Nested Class HOT 15
- mypy v1.11.0 crashing with `--pretty` and shows errors for union syntax HOT 6
- `AliasChoices` order overrides `settings_customise_sources` priority HOT 1
- Recommendations for background updates HOT 2
- Nested env variables treated as strings when env_file and env_prefix are set but variables aren't prefixed HOT 3
- In pydantic_settings don't use values from ".env" when env_file explictly points to a different file HOT 1
- Nested dataclass settings class gives `AttributeError: __pydantic_fields__` HOT 9
- Feature request: better support for argumentless CLI boolean flags HOT 4
- help message of sub model in basesetting do not show the proper default value if the top field has a default HOT 2
- partial specify the sub model value does not work HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydantic-settings.