From c2d44a7723a00f75ba6790cc654aaa507a7ab123 Mon Sep 17 00:00:00 2001 From: nrolin <62667546+nrolin@users.noreply.github.com> Date: Tue, 18 Jun 2024 09:29:42 +0200 Subject: [PATCH] add in-place reloading in docs (#316) --- docs/index.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/index.md b/docs/index.md index 49759430..11864d39 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1514,3 +1514,39 @@ except ValidationError as exc_info: For further information visit https://errors.pydantic.dev/2/v/missing """ ``` + + +## In-place reloading + +In case you want to reload in-place an existing setting, you can do it by using its `__init__` method : + +```py +import os + +from pydantic import Field + +from pydantic_settings import BaseSettings + + +class Settings(BaseSettings): + foo: str = Field('foo') + + +mutable_settings = Settings() + +print(mutable_settings.foo) +#> foo + +os.environ['foo'] = 'bar' +print(mutable_settings.foo) +#> foo + +mutable_settings.__init__() +print(mutable_settings.foo) +#> bar + +os.environ.pop('foo') +mutable_settings.__init__() +print(mutable_settings.foo) +#> foo +```