Skip to content

Commit 386f043

Browse files
author
Brian Kohan
committed
fix help issues #69
1 parent b2f56fd commit 386f043

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

django_typer/tests/apps/test_app/management/commands/help_precedence15.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Command(BaseCommand, help=_("")):
1313
Now class docstring is used!
1414
"""
1515

16-
help = None
16+
help = ""
1717

1818
def handle(self, arg1: str, arg2: str, arg3: float = 0.5, arg4: int = 1):
1919
assert self.__class__ == Command
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json
2+
3+
from django.utils.translation import gettext_lazy as _
4+
5+
from .help_precedence8 import Command as BaseCommand
6+
7+
8+
# if you really want to use a class docstring to override a base class help at
9+
# higher precedence you can unset the base class help attribute by setting the
10+
# higher precedence values to empty strings
11+
class Command(BaseCommand, help=None):
12+
"""
13+
Now class docstring is used!
14+
"""
15+
16+
help = None
17+
18+
def handle(self, arg1: str, arg2: str, arg3: float = 0.5, arg4: int = 1):
19+
assert self.__class__ == Command
20+
opts = {"arg1": arg1, "arg2": arg2, "arg3": arg3, "arg4": arg4}
21+
return json.dumps(opts)

django_typer/tests/test_help_precedence.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ def test_help_precedence15(self):
148148
)
149149
self.assertIn("Now class docstring is used!", buffer.getvalue())
150150

151+
def test_help_precedence16(self):
152+
buffer = StringIO()
153+
cmd = get_command("help_precedence16", stdout=buffer, no_color=True)
154+
cmd.print_help("./manage.py", "help_precedence16")
155+
self.assertNotIn(
156+
"Test minimal TyperCommand subclass - typer param", buffer.getvalue()
157+
)
158+
self.assertIn("Now class docstring is used!", buffer.getvalue())
159+
151160
@pytest.mark.skip()
152161
def test_help_from_other_dir(self):
153162
"""

doc/source/howto.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,26 @@ The precedence order, for a simple command is as follows:
652652
4: Function docstring is last priority and is not subject to translation.
653653
"""
654654
655+
The rule for how helps are resolved when inheriting from other commands is that higher precedence
656+
helps in base classes will be chosen over lower priority helps in deriving classes. However, if
657+
you would like to use a docstring as the help in a derived class instead of the high priority
658+
help in a base class you can set the equivalent priority help in the deriving class to the empty
659+
string:
660+
661+
.. code-block:: python
662+
663+
class Command(TyperCommand, help=_("High precedence help defined in base class.")):
664+
...
665+
666+
...
667+
668+
from upstream.management.commands.command import Command as BaseCommand
669+
class Command(BaseCommand, help=None):
670+
"""
671+
Docstring will be used as help.
672+
"""
673+
674+
655675
656676
Document Commands w/Sphinx
657677
--------------------------

0 commit comments

Comments
 (0)