Skip to content
Myr

⚡️ 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

5 files+51 added−10 removed

added:tests/assets/print_modules.py+15 added
1import sys23import typer45app = typer.Typer()678@app.command()9def main():10 for m in sys.modules:11 print(m)121314if __name__ == "__main__":15 app()
added:tests/test_rich_import.py+21 added
1import subprocess2import sys3from pathlib import Path45ACCEPTED_MODULES = {"rich._extension", "rich"}678def 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 modules
modified:typer/cli.py+2 added-1 removed
13 unmodified lines
14try:15 import rich1617 has_rich = True18 from . import rich_utils1819except ImportError: # pragma: no cover20 has_rich = False21 rich = None # type: ignore
251 unmodified lines
273274def _parse_html(input_text: str) -> str:275 if not has_rich: # pragma: no cover276 return input_text277 from . import rich_utils278279 return rich_utils.rich_to_html(input_text)280281282@utils_app.command()
34 unmodified lines
modified:typer/core.py+8 added-2 removed
31 unmodified lines
3233try:34 import rich3536 from . import rich_utils3736 DEFAULT_MARKUP_MODE: MarkupMode = "rich"3738except ImportError: # pragma: no cover39 rich = None # type: ignore
170 unmodified lines
210 if not standalone_mode:211 raise212 # Typer override213 if rich and rich_markup_mode is not None:214 from . import rich_utils215216 rich_utils.rich_format_error(e)217 else:218 e.show()219 # Typer override end
22 unmodified lines
242 if not standalone_mode:243 raise244 # Typer override245 if rich and rich_markup_mode is not None:246 from . import rich_utils247248 rich_utils.rich_abort_error()249 else:250 click.echo(_("Aborted!"), file=sys.stderr)251 # Typer override end
454 unmodified lines
706707 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_utils711712 return rich_utils.rich_format_help(713 obj=self,714 ctx=ctx,715 markup_mode=self.rich_markup_mode,
55 unmodified lines
771772 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_utils776777 return rich_utils.rich_format_help(778 obj=self,779 ctx=ctx,780 markup_mode=self.rich_markup_mode,
7 unmodified lines
modified:typer/main.py+5 added-7 removed
49 unmodified lines
50from .utils import get_params_from_function5152try:53 import rich54 from rich.traceback import Traceback5456 from . import rich_utils5758 console_stderr = rich_utils._get_rich_console(stderr=True)5955except ImportError: # pragma: no cover56 rich = None # type: ignore5758_original_except_hook = sys.excepthook
18 unmodified lines
77 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 Traceback8283 from . import rich_utils8485 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