Skip to content

Commit a00f884

Browse files
committed
fix #88, add import deprecation warning
1 parent 1e61f9b commit a00f884

File tree

9 files changed

+95
-58
lines changed

9 files changed

+95
-58
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ Please refer to the [full documentation](https://django-typer.readthedocs.io/) f
3131

3232
![django-typer example](https://raw.githubusercontent.com/bckohan/django-typer/main/doc/source/_static/img/closepoll_example.gif)
3333

34+
**Imports from ``django_typer`` **have been deprecated and will be removed in 3.0! Imports have moved to ``django_typer.management``:**
35+
36+
```python
37+
# old way
38+
from django_typer import TyperCommand, command, group, initialize, Typer
39+
40+
# new way!
41+
from django_typer.management import TyperCommand, command, group, initialize, Typer
42+
```
43+
3444
## Installation
3545

3646
1. Clone django-typer from GitHub or install a release off [PyPI](https://pypi.org/project/django-typer/):

django_typer/__init__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@
3131

3232
# WARNING - these imports are going away in version 3!
3333
# import them from django_typer.management directly
34-
from .management import (
35-
CommandNode, # noqa: F401
36-
Context, # noqa: F401
37-
DjangoTyperMixin, # noqa: F401
38-
DTCommand, # noqa: F401
39-
DTGroup, # noqa: F401
40-
Typer, # noqa: F401
41-
TyperCommand, # noqa: F401
42-
callback, # noqa: F401
43-
command, # noqa: F401
44-
get_command, # noqa: F401
45-
group, # noqa: F401
46-
initialize, # noqa: F401
47-
model_parser_completer, # noqa: F401
48-
)
34+
# from .management import (
35+
# CommandNode, # noqa: F401
36+
# Context, # noqa: F401
37+
# DjangoTyperMixin, # noqa: F401
38+
# DTCommand, # noqa: F401
39+
# DTGroup, # noqa: F401
40+
# Typer, # noqa: F401
41+
# TyperCommand, # noqa: F401
42+
# callback, # noqa: F401
43+
# command, # noqa: F401
44+
# get_command, # noqa: F401
45+
# group, # noqa: F401
46+
# initialize, # noqa: F401
47+
# model_parser_completer, # noqa: F401
48+
# )
4949

5050
VERSION = (2, 1, 0)
5151

django_typer/apps.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,33 @@
55

66
import inspect
77
import os
8+
import sys
89
import typing as t
9-
from types import ModuleType
1010

1111
from django.apps import AppConfig
1212
from django.conf import settings
1313
from django.core.checks import CheckMessage, register
1414
from django.core.checks import Warning as CheckWarning
1515
from django.utils.translation import gettext as _
1616

17-
from django_typer import patch
18-
from django_typer.utils import traceback_config
17+
from .config import traceback_config
1918

20-
patch.apply()
19+
tb_config = traceback_config()
20+
try_install = isinstance(tb_config, dict) and not tb_config.get("no_install", False)
2121

22-
rich: t.Optional[ModuleType]
23-
traceback: t.Optional[ModuleType]
22+
if try_install:
23+
try:
24+
import rich
25+
from rich import traceback
2426

25-
try:
26-
import sys
27+
from django_typer import patch
2728

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
3132

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-
):
3933
# install rich tracebacks if we've been configured to do so (default)
34+
assert isinstance(tb_config, dict)
4035
no_color = "NO_COLOR" in os.environ
4136
force_color = "FORCE_COLOR" in os.environ
4237
traceback.install(
@@ -64,9 +59,9 @@
6459
# on when typer was imported it may have the original fallback system hook or our
6560
# installed rich one - we patch it here to make sure!
6661
typer_main._original_except_hook = sys.excepthook
67-
except ImportError:
68-
rich = None
69-
traceback = None
62+
63+
except ImportError:
64+
pass
7065

7166

7267
@register("settings")
@@ -76,15 +71,17 @@ def check_traceback_config(app_configs, **kwargs) -> t.List[CheckMessage]:
7671
contains only the expected parameters.
7772
"""
7873
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+
8279
expected = {
8380
"no_install",
8481
"short",
8582
*inspect.signature(traceback.install).parameters.keys(),
8683
}
87-
unexpected = set(tb_cfg.keys()) - expected
84+
unexpected = set(tb_config.keys()) - expected
8885
if unexpected:
8986
warnings.append(
9087
CheckWarning(
@@ -96,6 +93,8 @@ def check_traceback_config(app_configs, **kwargs) -> t.List[CheckMessage]:
9693
id="django_typer.W001",
9794
)
9895
)
96+
except ImportError:
97+
pass
9998
return warnings
10099

101100

django_typer/config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import typing as t
2+
3+
from django.conf import settings
4+
5+
6+
def traceback_config() -> t.Union[bool, t.Dict[str, t.Any]]:
7+
"""
8+
Fetch the rich traceback installation parameters from our settings. By default
9+
rich tracebacks are on with show_locals = True. If the config is set to False
10+
or None rich tracebacks will not be installed even if the library is present.
11+
12+
This allows us to have a common traceback configuration for all commands. If rich
13+
tracebacks are managed separately this setting can also be switched off.
14+
"""
15+
cfg = getattr(settings, "DT_RICH_TRACEBACK_CONFIG", {"show_locals": True})
16+
if cfg:
17+
return {"show_locals": True, **cfg}
18+
return bool(cfg)

django_typer/management/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from typer.models import Default, DefaultPlaceholder # noqa: E402
3333

3434
from ..completers import ModelObjectCompleter # noqa: E402
35+
from ..config import traceback_config # noqa: E402
3536
from ..parsers import ModelObjectParser # noqa: E402
3637
from ..types import ( # noqa: E402
3738
ForceColor,
@@ -50,7 +51,6 @@
5051
called_from_module,
5152
get_usage_script,
5253
is_method,
53-
traceback_config,
5454
with_typehint,
5555
)
5656

django_typer/utils.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from threading import local
1515
from types import MethodType, ModuleType
1616

17-
from django.conf import settings
17+
from .config import traceback_config
1818

1919
# DO NOT IMPORT ANYTHING FROM TYPER HERE - SEE patch.py
2020

@@ -46,21 +46,6 @@ def get_usage_script(script: t.Optional[str] = None) -> t.Union[Path, str]:
4646
return cmd_pth.absolute()
4747

4848

49-
def traceback_config() -> t.Union[bool, t.Dict[str, t.Any]]:
50-
"""
51-
Fetch the rich traceback installation parameters from our settings. By default
52-
rich tracebacks are on with show_locals = True. If the config is set to False
53-
or None rich tracebacks will not be installed even if the library is present.
54-
55-
This allows us to have a common traceback configuration for all commands. If rich
56-
tracebacks are managed separately this setting can also be switched off.
57-
"""
58-
cfg = getattr(settings, "DT_RICH_TRACEBACK_CONFIG", {"show_locals": True})
59-
if cfg:
60-
return {"show_locals": True, **cfg}
61-
return bool(cfg)
62-
63-
6449
_command_context = local()
6550

6651

doc/source/changelog.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ Change Log
55
v2.1.0
66
======
77

8+
.. warning::
9+
10+
**Imports from** ``django_typer`` **have been deprecated and will be removed in 3.0!** Imports
11+
have moved to ``django_typer.management``:
12+
13+
.. code-block::
14+
15+
# old way
16+
from django_typer import TyperCommand, command, group, initialize, Typer
17+
18+
# new way!
19+
from django_typer.management import TyperCommand, command, group, initialize, Typer
20+
21+
* Implemented `Only attempt to import rich and typer if settings has not disabled tracebacks. <https://github.com/bckohan/django-typer/issues/88>`_
822
* Implemented `Move tests into top level dir. <https://github.com/bckohan/django-typer/issues/87>`_
923
* Implemented `Move core code out of __init__.py into management/__init__.py <https://github.com/bckohan/django-typer/issues/81>`_
1024
* Fixed `Typer(help="") doesnt work. <https://github.com/bckohan/django-typer/issues/78>`_

doc/source/index.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ familiar with. All of the BaseCommand_ functionality is preserved, so that
2828
commands.
2929
* Add plugins to upstream commands.
3030

31+
.. warning::
32+
33+
**Imports from** ``django_typer`` **have been deprecated and will be removed in 3.0!** Imports
34+
have moved to ``django_typer.management``:
35+
36+
.. code-block::
37+
38+
# old way
39+
from django_typer import TyperCommand, command, group, initialize, Typer
40+
41+
# new way!
42+
from django_typer.management import TyperCommand, command, group, initialize, Typer
3143
3244
:big:`Installation`
3345

tests/settings/no_django_typer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from .base import *
22

33
INSTALLED_APPS = [
4-
#"django_typer",
54
"django.contrib.admin",
65
"django.contrib.auth",
76
"django.contrib.contenttypes",

0 commit comments

Comments
 (0)