Skip to content

Commit b8e0522

Browse files
committed
allow handle() to be an initializer #24
1 parent f435e5e commit b8e0522

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

django_typer/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,15 +867,17 @@ def decorator(func: t.Callable[..., t.Any]):
867867
setattr(
868868
func,
869869
"_typer_callback_",
870-
lambda cmd, _name=None, **extra: cmd.typer_app.callback(
870+
lambda cmd, _name=None, _help=Default(
871+
None
872+
), **extra: cmd.typer_app.callback(
871873
name=name or _name,
872874
cls=type("_AdaptedCallback", (cls,), {"django_command": cmd}),
873875
invoke_without_command=invoke_without_command,
874876
subcommand_metavar=subcommand_metavar,
875877
chain=chain,
876878
result_callback=result_callback,
877879
context_settings=context_settings,
878-
help=cmd.typer_app.info.help or help,
880+
help=cmd.typer_app.info.help or help or _help,
879881
epilog=epilog,
880882
short_help=short_help,
881883
options_metavar=options_metavar,
@@ -886,7 +888,9 @@ def decorator(func: t.Callable[..., t.Any]):
886888
rich_help_panel=rich_help_panel,
887889
**kwargs,
888890
**extra,
889-
)(func),
891+
)(
892+
func
893+
),
890894
)
891895
return func
892896

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from click import get_current_context
2+
3+
from django_typer import TyperCommand, command, initialize
4+
5+
6+
class Command(TyperCommand):
7+
8+
help = "Test handle as initializer."
9+
10+
@initialize(invoke_without_command=True)
11+
def handle(self):
12+
if (ctx := get_current_context(silent=True)) and ctx.invoked_subcommand:
13+
return
14+
15+
# if we're here a subcommand was not invoked
16+
return "handle"
17+
18+
@command()
19+
def subcommand(self):
20+
return "subcommand"

django_typer/tests/tests.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ def test_command_line(self, settings=None):
12521252

12531253
self.assertEqual(get_command("groups").echo(message="hey!").strip(), "hey!")
12541254

1255-
result = run_command("groups", *settings, "echo", "hey!", "5")
1255+
result = run_command("groups", "--no-color", *settings, "echo", "hey!", "5")
12561256
if override:
12571257
self.assertEqual(result[0].strip(), ("hey! " * 5).strip())
12581258
self.assertEqual(
@@ -1413,7 +1413,15 @@ def test_command_line(self, settings=None):
14131413
self.assertEqual(grp_cmd.lower(4, 9), "ANNAmonteS")
14141414

14151415
result = run_command(
1416-
"groups", *settings, "string", "annamontes", "case", "upper", "4", "9"
1416+
"groups",
1417+
"--no-color",
1418+
*settings,
1419+
"string",
1420+
"annamontes",
1421+
"case",
1422+
"upper",
1423+
"4",
1424+
"9",
14171425
)
14181426
if override:
14191427
self.assertIn(
@@ -2634,12 +2642,12 @@ class TracebackTests(TestCase):
26342642
"""
26352643

26362644
def test_usage_error_no_tb(self):
2637-
stdout, stderr, retcode = run_command("tb", "wrong")
2645+
stdout, stderr, retcode = run_command("tb", "--no-color", "wrong")
26382646
self.assertTrue("Usage: ./manage.py tb [OPTIONS] COMMAND [ARGS]" in stdout)
26392647
self.assertTrue("No such command" in stderr)
26402648
self.assertTrue(retcode > 0)
26412649

2642-
stdout, stderr, retcode = run_command("tb", "error", "wrong")
2650+
stdout, stderr, retcode = run_command("tb", "--no-color", "error", "wrong")
26432651
self.assertTrue("Usage: ./manage.py tb error [OPTIONS]" in stdout)
26442652
self.assertTrue("Got unexpected extra argument" in stderr)
26452653
self.assertTrue(retcode > 0)
@@ -2652,7 +2660,9 @@ def test_usage_error_no_tb(self):
26522660

26532661
def test_usage_error_with_tb_if_requested(self):
26542662

2655-
stdout, stderr, retcode = run_command("tb", "--traceback", "wrong")
2663+
stdout, stderr, retcode = run_command(
2664+
"tb", "--no-color", "--traceback", "wrong"
2665+
)
26562666
self.assertFalse(stdout.strip())
26572667
self.assertTrue("Traceback" in stderr)
26582668
if rich_installed:
@@ -2662,7 +2672,9 @@ def test_usage_error_with_tb_if_requested(self):
26622672
self.assertTrue("No such command 'wrong'" in stderr)
26632673
self.assertTrue(retcode > 0)
26642674

2665-
stdout, stderr, retcode = run_command("tb", "--traceback", "error", "wrong")
2675+
stdout, stderr, retcode = run_command(
2676+
"tb", "--no-color", "--traceback", "error", "wrong"
2677+
)
26662678
self.assertFalse(stdout.strip())
26672679
self.assertTrue("Traceback" in stderr)
26682680
if rich_installed:
@@ -2705,3 +2717,31 @@ def test_exit_on_call(self):
27052717

27062718
with self.assertRaises(SystemExit):
27072719
call_command("tb", "exit", "--code=1")
2720+
2721+
2722+
class TestHandleAsInit(TestCase):
2723+
2724+
def test_handle_as_init_run(self):
2725+
2726+
stdout, stderr, retcode = run_command("handle_as_init")
2727+
self.assertTrue("handle" in stdout)
2728+
self.assertFalse(stderr.strip())
2729+
self.assertEqual(retcode, 0)
2730+
2731+
stdout, stderr, retcode = run_command("handle_as_init", "subcommand")
2732+
self.assertTrue("subcommand" in stdout)
2733+
self.assertFalse(stderr.strip())
2734+
self.assertEqual(retcode, 0)
2735+
2736+
def test_handle_as_init_call(self):
2737+
2738+
self.assertEqual(call_command("handle_as_init").strip(), "handle")
2739+
self.assertEqual(
2740+
call_command("handle_as_init", "subcommand").strip(), "subcommand"
2741+
)
2742+
2743+
def test_handle_as_init_direct(self):
2744+
2745+
self.assertEqual(get_command("handle_as_init")(), "handle")
2746+
self.assertEqual(get_command("handle_as_init", "subcommand")(), "subcommand")
2747+
self.assertEqual(get_command("handle_as_init").subcommand(), "subcommand")

doc/source/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ v1.0.2
1111
* Fixed `Run type checking with django-stubs installed. <https://github.com/bckohan/django-typer/issues/30>`_
1212
* Fixed `Add pyright to linting and resolve any pyright errors. <https://github.com/bckohan/django-typer/issues/29>`_
1313
* Fixed `Missing subcommand produces stack trace without --traceback. <https://github.com/bckohan/django-typer/issues/27>`
14+
* Fixed `Allow handle() to be an initializer. <https://github.com/bckohan/django-typer/issues/24>`_
1415

1516
v1.0.1
1617
======

0 commit comments

Comments
 (0)