107
107
)
108
108
from .utils import _command_context , traceback_config , with_typehint
109
109
110
+ if sys .version_info < (3 , 9 ):
111
+ from typing_extensions import ParamSpec
112
+ else :
113
+ from typing import ParamSpec
114
+
115
+
110
116
VERSION = (1 , 0 , 2 )
111
117
112
118
__title__ = "Django Typer"
126
132
"model_parser_completer" ,
127
133
]
128
134
135
+ P = ParamSpec ("P" )
136
+ R = t .TypeVar ("R" )
137
+
129
138
130
139
def model_parser_completer (
131
140
model_cls : t .Type [Model ],
@@ -532,7 +541,9 @@ class GroupFunction(Typer):
532
541
django_command_cls : t .Type ["TyperCommand" ]
533
542
_callback : t .Callable [..., t .Any ]
534
543
535
- def __get__ (self , obj , obj_type = None ):
544
+ def __get__ (
545
+ self , obj : t .Optional ["TyperCommand" ], _ = None
546
+ ) -> t .Union ["GroupFunction" , MethodType ]:
536
547
"""
537
548
Our Typer app wrapper also doubles as a descriptor, so when
538
549
it is accessed on the instance, we return the wrapped function
@@ -590,8 +601,8 @@ def command( # type: ignore
590
601
deprecated : bool = False ,
591
602
# Rich settings
592
603
rich_help_panel : t .Union [str , None ] = Default (None ),
593
- ** kwargs ,
594
- ):
604
+ ** kwargs : t . Dict [ str , t . Any ] ,
605
+ ) -> t . Callable [[ t . Callable [ P , R ]], t . Callable [ P , R ]] :
595
606
"""
596
607
A function decorator that creates a new command and attaches it to this group.
597
608
This is a passthrough to Typer.command() and the options are the same, except
@@ -637,21 +648,29 @@ def command1(self):
637
648
:param rich_help_panel: the rich help panel to use - if rich is installed
638
649
this can be used to group commands into panels in the help output.
639
650
"""
640
- return super ().command (
641
- name = name ,
642
- cls = cls ,
643
- context_settings = context_settings ,
644
- help = help ,
645
- epilog = epilog ,
646
- short_help = short_help ,
647
- options_metavar = options_metavar ,
648
- add_help_option = add_help_option ,
649
- no_args_is_help = no_args_is_help ,
650
- hidden = hidden ,
651
- deprecated = deprecated ,
652
- rich_help_panel = rich_help_panel ,
653
- ** kwargs ,
654
- )
651
+
652
+ def decorator (f : t .Callable [P , R ]) -> t .Callable [P , R ]:
653
+ return super ( # pylint: disable=super-with-arguments
654
+ GroupFunction , self
655
+ ).command (
656
+ name = name ,
657
+ cls = cls ,
658
+ context_settings = context_settings ,
659
+ help = help ,
660
+ epilog = epilog ,
661
+ short_help = short_help ,
662
+ options_metavar = options_metavar ,
663
+ add_help_option = add_help_option ,
664
+ no_args_is_help = no_args_is_help ,
665
+ hidden = hidden ,
666
+ deprecated = deprecated ,
667
+ rich_help_panel = rich_help_panel ,
668
+ ** kwargs ,
669
+ )(
670
+ f
671
+ )
672
+
673
+ return decorator
655
674
656
675
def group (
657
676
self ,
@@ -673,8 +692,8 @@ def group(
673
692
deprecated : bool = Default (False ),
674
693
# Rich settings
675
694
rich_help_panel : t .Union [str , None ] = Default (None ),
676
- ** kwargs ,
677
- ):
695
+ ** kwargs : t . Dict [ str , t . Any ] ,
696
+ ) -> t . Callable [[ t . Callable [..., t . Any ]], "GroupFunction" ] :
678
697
"""
679
698
Create a new subgroup and attach it to this group. This is like creating a new
680
699
Typer app and adding it to a parent Typer app. The kwargs are passed through
@@ -721,7 +740,7 @@ def subcommand(self):
721
740
this can be used to group commands into panels in the help output.
722
741
"""
723
742
724
- def create_app (func : t .Callable [..., t .Any ]):
743
+ def create_app (func : t .Callable [..., t .Any ]) -> GroupFunction :
725
744
grp = GroupFunction ( # type: ignore
726
745
name = name ,
727
746
cls = cls ,
@@ -769,8 +788,8 @@ def initialize(
769
788
deprecated : bool = Default (False ),
770
789
# Rich settings
771
790
rich_help_panel : t .Union [str , None ] = Default (None ),
772
- ** kwargs ,
773
- ):
791
+ ** kwargs : t . Dict [ str , t . Any ] ,
792
+ ) -> t . Callable [[ t . Callable [ P , R ]], t . Callable [ P , R ]] :
774
793
"""
775
794
A function decorator that creates a Typer_
776
795
`callback <https://typer.tiangolo.com/tutorial/commands/callback/>`_. This
@@ -863,7 +882,7 @@ def divide(
863
882
this can be used to group commands into panels in the help output.
864
883
"""
865
884
866
- def decorator (func : t .Callable [..., t . Any ]) :
885
+ def decorator (func : t .Callable [P , R ]) -> t . Callable [ P , R ] :
867
886
setattr (
868
887
func ,
869
888
"_typer_callback_" ,
@@ -899,7 +918,7 @@ def decorator(func: t.Callable[..., t.Any]):
899
918
900
919
def command ( # pylint: disable=keyword-arg-before-vararg
901
920
name : t .Optional [str ] = None ,
902
- * args ,
921
+ * ,
903
922
cls : t .Type [TyperCommandWrapper ] = TyperCommandWrapper ,
904
923
context_settings : t .Optional [t .Dict [t .Any , t .Any ]] = None ,
905
924
help : t .Optional [str ] = None , # pylint: disable=redefined-builtin
@@ -912,8 +931,8 @@ def command( # pylint: disable=keyword-arg-before-vararg
912
931
deprecated : bool = False ,
913
932
# Rich settings
914
933
rich_help_panel : t .Union [str , None ] = Default (None ),
915
- ** kwargs ,
916
- ):
934
+ ** kwargs : t . Dict [ str , t . Any ] ,
935
+ ) -> t . Callable [[ t . Callable [ P , R ]], t . Callable [ P , R ]] :
917
936
"""
918
937
A function decorator that creates a new command and attaches it to the root
919
938
command group. This is a passthrough to
@@ -970,13 +989,12 @@ def other_command(self):
970
989
this can be used to group commands into panels in the help output.
971
990
"""
972
991
973
- def decorator (func : t .Callable [..., t . Any ]) :
992
+ def decorator (func : t .Callable [P , R ]) -> t . Callable [ P , R ] :
974
993
setattr (
975
994
func ,
976
995
"_typer_command_" ,
977
996
lambda cmd , _name = None , _help = None , ** extra : cmd .typer_app .command (
978
997
name = name or _name ,
979
- * args ,
980
998
cls = type ("_AdaptedCommand" , (cls ,), {"django_command" : cmd }),
981
999
context_settings = context_settings ,
982
1000
help = help or _help ,
@@ -1017,8 +1035,8 @@ def group(
1017
1035
deprecated : bool = Default (False ),
1018
1036
# Rich settings
1019
1037
rich_help_panel : t .Union [str , None ] = Default (None ),
1020
- ** kwargs ,
1021
- ):
1038
+ ** kwargs : t . Dict [ str , t . Any ] ,
1039
+ ) -> t . Callable [[ t . Callable [..., t . Any ]], GroupFunction ] :
1022
1040
"""
1023
1041
A function decorator that creates a new subgroup and attaches it to the root
1024
1042
command group. This is like creating a new Typer_ app and adding it to a parent
@@ -1083,7 +1101,7 @@ def subcommand(self):
1083
1101
this can be used to group commands into panels in the help output.
1084
1102
"""
1085
1103
1086
- def create_app (func : t .Callable [..., t .Any ]):
1104
+ def create_app (func : t .Callable [..., t .Any ]) -> GroupFunction :
1087
1105
grp = GroupFunction ( # type: ignore
1088
1106
name = name ,
1089
1107
cls = cls ,
0 commit comments