Skip to content

Commit b1f5daf

Browse files
committed
Simplify output manager state management
- Initialize _current_output_manager to DisabledOutputManager() instead of None - Use OutputManager protocol as the type for _current_output_manager - _disable_output_manager() now resets to a new DisabledOutputManager - Remove the singleton _DISABLED_OUTPUT_MANAGER from _output.py - _is_output_enabled() now checks isinstance instead of None check This ensures there's always an output manager available, simplifying the invariant.
1 parent 2fe821e commit b1f5daf

2 files changed

Lines changed: 8 additions & 23 deletions

File tree

modal/_output.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,3 @@ def step_completed(message: str) -> str:
216216
@staticmethod
217217
def substep_completed(message: str) -> str:
218218
return message
219-
220-
221-
# Singleton instance of the disabled output manager
222-
_DISABLED_OUTPUT_MANAGER = DisabledOutputManager()

modal/output.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@
88

99
import contextlib
1010
from collections.abc import Generator
11-
from typing import TYPE_CHECKING
12-
13-
if TYPE_CHECKING:
14-
from ._output import OutputManager
1511

12+
from ._output import DisabledOutputManager, OutputManager
1613

1714
# Module-level state for output management
18-
_current_output_manager: "OutputManager | None" = None
15+
_current_output_manager: OutputManager = DisabledOutputManager()
1916

2017

2118
@contextlib.contextmanager
22-
def enable_output(
23-
show_progress: bool = True, show_timestamps: bool = False
24-
) -> Generator["OutputManager | None", None, None]:
19+
def enable_output(show_progress: bool = True, show_timestamps: bool = False) -> Generator[OutputManager, None, None]:
2520
"""Context manager that enable output when using the Python SDK.
2621
2722
This will print to stdout and stderr things such as
@@ -46,10 +41,10 @@ def enable_output(
4641
try:
4742
yield _current_output_manager
4843
finally:
49-
_current_output_manager = None
44+
_current_output_manager = DisabledOutputManager()
5045

5146

52-
def _get_output_manager() -> "OutputManager":
47+
def _get_output_manager() -> OutputManager:
5348
"""Get the current output manager.
5449
5550
Returns a RichOutputManager when output is enabled, otherwise returns
@@ -58,13 +53,7 @@ def _get_output_manager() -> "OutputManager":
5853
This allows code to call output methods without checking if output is enabled,
5954
simplifying the calling code.
6055
"""
61-
if _current_output_manager is not None:
62-
return _current_output_manager
63-
64-
# Return the singleton disabled output manager
65-
from ._output import _DISABLED_OUTPUT_MANAGER
66-
67-
return _DISABLED_OUTPUT_MANAGER
56+
return _current_output_manager
6857

6958

7059
def _is_output_enabled() -> bool:
@@ -73,7 +62,7 @@ def _is_output_enabled() -> bool:
7362
This is useful for code that needs to conditionally perform operations
7463
based on whether output is truly enabled (e.g., starting a logs loop).
7564
"""
76-
return _current_output_manager is not None
65+
return not isinstance(_current_output_manager, DisabledOutputManager)
7766

7867

7968
def _disable_output_manager() -> None:
@@ -83,4 +72,4 @@ def _disable_output_manager() -> None:
8372
to _get_output_manager() return a DisabledOutputManager.
8473
"""
8574
global _current_output_manager
86-
_current_output_manager = None
75+
_current_output_manager = DisabledOutputManager()

0 commit comments

Comments
 (0)