Skip to content

Commit 179b0b1

Browse files
committed
1 parent 321694d commit 179b0b1

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

django_typer/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@
151151
]
152152

153153
P = ParamSpec("P")
154+
P2 = ParamSpec("P2")
154155
R = t.TypeVar("R")
156+
R2 = t.TypeVar("R2")
155157
C = t.TypeVar("C", bound=BaseCommand)
156158

157159
_CACHE_KEY = "_register_typer"
@@ -1171,6 +1173,16 @@ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
11711173
assert self.initializer
11721174
return self.initializer(*args, **kwargs)
11731175

1176+
@t.overload
1177+
def __get__(
1178+
self, obj: t.Union[None, t.Type["TyperCommand"]], owner: t.Any = None
1179+
) -> "CommandGroup[P, R]": ...
1180+
1181+
@t.overload
1182+
def __get__(
1183+
self, obj: "TyperCommand", owner: t.Any = None
1184+
) -> t.Union[MethodType, t.Callable[P, R]]: ...
1185+
11741186
def __get__(
11751187
self, obj: t.Any, owner: t.Any = None
11761188
) -> t.Union["CommandGroup[P, R]", MethodType, t.Callable[P, R]]:
@@ -1347,7 +1359,7 @@ def command( # type: ignore
13471359
# Rich settings
13481360
rich_help_panel: t.Union[str, None] = Default(None),
13491361
**kwargs,
1350-
) -> t.Callable[[t.Callable[P, R]], t.Callable[P, R]]:
1362+
) -> t.Callable[[t.Callable[P2, R2]], t.Callable[P2, R2]]:
13511363
"""
13521364
A function decorator that creates a new command and attaches it to this group.
13531365
This is a passthrough to Typer.command() and the options are the same, except
@@ -1418,7 +1430,7 @@ def command1(self):
14181430
**kwargs,
14191431
)
14201432

1421-
def make_command(f: t.Callable[P, R]) -> t.Callable[P, R]:
1433+
def make_command(f: t.Callable[P2, R2]) -> t.Callable[P2, R2]:
14221434
owner = kwargs.pop("_owner", None)
14231435
if owner:
14241436
# attach this function to the adapted Command class
@@ -1464,7 +1476,7 @@ def group(
14641476
# Rich settings
14651477
rich_help_panel: t.Union[str, None] = Default(None),
14661478
**kwargs,
1467-
) -> t.Callable[[t.Callable[P, R]], "CommandGroup[P, R]"]:
1479+
) -> t.Callable[[t.Callable[P2, R2]], "CommandGroup[P2, R2]"]:
14681480
"""
14691481
Create a new subgroup and attach it to this group. This is like creating a new
14701482
Typer app and adding it to a parent Typer app. The kwargs are passed through
@@ -1540,7 +1552,7 @@ def subcommand(self):
15401552
)
15411553
return grp
15421554

1543-
def create_app(func: t.Callable[P, R]) -> CommandGroup[P, R]:
1555+
def create_app(func: t.Callable[P2, R2]) -> CommandGroup[P2, R2]:
15441556
owner = t.cast(t.Optional[t.Type[TyperCommand]], kwargs.pop("_owner", None))
15451557
self.groups.append(
15461558
CommandGroup( # pyright: ignore[reportArgumentType]
@@ -1597,7 +1609,7 @@ def initialize(
15971609
# Rich settings
15981610
rich_help_panel: t.Union[str, None] = Default(None),
15991611
**kwargs,
1600-
) -> t.Callable[[t.Callable[P, R]], t.Callable[P, R]]:
1612+
) -> t.Callable[[t.Callable[P2, R2]], t.Callable[P2, R2]]:
16011613
"""
16021614
A function decorator that creates a Typer_
16031615
`callback <https://typer.tiangolo.com/tutorial/commands/callback/>`_. This
@@ -1690,7 +1702,7 @@ def divide(
16901702
this can be used to group commands into panels in the help output.
16911703
"""
16921704

1693-
def make_initializer(func: t.Callable[P, R]) -> t.Callable[P, R]:
1705+
def make_initializer(func: t.Callable[P2, R2]) -> t.Callable[P2, R2]:
16941706
_cache_initializer(
16951707
func,
16961708
common_init=True,

django_typer/tests/apps/test_app2/management/commands/groups.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Command(GroupsCommand, epilog="Overridden from test_app."):
2323

2424
suppressed_base_arguments = ["--version"]
2525

26+
setting_name: str
27+
2628
@initialize()
2729
def init(self, verbosity: types.Verbosity = verbosity):
2830
"""
@@ -47,7 +49,7 @@ def echo(
4749

4850
# test override base class command and remove arguments
4951
@GroupsCommand.case.command()
50-
def upper(self):
52+
def upper(self) -> None:
5153
return super().upper(0, None)
5254

5355
@GroupsCommand.string.command()
@@ -63,7 +65,7 @@ def setting(
6365
Get or set Django settings.
6466
"""
6567
assert self.__class__ is Command
66-
self.setting = setting
68+
self.setting_name = setting
6769

6870
@setting.command()
6971
def print(
@@ -75,5 +77,5 @@ def print(
7577
"""
7678
assert self.__class__ is Command
7779
if safe:
78-
return getattr(settings, self.setting, None)
79-
return getattr(settings, self.setting)
80+
return getattr(settings, self.setting_name, None)
81+
return getattr(settings, self.setting_name)

0 commit comments

Comments
 (0)