5
5
6
6
import inspect
7
7
import os
8
+ import sys
8
9
import typing as t
9
- from types import ModuleType
10
10
11
11
from django .apps import AppConfig
12
12
from django .conf import settings
13
13
from django .core .checks import CheckMessage , register
14
14
from django .core .checks import Warning as CheckWarning
15
15
from django .utils .translation import gettext as _
16
16
17
- from django_typer import patch
18
- from django_typer .utils import traceback_config
17
+ from .config import traceback_config
19
18
20
- patch .apply ()
19
+ tb_config = traceback_config ()
20
+ try_install = isinstance (tb_config , dict ) and not tb_config .get ("no_install" , False )
21
21
22
- rich : t .Optional [ModuleType ]
23
- traceback : t .Optional [ModuleType ]
22
+ if try_install :
23
+ try :
24
+ import rich
25
+ from rich import traceback
24
26
25
- try :
26
- import sys
27
+ from django_typer import patch
27
28
28
- import rich
29
- from rich import traceback
30
- from typer import main as typer_main
29
+ patch . apply ()
30
+
31
+ from typer import main as typer_main
31
32
32
- tb_config = traceback_config ()
33
- if (
34
- rich
35
- and traceback
36
- and isinstance (tb_config , dict )
37
- and not tb_config .get ("no_install" , False )
38
- ):
39
33
# install rich tracebacks if we've been configured to do so (default)
34
+ assert isinstance (tb_config , dict )
40
35
no_color = "NO_COLOR" in os .environ
41
36
force_color = "FORCE_COLOR" in os .environ
42
37
traceback .install (
64
59
# on when typer was imported it may have the original fallback system hook or our
65
60
# installed rich one - we patch it here to make sure!
66
61
typer_main ._original_except_hook = sys .excepthook
67
- except ImportError :
68
- rich = None
69
- traceback = None
62
+
63
+ except ImportError :
64
+ pass
70
65
71
66
72
67
@register ("settings" )
@@ -76,15 +71,17 @@ def check_traceback_config(app_configs, **kwargs) -> t.List[CheckMessage]:
76
71
contains only the expected parameters.
77
72
"""
78
73
warnings : t .List [CheckMessage ] = []
79
- tb_cfg = traceback_config ()
80
- if isinstance (tb_cfg , dict ):
81
- if rich and traceback :
74
+ if try_install :
75
+ assert isinstance (tb_config , dict )
76
+ try :
77
+ from rich import traceback
78
+
82
79
expected = {
83
80
"no_install" ,
84
81
"short" ,
85
82
* inspect .signature (traceback .install ).parameters .keys (),
86
83
}
87
- unexpected = set (tb_cfg .keys ()) - expected
84
+ unexpected = set (tb_config .keys ()) - expected
88
85
if unexpected :
89
86
warnings .append (
90
87
CheckWarning (
@@ -96,6 +93,8 @@ def check_traceback_config(app_configs, **kwargs) -> t.List[CheckMessage]:
96
93
id = "django_typer.W001" ,
97
94
)
98
95
)
96
+ except ImportError :
97
+ pass
99
98
return warnings
100
99
101
100
0 commit comments