Skip to content

Commit eead8fc

Browse files
Copilotbittner
andcommitted
Refactor LoggingIsolationMixin to use __enter__/__exit__ transparently via MRO
Co-authored-by: bittner <[email protected]>
1 parent e4ff76e commit eead8fc

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

cli_test_helpers/decorators.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,27 @@ class LoggingIsolationMixin:
2121
allows code under test to call ``logging.basicConfig()`` successfully even
2222
when test frameworks (like pytest) have already configured logging handlers.
2323
24-
Classes using this mixin should call ``_save_logging_handlers()`` at the
25-
start of their ``__enter__()`` method and ``_restore_logging_handlers()``
26-
at the end of their ``__exit__()`` method.
24+
This mixin works transparently through Python's method resolution order
25+
(MRO) and requires no explicit method calls from subclasses.
2726
"""
2827

29-
def _save_logging_handlers(self):
30-
"""Save and clear current logging handlers."""
28+
def __enter__(self):
29+
"""Save and clear logging handlers before entering the context."""
3130
self._old_handlers = logging.root.handlers[:]
3231
logging.root.handlers.clear()
32+
# Call parent __enter__ if it exists (for cooperative inheritance)
33+
if hasattr(super(), '__enter__'):
34+
return super().__enter__()
35+
return self
3336

34-
def _restore_logging_handlers(self):
35-
"""Restore previously saved logging handlers."""
37+
def __exit__(self, exc_type, exc_val, exc_tb):
38+
"""Restore logging handlers after exiting the context."""
3639
if hasattr(self, '_old_handlers'):
3740
logging.root.handlers[:] = self._old_handlers
41+
# Call parent __exit__ if it exists (for cooperative inheritance)
42+
if hasattr(super(), '__exit__'):
43+
return super().__exit__(exc_type, exc_val, exc_tb)
44+
return None
3845

3946

4047
class ArgvContext(LoggingIsolationMixin):
@@ -51,13 +58,13 @@ def __init__(self, *new_args):
5158
self.args = type(self._old)(new_args)
5259

5360
def __enter__(self):
54-
self._save_logging_handlers()
61+
super().__enter__()
5562
sys.argv = self.args
5663
return self
5764

5865
def __exit__(self, exc_type, exc_val, exc_tb):
5966
sys.argv = self._old
60-
self._restore_logging_handlers()
67+
super().__exit__(exc_type, exc_val, exc_tb)
6168

6269

6370
class EnvironContext(LoggingIsolationMixin, patch.dict):
@@ -80,8 +87,6 @@ def __init__(self, **kwargs):
8087
super().__init__("os.environ", **kwargs)
8188

8289
def __enter__(self):
83-
self._save_logging_handlers()
84-
8590
super().__enter__()
8691

8792
for key in self.clear_variables:
@@ -91,7 +96,6 @@ def __enter__(self):
9196
return self
9297

9398
def __exit__(self, exc_type, exc_val, exc_tb):
94-
self._restore_logging_handlers()
9599
return super().__exit__(exc_type, exc_val, exc_tb)
96100

97101

@@ -111,15 +115,13 @@ def __init__(self, *args, **kwargs):
111115

112116
def __enter__(self):
113117
"""Create a temporary directory and ``cd`` into it."""
114-
self._save_logging_handlers()
118+
super().__enter__()
115119

116120
self.__prev_dir = os.getcwd()
117-
super().__enter__()
118121
os.chdir(self.name)
119122
return os.getcwd()
120123

121124
def __exit__(self, exc_type, exc_value, traceback):
122125
"""Return to the original directory before execution."""
123-
self._restore_logging_handlers()
124126
os.chdir(self.__prev_dir)
125127
return super().__exit__(exc_type, exc_value, traceback)

0 commit comments

Comments
 (0)