Comments (7)
@nrolin I think you can initialize the settings model again my_test = Config()
instead of my_test.__init__()
.
I think adding a method only for testing purposes is not a good idea.
from pydantic-settings.
@nrolin I think you can initialize the settings model again
my_test = Config()
instead ofmy_test.__init__()
.
Sorry my example wasn't clear enough
in my_file.py
from pydantic import Field
from pydantic_settings import BaseSettings
import os
class Config(BaseSettings):
test: bool = Field(default=True)
my_test = Config()
def get_my_test():
return my_test.test
in an ipython :
> from my_file import my_test, os, get_my_test
> get_my_test()
True
> os.environ["test"] = "False"
> get_my_test()
True
> my_test.__init__() # <- it would be better to have a reload method that is guaranteed to have the correct semantics
> get_my_test()
False
I can't do my_test = Config()
because it wouldn't change the result of get_my_test()
I think adding a method only for testing purposes is not a good idea.
Some people are doing really convoluted things to achieve this (e.g : https://rednafi.com/python/patch_pydantic_settings_in_pytest/ ), so it would be nice to have an official guideline on how to solve this issue.
from pydantic-settings.
Some people are doing really convoluted things to achieve this (e.g : https://rednafi.com/python/patch_pydantic_settings_in_pytest/ ), so it would be nice to have an official guideline on how to solve this issue.
I haven't read this carefully but adding some pytest fixture seems good.
BTW, I don't see any problem with using my_test.__init__()
. So, having another reload
method that does the same thing or calls __init__
is extra IMO.
from pydantic-settings.
BTW, I don't see any problem with using my_test.init(). So, having another reload method that does the same thing or calls init is extra IMO.
The problem is that as an user I suppose that __init__
does reload as if I was creating the object, but it kinda a gamble and nothing guarantees that the developer will not silently change the semantics without warning in a future version (which is 100% fair for a dunder method).
Adding it in the doc or a proper method gives a bit more security on this side.
from pydantic-settings.
Ok, would you like to open a PR for this?
from pydantic-settings.
Ok I will try
from pydantic-settings.
Doc added in c2d44a7
from pydantic-settings.
Related Issues (20)
- Create default templates (feature request) HOT 2
- Example from docs doesn't work can't import from pydantic.env_settings import SettingsSourceCallable HOT 1
- Can my custom PydanticBaseSettingsSource get values already retrieved from other PydanticBaseSettingsSources? HOT 2
- Comma delimited string as list of strings HOT 1
- Easily list the names of the env var required to set a field HOT 2
- Environment Variable Overrides Init Arguments in BaseSettings with Aliases HOT 5
- environment variables case sensitivity on windows HOT 3
- Can't read Pydantic Settings from stdin HOT 3
- Python 3.9 (and earlier I guess), and non PEP-484 type definitions with injector HOT 5
- Typing for multiple PathTypes (and support Traversable) for SettingsConfigDict json_file, toml_file, yaml_file, etc HOT 2
- Validation error for 3 levels of nested dicts in v2.3.0 HOT 7
- Field alias AttributeError: 'AliasChoices' object has no attribute 'lower' - CliSettingsSource HOT 3
- CLI help interacts badly with strings containing `'%'` HOT 4
- CliSettingSource fails to handle `AliasChoices` and `AliasPath` HOT 3
- Error when using validator in nested model with case_sensitive=True and using upper case HOT 7
- Settings parsing breaks with complex type HOT 8
- What's the recommended way to access settings loaded from previous sources? HOT 1
- Env does not override nested value when other sources are used
- 2.3.4: `pyupgrade --py39-plus` generated changes causes pytest fails 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.