Coverage for /usr/local/lib/python3.12/site-packages/prefect/settings/context.py: 88%
20 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 11:21 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 11:21 +0000
1from contextlib import contextmanager 1a
2from typing import TYPE_CHECKING, Any, Generator, Iterable, Mapping, Optional 1a
4from prefect.settings.models.root import Settings 1a
6if TYPE_CHECKING: 6 ↛ 7line 6 didn't jump to line 7 because the condition on line 6 was never true1a
7 from prefect.settings.legacy import Setting
10def get_current_settings() -> Settings: 1a
11 """
12 Returns a settings object populated with values from the current settings context
13 or, if no settings context is active, the environment.
14 """
15 from prefect.context import SettingsContext 1abcde
17 settings_context = SettingsContext.get() 1abcde
18 if settings_context is not None: 1abcde
19 return settings_context.settings 1abcde
21 return Settings() 1a
24@contextmanager 1a
25def temporary_settings( 1a
26 updates: Optional[Mapping["Setting", Any]] = None,
27 set_defaults: Optional[Mapping["Setting", Any]] = None,
28 restore_defaults: Optional[Iterable["Setting"]] = None,
29) -> Generator[Settings, None, None]:
30 """
31 Temporarily override the current settings by entering a new profile.
33 See `Settings.copy_with_update` for details on different argument behavior.
35 Examples:
37 ```python
38 from prefect.settings import PREFECT_API_URL
40 with temporary_settings(updates={PREFECT_API_URL: "foo"}):
41 assert PREFECT_API_URL.value() == "foo"
43 with temporary_settings(set_defaults={PREFECT_API_URL: "bar"}):
44 assert PREFECT_API_URL.value() == "foo"
46 with temporary_settings(restore_defaults={PREFECT_API_URL}):
47 assert PREFECT_API_URL.value() is None
49 with temporary_settings(set_defaults={PREFECT_API_URL: "bar"})
50 assert PREFECT_API_URL.value() == "bar"
51 assert PREFECT_API_URL.value() is None
52 ```
53 """
54 import prefect.context 1a
56 context = prefect.context.get_settings_context() 1a
58 if not restore_defaults: 58 ↛ 61line 58 didn't jump to line 61 because the condition on line 58 was always true1a
59 restore_defaults = [] 1a
61 new_settings = context.settings.copy_with_update( 1a
62 updates=updates, set_defaults=set_defaults, restore_defaults=restore_defaults
63 )
65 with prefect.context.SettingsContext( 1a
66 profile=context.profile, settings=new_settings
67 ):
68 yield new_settings 1a