Skip to content

Commit d552c61

Browse files
committed
more tests for coverage
1 parent 77de187 commit d552c61

File tree

7 files changed

+142
-22
lines changed

7 files changed

+142
-22
lines changed

django_typer/__init__.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,6 @@ class Command(
798798
name=kwargs.pop("name", None) or cmd_module.__name__.split(".")[-1],
799799
**kwargs,
800800
typer_app=args[0] if args else None,
801-
inherit_extensions=True,
802801
):
803802
pass
804803

@@ -1830,9 +1829,6 @@ class TyperCommandMeta(type):
18301829
in the traceback configuration setting (off by default). This allows tracebacks
18311830
to be configured by the user on a per deployment basis in the settings file. We
18321831
therefore do not advise hardcoding this value.
1833-
:param inherit_extensions: whether to inherit extensions from the parent class, that
1834-
is any commands or groups that were defined on the parent class after its
1835-
definition.
18361832
"""
18371833

18381834
style: ColorStyle
@@ -1878,7 +1874,6 @@ def __new__(
18781874
True
18791875
),
18801876
pretty_exceptions_short: t.Union[DefaultPlaceholder, bool] = Default(True),
1881-
inherit_extensions: bool = False,
18821877
**kwargs,
18831878
):
18841879
"""
@@ -1949,21 +1944,6 @@ def command_bases() -> t.Generator[t.Type[TyperCommand], None, None]:
19491944
**kwargs,
19501945
)
19511946

1952-
if inherit_extensions:
1953-
for base in command_bases():
1954-
typer_app.registered_callback = (
1955-
typer_app.registered_callback
1956-
or base.typer_app.registered_callback
1957-
)
1958-
typer_app.registered_commands = [
1959-
*copy(base.typer_app.registered_commands),
1960-
*typer_app.registered_commands,
1961-
]
1962-
typer_app.registered_groups = [
1963-
*deepcopy(base.typer_app.registered_groups),
1964-
*typer_app.registered_groups,
1965-
]
1966-
19671947
# move up here
19681948
def get_ctor(attr: t.Any) -> t.Optional[t.Callable[..., t.Any]]:
19691949
return getattr(attr, _CACHE_KEY, None)
@@ -2553,6 +2533,8 @@ def init(self, ...):
25532533
)
25542534

25552535
def make_initializer(func: t.Callable[P, R]) -> t.Callable[P, R]:
2536+
# we might have to override a method defined in the base class
2537+
setattr(cmd, func.__name__, func)
25562538
cmd.typer_app.callback(
25572539
name=name,
25582540
cls=type("_Initializer", (cls,), {"django_command": cmd}),
@@ -2651,6 +2633,8 @@ def new_command(self, ...):
26512633
)
26522634

26532635
def make_command(func: t.Callable[P, R]) -> t.Callable[P, R]:
2636+
# we might have to override a method defined in the base class
2637+
setattr(cmd, func.__name__, func)
26542638
cmd.typer_app.command(
26552639
name=name,
26562640
cls=type("_Command", (cls,), {"django_command": cmd}),

django_typer/management/commands/shellcompletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ def get_completion() -> None:
589589
cmd_str=cmd_str
590590
)
591591
) from err
592-
raise # otherwise nowhere to go - just error out
592+
return # otherwise nowhere to go - just empty out
593593

594594
if isinstance(cmd, TyperCommand): # type: ignore[unreachable]
595595
# this will exit out so no return is needed here
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from django_typer.tests.apps.test_app.management.commands.method_override import (
2+
Command as MethodOverride,
3+
)
4+
5+
6+
@MethodOverride.initialize(invoke_without_command=True)
7+
def init(self):
8+
"""
9+
Command1
10+
"""
11+
assert self.__class__ is MethodOverride
12+
return f"adapter0::init()"
13+
14+
15+
@MethodOverride.command()
16+
def cmd1(self):
17+
"""
18+
Command1
19+
"""
20+
assert self.__class__ is MethodOverride
21+
return f"adapter0::cmd1()"
22+
23+
24+
@MethodOverride.command()
25+
def cmd2(self):
26+
"""
27+
Command1
28+
"""
29+
assert self.__class__ is MethodOverride
30+
return f"adapter0::cmd2()"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django_typer import TyperCommand, initialize, command
2+
3+
4+
class Command(TyperCommand):
5+
@initialize(invoke_without_command=True)
6+
def init(self):
7+
assert self.__class__ is Command
8+
return f"test_app::init()"
9+
10+
@command()
11+
def cmd1(self):
12+
"""
13+
Command1
14+
"""
15+
assert self.__class__ is Command
16+
return f"test_app::cmd1()"
17+
18+
@TyperCommand.command() # overly verbose but tolerated
19+
def cmd2(self):
20+
"""
21+
Command2
22+
"""
23+
assert self.__class__ is Command
24+
return f"test_app::cmd2()"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django_typer import initialize, command, group
2+
from .interference_ext import Command as InterferenceExt
3+
4+
5+
class Command(InterferenceExt):
6+
help = "Inherited and extend interference - test classmethod initialize()"
7+
8+
@InterferenceExt.initialize(invoke_without_command=True)
9+
def init(self, arg: int = 5):
10+
return f"test_app2::interference_ext_init::init({arg})"

django_typer/tests/test_interface.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ def test_typercommandmeta_interface_matches(self):
152152
from django_typer import TyperCommandMeta
153153

154154
typer_command_params = set(get_named_arguments(TyperCommandMeta.__new__))
155-
typer_command_params.remove("inherit_extensions")
156155
typer_params = set(get_named_arguments(typer.Typer.__init__))
157156
typer_params.remove("add_completion")
158157
typer_params.remove("cls")

django_typer/tests/test_interference.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,76 @@ def test_inherit_initialize_interference(self):
182182
self.assertEqual(
183183
call_command("interference_ext"), "test_app2::interference::init(True)"
184184
)
185+
186+
def test_initialize_classmethod_run(self):
187+
stdout, stderr, returncode = run_command(
188+
"interference_ext_init",
189+
"--settings",
190+
"django_typer.tests.settings.override",
191+
)
192+
self.assertEqual(returncode, 0, msg=stderr)
193+
self.assertEqual(stdout.strip(), "test_app2::interference_ext_init::init(5)")
194+
195+
stdout, stderr, returncode = run_command(
196+
"method_override",
197+
"--settings",
198+
"django_typer.tests.settings.adapted",
199+
)
200+
self.assertEqual(returncode, 0, msg=stderr)
201+
self.assertEqual(stdout.strip(), "adapter0::init()")
202+
203+
def test_initialize_classmethod_call(self):
204+
self.assertEqual(
205+
call_command("interference_ext_init"),
206+
"test_app2::interference_ext_init::init(5)",
207+
)
208+
209+
self.assertEqual(
210+
call_command("method_override"),
211+
"adapter0::init()",
212+
)
213+
214+
def test_initialize_classmethod_direct(self):
215+
from django_typer import TyperCommand
216+
217+
command = get_command("interference_ext_init", TyperCommand)
218+
self.assertEqual(command.init(), "test_app2::interference_ext_init::init(5)")
219+
220+
command = get_command("method_override", TyperCommand)
221+
self.assertEqual(command.init(), "adapter0::init()")
222+
223+
def test_command_classmethod_run(self):
224+
stdout, stderr, returncode = run_command(
225+
"method_override",
226+
"--settings",
227+
"django_typer.tests.settings.adapted",
228+
"cmd1",
229+
)
230+
self.assertEqual(returncode, 0, msg=stderr)
231+
self.assertEqual(stdout.strip(), "adapter0::cmd1()")
232+
233+
stdout, stderr, returncode = run_command(
234+
"method_override",
235+
"--settings",
236+
"django_typer.tests.settings.adapted",
237+
"cmd2",
238+
)
239+
self.assertEqual(returncode, 0, msg=stderr)
240+
self.assertEqual(stdout.strip(), "adapter0::cmd2()")
241+
242+
def test_command_classmethod_call(self):
243+
self.assertEqual(
244+
call_command("method_override", "cmd1"),
245+
"adapter0::cmd1()",
246+
)
247+
self.assertEqual(
248+
call_command("method_override", "cmd2"),
249+
"adapter0::cmd2()",
250+
)
251+
252+
def test_command_classmethod_direct(self):
253+
from django_typer import TyperCommand
254+
255+
command = get_command("method_override", TyperCommand)
256+
self.assertEqual(command.cmd1(), "adapter0::cmd1()")
257+
self.assertEqual(command.cmd2(), "adapter0::cmd2()")

0 commit comments

Comments
 (0)