⚡️ Lazy-load `rich_utils` to reduce startup time (#1128)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Martina Oefelein · committed by GitHub · parent ad6d4cd · Browse files · 5 files·+51 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+51 added−10 removed
- tests:+36 added−0 removed
- other:+15 added−10 removed
Provenance
- Commit
f398fb1- Tree
d11fa04- Author
Martina Oefelein <oefe@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ad6d4cd
5 files changed+51 added−10 removed
added:tests/assets/print_modules.py+15 added
1+import sys2+3+import typer4+5+app = typer.Typer()6+7+8+@app.command()9+def main():10+ for m in sys.modules:11+ print(m)12+13+14+if __name__ == "__main__":15+ app()added:tests/test_rich_import.py+21 added
1+import subprocess2+import sys3+from pathlib import Path4+5+ACCEPTED_MODULES = {"rich._extension", "rich"}6+7+8+def test_rich_not_imported_unnecessary():9+ file_path = Path(__file__).parent / "assets/print_modules.py"10+ result = subprocess.run(11+ [sys.executable, "-m", "coverage", "run", str(file_path)],12+ capture_output=True,13+ encoding="utf-8",14+ )15+ modules = result.stdout.splitlines()16+ modules = [17+ module18+ for module in modules19+ if module not in ACCEPTED_MODULES and module.startswith("rich")20+ ]21+ assert not modulesmodified:typer/cli.py+2 added-1 removed
13 unmodified lines14 try:15 import rich16 17 has_rich = True18- from . import rich_utils18 19 except ImportError: # pragma: no cover20 has_rich = False21 rich = None # type: ignore251 unmodified lines273 274 def _parse_html(input_text: str) -> str:275 if not has_rich: # pragma: no cover276 return input_text277+ from . import rich_utils278+279 return rich_utils.rich_to_html(input_text)280 281 282 @utils_app.command()34 unmodified linesmodified:typer/core.py+8 added-2 removed
31 unmodified lines32 33 try:34 import rich35 36- from . import rich_utils37-36 DEFAULT_MARKUP_MODE: MarkupMode = "rich"37 38 except ImportError: # pragma: no cover39 rich = None # type: ignore170 unmodified lines210 if not standalone_mode:211 raise212 # Typer override213 if rich and rich_markup_mode is not None:214+ from . import rich_utils215+216 rich_utils.rich_format_error(e)217 else:218 e.show()219 # Typer override end22 unmodified lines242 if not standalone_mode:243 raise244 # Typer override245 if rich and rich_markup_mode is not None:246+ from . import rich_utils247+248 rich_utils.rich_abort_error()249 else:250 click.echo(_("Aborted!"), file=sys.stderr)251 # Typer override end454 unmodified lines706 707 def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:708 if not rich or self.rich_markup_mode is None:709 return super().format_help(ctx, formatter)710+ from . import rich_utils711+712 return rich_utils.rich_format_help(713 obj=self,714 ctx=ctx,715 markup_mode=self.rich_markup_mode,55 unmodified lines771 772 def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:773 if not rich or self.rich_markup_mode is None:774 return super().format_help(ctx, formatter)775+ from . import rich_utils776+777 return rich_utils.rich_format_help(778 obj=self,779 ctx=ctx,780 markup_mode=self.rich_markup_mode,7 unmodified linesmodified:typer/main.py+5 added-7 removed
49 unmodified lines50 from .utils import get_params_from_function51 52 try:53 import rich54- from rich.traceback import Traceback54 56- from . import rich_utils57-58- console_stderr = rich_utils._get_rich_console(stderr=True)59-55 except ImportError: # pragma: no cover56 rich = None # type: ignore57 58 _original_except_hook = sys.excepthook18 unmodified lines77 click_path = os.path.dirname(click.__file__)78 supress_internal_dir_names = [typer_path, click_path]79 exc = exc_value80 if rich:86- from .rich_utils import MAX_WIDTH81+ from rich.traceback import Traceback82 83+ from . import rich_utils84+85 rich_tb = Traceback.from_exception(86 type(exc),87 exc,88 exc.__traceback__,89 show_locals=exception_config.pretty_exceptions_show_locals,90 suppress=supress_internal_dir_names,94- width=MAX_WIDTH,91+ width=rich_utils.MAX_WIDTH,92 )93+ console_stderr = rich_utils._get_rich_console(stderr=True)94 console_stderr.print(rich_tb)95 return96 tb_exc = traceback.TracebackException.from_exception(exc)97 stack: List[FrameSummary] = []1046 unmodified lines