Skip to content

Commit f398fb1

Browse files
authored
⚡️ Lazy-load rich_utils to reduce startup time (#1128)
1 parent ad6d4cd commit f398fb1

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

tests/assets/print_modules.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
3+
import typer
4+
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()

tests/test_rich_import.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import subprocess
2+
import sys
3+
from pathlib import Path
4+
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+
module
18+
for module in modules
19+
if module not in ACCEPTED_MODULES and module.startswith("rich")
20+
]
21+
assert not modules

typer/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import rich
1616

1717
has_rich = True
18-
from . import rich_utils
1918

2019
except ImportError: # pragma: no cover
2120
has_rich = False
@@ -275,6 +274,8 @@ def get_docs_for_click(
275274
def _parse_html(input_text: str) -> str:
276275
if not has_rich: # pragma: no cover
277276
return input_text
277+
from . import rich_utils
278+
278279
return rich_utils.rich_to_html(input_text)
279280

280281

typer/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
try:
3434
import rich
3535

36-
from . import rich_utils
37-
3836
DEFAULT_MARKUP_MODE: MarkupMode = "rich"
3937

4038
except ImportError: # pragma: no cover
@@ -213,6 +211,8 @@ def _main(
213211
raise
214212
# Typer override
215213
if rich and rich_markup_mode is not None:
214+
from . import rich_utils
215+
216216
rich_utils.rich_format_error(e)
217217
else:
218218
e.show()
@@ -243,6 +243,8 @@ def _main(
243243
raise
244244
# Typer override
245245
if rich and rich_markup_mode is not None:
246+
from . import rich_utils
247+
246248
rich_utils.rich_abort_error()
247249
else:
248250
click.echo(_("Aborted!"), file=sys.stderr)
@@ -705,6 +707,8 @@ def main(
705707
def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
706708
if not rich or self.rich_markup_mode is None:
707709
return super().format_help(ctx, formatter)
710+
from . import rich_utils
711+
708712
return rich_utils.rich_format_help(
709713
obj=self,
710714
ctx=ctx,
@@ -768,6 +772,8 @@ def main(
768772
def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
769773
if not rich or self.rich_markup_mode is None:
770774
return super().format_help(ctx, formatter)
775+
from . import rich_utils
776+
771777
return rich_utils.rich_format_help(
772778
obj=self,
773779
ctx=ctx,

typer/main.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@
5151

5252
try:
5353
import rich
54-
from rich.traceback import Traceback
55-
56-
from . import rich_utils
57-
58-
console_stderr = rich_utils._get_rich_console(stderr=True)
5954

6055
except ImportError: # pragma: no cover
6156
rich = None # type: ignore
@@ -83,16 +78,19 @@ def except_hook(
8378
supress_internal_dir_names = [typer_path, click_path]
8479
exc = exc_value
8580
if rich:
86-
from .rich_utils import MAX_WIDTH
81+
from rich.traceback import Traceback
82+
83+
from . import rich_utils
8784

8885
rich_tb = Traceback.from_exception(
8986
type(exc),
9087
exc,
9188
exc.__traceback__,
9289
show_locals=exception_config.pretty_exceptions_show_locals,
9390
suppress=supress_internal_dir_names,
94-
width=MAX_WIDTH,
91+
width=rich_utils.MAX_WIDTH,
9592
)
93+
console_stderr = rich_utils._get_rich_console(stderr=True)
9694
console_stderr.print(rich_tb)
9795
return
9896
tb_exc = traceback.TracebackException.from_exception(exc)

0 commit comments

Comments
 (0)