Skip to content

Commit ce3c7d1

Browse files
committed
fix handle() rename bug, flush out howto docs
1 parent d66095a commit ce3c7d1

File tree

7 files changed

+450
-28
lines changed

7 files changed

+450
-28
lines changed

django_typer/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,8 @@ def decorator(func: CommandFunctionType):
859859
setattr(
860860
func,
861861
"_typer_callback_",
862-
lambda cmd, **extra: cmd.typer_app.callback(
863-
name=name or extra.pop("name", None),
862+
lambda cmd, _name=None, **extra: cmd.typer_app.callback(
863+
name=name or _name,
864864
cls=type("_AdaptedCallback", (cls,), {"django_command": cmd}),
865865
invoke_without_command=invoke_without_command,
866866
subcommand_metavar=subcommand_metavar,
@@ -962,8 +962,8 @@ def decorator(func: CommandFunctionType):
962962
setattr(
963963
func,
964964
"_typer_command_",
965-
lambda cmd, **extra: cmd.typer_app.command(
966-
name=name or extra.pop("name", None),
965+
lambda cmd, _name=None, **extra: cmd.typer_app.command(
966+
name=name or _name,
967967
*args,
968968
cls=type("_AdaptedCommand", (cls,), {"django_command": cmd}),
969969
context_settings=context_settings,
@@ -1290,7 +1290,7 @@ def get_ctor(attr: str) -> t.Optional[t.Callable[..., t.Any]]:
12901290
if cmd_cls._handle:
12911291
ctor = get_ctor(cmd_cls._handle)
12921292
if ctor:
1293-
ctor(cls, name=cls.typer_app.info.name)
1293+
ctor(cls, _name=cls.typer_app.info.name)
12941294
else:
12951295
cls._num_commands += 1
12961296
cls.typer_app.command(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import typing as t
2+
3+
from typer import Argument, Option
4+
5+
from django_typer import TyperCommand, command
6+
7+
8+
class Command(TyperCommand):
9+
10+
@command()
11+
def option1(self, flag: bool = False):
12+
pass
13+
14+
@command()
15+
def option2(self, name: str = "world"):
16+
pass
17+
18+
@command()
19+
def option3(self, name: t.Annotated[str, Option(help="The name of the thing")]):
20+
pass
21+
22+
@command()
23+
def arg1(self, int_arg: int):
24+
pass
25+
26+
@command()
27+
def arg2(self, int_arg: t.Annotated[int, Argument(help="An integer argument")]):
28+
pass
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django_typer import TyperCommand, command
2+
3+
4+
class Command(TyperCommand):
5+
6+
@command(name="subcommand1")
7+
def handle(self): ...
8+
9+
@command()
10+
def subcommand2(self): ...
11+
12+
@command()
13+
def subcommand3(self): ...
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django_typer import TyperCommand, command
2+
3+
4+
class Command(TyperCommand):
5+
6+
@command(name="default")
7+
def handle(self):
8+
return "handle"
9+
10+
@command(name="renamed")
11+
def subcommand1(self):
12+
return "subcommand1"
13+
14+
@command("renamed2")
15+
def subcommand2(self):
16+
return "subcommand2"

django_typer/tests/tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ def test_command_context(self):
168168
self.assertEqual(basic, get_current_command())
169169
self.assertIsNone(get_current_command())
170170

171+
def test_renaming(self):
172+
self.assertEqual(run_command("rename", "default")[0].strip(), "handle")
173+
self.assertEqual(run_command("rename", "renamed")[0].strip(), "subcommand1")
174+
self.assertEqual(run_command("rename", "renamed2")[0].strip(), "subcommand2")
175+
176+
self.assertEqual(call_command("rename", "default"), "handle")
177+
self.assertEqual(call_command("rename", "renamed"), "subcommand1")
178+
self.assertEqual(call_command("rename", "renamed2"), "subcommand2")
179+
180+
self.assertEqual(get_command("rename")(), "handle")
181+
self.assertEqual(get_command("rename").subcommand1(), "subcommand1")
182+
self.assertEqual(get_command("rename").subcommand2(), "subcommand2")
183+
171184

172185
class CommandDefinitionTests(TestCase):
173186
def test_group_callback_throws(self):

0 commit comments

Comments
 (0)