88
99import contextlib
1010from collections .abc import Generator
11- from typing import TYPE_CHECKING , Union
12-
13- if TYPE_CHECKING :
14- from ._output import DisabledOutputManager
15- from ._rich_output import RichOutputManager
1611
12+ from ._output import _DISABLED_OUTPUT_MANAGER , OutputManager
1713
1814# Module-level state for output management
19- _current_output_manager : "Union[RichOutputManager, None]" = None
15+ _current_output_manager : OutputManager = _DISABLED_OUTPUT_MANAGER
2016
2117
2218@contextlib .contextmanager
23- def enable_output (
24- show_progress : bool = True , show_timestamps : bool = False
25- ) -> Generator ["RichOutputManager | None" , None , None ]:
19+ def enable_output (show_progress : bool = True , show_timestamps : bool = False ) -> Generator [OutputManager , None , None ]:
2620 """Context manager that enable output when using the Python SDK.
2721
2822 This will print to stdout and stderr things such as
@@ -39,6 +33,7 @@ def enable_output(
3933 ```
4034 """
4135 global _current_output_manager
36+ previous_output_manager = _current_output_manager
4237
4338 if show_progress :
4439 from ._rich_output import RichOutputManager
@@ -47,10 +42,10 @@ def enable_output(
4742 try :
4843 yield _current_output_manager
4944 finally :
50- _current_output_manager = None
45+ _current_output_manager = previous_output_manager
5146
5247
53- def _get_output_manager () -> "Union[RichOutputManager, DisabledOutputManager]" :
48+ def _get_output_manager () -> OutputManager :
5449 """Get the current output manager.
5550
5651 Returns a RichOutputManager when output is enabled, otherwise returns
@@ -59,22 +54,7 @@ def _get_output_manager() -> "Union[RichOutputManager, DisabledOutputManager]":
5954 This allows code to call output methods without checking if output is enabled,
6055 simplifying the calling code.
6156 """
62- if _current_output_manager is not None :
63- return _current_output_manager
64-
65- # Return the singleton disabled output manager
66- from ._output import _DISABLED_OUTPUT_MANAGER
67-
68- return _DISABLED_OUTPUT_MANAGER
69-
70-
71- def _is_output_enabled () -> bool :
72- """Check if rich output is enabled.
73-
74- This is useful for code that needs to conditionally perform operations
75- based on whether output is truly enabled (e.g., starting a logs loop).
76- """
77- return _current_output_manager is not None
57+ return _current_output_manager
7858
7959
8060def _disable_output_manager () -> None :
@@ -84,4 +64,4 @@ def _disable_output_manager() -> None:
8464 to _get_output_manager() return a DisabledOutputManager.
8565 """
8666 global _current_output_manager
87- _current_output_manager = None
67+ _current_output_manager = _DISABLED_OUTPUT_MANAGER
0 commit comments