Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/assets/print_modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys

import typer

app = typer.Typer()


@app.command()
def main():
for m in sys.modules:
print(m)


if __name__ == "__main__":
app()
21 changes: 21 additions & 0 deletions tests/test_rich_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import subprocess
import sys
from pathlib import Path

ACCEPTED_MODULES = {"rich._extension", "rich"}


def test_rich_not_imported_unnecessary():
file_path = Path(__file__).parent / "assets/print_modules.py"
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", str(file_path)],
capture_output=True,
encoding="utf-8",
)
modules = result.stdout.splitlines()
modules = [
module
for module in modules
if module not in ACCEPTED_MODULES and module.startswith("rich")
]
assert not modules
3 changes: 2 additions & 1 deletion typer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import rich

has_rich = True
from . import rich_utils

except ImportError: # pragma: no cover
has_rich = False
Expand Down Expand Up @@ -275,6 +274,8 @@ def get_docs_for_click(
def _parse_html(input_text: str) -> str:
if not has_rich: # pragma: no cover
return input_text
from . import rich_utils

return rich_utils.rich_to_html(input_text)


Expand Down
10 changes: 8 additions & 2 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
try:
import rich

from . import rich_utils

DEFAULT_MARKUP_MODE: MarkupMode = "rich"

except ImportError: # pragma: no cover
Expand Down Expand Up @@ -213,6 +211,8 @@ def _main(
raise
# Typer override
if rich and rich_markup_mode is not None:
from . import rich_utils

rich_utils.rich_format_error(e)
else:
e.show()
Expand Down Expand Up @@ -243,6 +243,8 @@ def _main(
raise
# Typer override
if rich and rich_markup_mode is not None:
from . import rich_utils

rich_utils.rich_abort_error()
else:
click.echo(_("Aborted!"), file=sys.stderr)
Expand Down Expand Up @@ -688,6 +690,8 @@ def main(
def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
if not rich or self.rich_markup_mode is None:
return super().format_help(ctx, formatter)
from . import rich_utils

return rich_utils.rich_format_help(
obj=self,
ctx=ctx,
Expand Down Expand Up @@ -751,6 +755,8 @@ def main(
def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
if not rich or self.rich_markup_mode is None:
return super().format_help(ctx, formatter)
from . import rich_utils

return rich_utils.rich_format_help(
obj=self,
ctx=ctx,
Expand Down
12 changes: 5 additions & 7 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@

try:
import rich
from rich.traceback import Traceback

from . import rich_utils

console_stderr = rich_utils._get_rich_console(stderr=True)

except ImportError: # pragma: no cover
rich = None # type: ignore
Expand Down Expand Up @@ -82,16 +77,19 @@ def except_hook(
supress_internal_dir_names = [typer_path, click_path]
exc = exc_value
if rich:
from .rich_utils import MAX_WIDTH
from rich.traceback import Traceback

from . import rich_utils

rich_tb = Traceback.from_exception(
type(exc),
exc,
exc.__traceback__,
show_locals=exception_config.pretty_exceptions_show_locals,
suppress=supress_internal_dir_names,
width=MAX_WIDTH,
width=rich_utils.MAX_WIDTH,
)
console_stderr = rich_utils._get_rich_console(stderr=True)
console_stderr.print(rich_tb)
return
tb_exc = traceback.TracebackException.from_exception(exc)
Expand Down
Loading