33from __future__ import annotations
44
55from contextlib import contextmanager
6- from dataclasses import dataclass
6+ from dataclasses import dataclass , fields , replace
77from pathlib import Path
88from typing import TYPE_CHECKING , Literal
99
@@ -58,8 +58,12 @@ class UsethisConfig:
5858 subprocess_verbose : bool = False
5959 project_dir : Path | None = None
6060
61+ def copy (self ) -> UsethisConfig :
62+ """Return a shallow copy of this configuration."""
63+ return replace (self )
64+
6165 @contextmanager
62- def set ( # noqa: PLR0913, PLR0915
66+ def set ( # noqa: PLR0913
6367 self ,
6468 * ,
6569 offline : bool | None = None ,
@@ -74,24 +78,14 @@ def set( # noqa: PLR0913, PLR0915
7478 project_dir : Path | str | None = None ,
7579 ) -> Generator [None , None , None ]:
7680 """Temporarily change command options."""
77- old_offline = self .offline
78- old_quiet = self .quiet
79- old_frozen = self .frozen
80- old_alert_only = self .alert_only
81- old_instruct_only = self .instruct_only
82- old_backend = self .backend
83- old_inferred_backend = self .inferred_backend
84- old_build_backend = self .build_backend
85- old_disable_pre_commit = self .disable_pre_commit
86- old_subprocess_verbose = self .subprocess_verbose
87- old_project_dir = self .project_dir
81+ old = self .copy ()
8882
8983 if offline is None :
90- offline = old_offline
84+ offline = self . offline
9185 if quiet is None :
92- quiet = old_quiet
86+ quiet = self . quiet
9387 if frozen is None :
94- frozen = old_frozen
88+ frozen = self . frozen
9589 if alert_only is None :
9690 alert_only = self .alert_only
9791 if instruct_only is None :
@@ -101,11 +95,11 @@ def set( # noqa: PLR0913, PLR0915
10195 if build_backend is None :
10296 build_backend = self .build_backend
10397 if disable_pre_commit is None :
104- disable_pre_commit = old_disable_pre_commit
98+ disable_pre_commit = self . disable_pre_commit
10599 if subprocess_verbose is None :
106- subprocess_verbose = old_subprocess_verbose
100+ subprocess_verbose = self . subprocess_verbose
107101 if project_dir is None :
108- project_dir = old_project_dir
102+ project_dir = self . project_dir
109103
110104 self .offline = offline
111105 self .quiet = quiet
@@ -122,17 +116,12 @@ def set( # noqa: PLR0913, PLR0915
122116 project_dir = Path (project_dir )
123117 self .project_dir = project_dir
124118 yield
125- self .offline = old_offline
126- self .quiet = old_quiet
127- self .frozen = old_frozen
128- self .alert_only = old_alert_only
129- self .instruct_only = old_instruct_only
130- self .backend = old_backend
131- self .inferred_backend = old_inferred_backend
132- self .build_backend = old_build_backend
133- self .disable_pre_commit = old_disable_pre_commit
134- self .subprocess_verbose = old_subprocess_verbose
135- self .project_dir = old_project_dir
119+ self ._restore (old )
120+
121+ def _restore (self , other : UsethisConfig ) -> None :
122+ """Restore all attributes from another configuration instance."""
123+ for f in fields (self ):
124+ setattr (self , f .name , getattr (other , f .name ))
136125
137126 def cpd (self ) -> Path :
138127 """Return the current project directory."""
0 commit comments