@@ -723,27 +723,25 @@ def _cache_initializer(
723
723
cls : t .Type [DTGroup ] = DTGroup ,
724
724
** kwargs ,
725
725
):
726
- if not hasattr (callback , _CACHE_KEY ):
727
-
728
- def register (
729
- cmd : "TyperCommand" ,
730
- _name : t .Optional [str ] = Default (None ),
731
- _help : t .Optional [str ] = Default (None ),
726
+ def register (
727
+ cmd : "TyperCommand" ,
728
+ _name : t .Optional [str ] = Default (None ),
729
+ _help : t .Optional [str ] = Default (None ),
730
+ ** extra ,
731
+ ):
732
+ return cmd .typer_app .callback (
733
+ name = name or _name ,
734
+ cls = type (
735
+ "_Initializer" ,
736
+ (cls ,),
737
+ {"django_command" : cmd , "common_init" : common_init },
738
+ ),
739
+ help = cmd .typer_app .info .help or help or _help ,
740
+ ** kwargs ,
732
741
** extra ,
733
- ):
734
- return cmd .typer_app .callback (
735
- name = name or _name ,
736
- cls = type (
737
- "_Initializer" ,
738
- (cls ,),
739
- {"django_command" : cmd , "common_init" : common_init },
740
- ),
741
- help = cmd .typer_app .info .help or help or _help ,
742
- ** kwargs ,
743
- ** extra ,
744
- )(_strip_static (callback ))
742
+ )(_strip_static (callback ))
745
743
746
- setattr (callback , _CACHE_KEY , register )
744
+ setattr (callback , _CACHE_KEY , register )
747
745
748
746
749
747
def _cache_command (
@@ -753,23 +751,21 @@ def _cache_command(
753
751
cls : t .Type [DTCommand ] = DTCommand ,
754
752
** kwargs ,
755
753
):
756
- if not hasattr (callback , _CACHE_KEY ):
757
-
758
- def register (
759
- cmd : "TyperCommand" ,
760
- _name : t .Optional [str ] = None ,
761
- _help : t .Optional [str ] = None ,
754
+ def register (
755
+ cmd : "TyperCommand" ,
756
+ _name : t .Optional [str ] = None ,
757
+ _help : t .Optional [str ] = None ,
758
+ ** extra ,
759
+ ):
760
+ return cmd .typer_app .command (
761
+ name = name or _name ,
762
+ cls = type ("_Command" , (cls ,), {"django_command" : cmd }),
763
+ help = help or _help or None ,
764
+ ** kwargs ,
762
765
** extra ,
763
- ):
764
- return cmd .typer_app .command (
765
- name = name or _name ,
766
- cls = type ("_Command" , (cls ,), {"django_command" : cmd }),
767
- help = help or _help or None ,
768
- ** kwargs ,
769
- ** extra ,
770
- )(_strip_static (callback ))
766
+ )(_strip_static (callback ))
771
767
772
- setattr (callback , _CACHE_KEY , register )
768
+ setattr (callback , _CACHE_KEY , register )
773
769
774
770
775
771
def _get_direct_function (
@@ -813,8 +809,8 @@ class Command(
813
809
814
810
if sys .version_info < (3 , 9 ):
815
811
# this is a workaround for a bug in python 3.8 that causes multiple
816
- # values for cls error. 3.8 support is sunsetting soon so we just punt
817
- # on this one - REMOVE in next version
812
+ # values for cls error. 3.8 support is sun setting soon so we just punt
813
+ # on this one - REMOVE when 3.8 support is dropped
818
814
kwargs .pop ("cls" , None )
819
815
return super ().__call__ (* args , ** kwargs )
820
816
@@ -861,11 +857,6 @@ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
861
857
return _get_direct_function (cmd , self )(* args , ** kwargs )
862
858
return super ().__call__ (* args , ** kwargs )
863
859
864
- @t .overload # pragma: no cover
865
- def __get__ (
866
- self , obj : "TyperCommand" , owner : t .Type ["TyperCommand" ]
867
- ) -> "Typer[P, R]" : ...
868
-
869
860
@t .overload # pragma: no cover
870
861
def __get__ (
871
862
self , obj : "TyperCommandMeta" , owner : t .Type ["TyperCommandMeta" ]
@@ -888,7 +879,7 @@ def __get__(
888
879
@t .overload # pragma: no cover
889
880
def __get__ (
890
881
self , obj : "TyperCommand" , owner : t .Any = None
891
- ) -> MethodType : # t.Union[MethodType, t.Callable[P, R]]
882
+ ) -> "Typer[P, R]" : # t.Union[MethodType, t.Callable[P, R]]
892
883
# todo - we could return the generic callable type here but the problem
893
884
# is self is included in the ParamSpec and it seems tricky to remove?
894
885
# MethodType loses the parameters but is preferable to type checking errors
@@ -913,18 +904,18 @@ def __getattr__(self, name: str) -> t.Any:
913
904
cmd_obj = self .cmd_obj ()
914
905
for cmd in self .registered_commands :
915
906
assert cmd .callback
916
- if name in [ cmd .callback .__name__ , cmd .name ] :
907
+ if name in ( cmd .callback .__name__ , cmd .name ) :
917
908
if cmd_obj :
918
909
return _get_direct_function (cmd_obj , cmd )
919
910
return cmd
920
911
for grp in self .registered_groups :
921
912
cmd_grp = t .cast (Typer , grp .typer_instance )
922
913
assert cmd_grp
923
- if name in [
914
+ if name in (
924
915
cmd_grp .name ,
925
916
grp .name ,
926
917
getattr (cmd_grp .info .callback , "__name__" , None ),
927
- ] :
918
+ ) :
928
919
return cmd_grp
929
920
raise AttributeError (
930
921
"{cls} object has no attribute {name}" .format (
@@ -1024,8 +1015,11 @@ def __init__(
1024
1015
self .registered_callback = typer_app .registered_callback
1025
1016
self .registered_commands = copy (typer_app .registered_commands )
1026
1017
self .registered_groups = deepcopy (typer_app .registered_groups )
1027
- if isinstance (self .rich_help_panel , DefaultPlaceholder ):
1028
- self .rich_help_panel = typer_app .rich_help_panel # type: ignore[unreachable]
1018
+ self .rich_help_panel = (
1019
+ typer_app .rich_help_panel
1020
+ if isinstance (self .rich_help_panel , DefaultPlaceholder )
1021
+ else self .rich_help_panel
1022
+ )
1029
1023
for param , cfg in vars (self .info ).items ():
1030
1024
if isinstance (cfg , DefaultPlaceholder ):
1031
1025
setattr (self .info , param , getattr (typer_app .info , param ))
@@ -1775,10 +1769,9 @@ def depth_first_match(
1775
1769
for grp in reversed (app .registered_groups ):
1776
1770
assert grp .typer_instance
1777
1771
grp_app = t .cast (Typer , grp .typer_instance )
1778
- if grp .typer_instance :
1779
- found = depth_first_match (grp_app , name )
1780
- if found :
1781
- return found
1772
+ found = depth_first_match (grp_app , name )
1773
+ if found :
1774
+ return found
1782
1775
return None
1783
1776
1784
1777
@@ -2999,22 +2992,21 @@ def __getattr__(self, name: str) -> t.Any:
2999
2992
and return that command or group if the attribute name matches the command/group
3000
2993
function OR its registered CLI name.
3001
2994
"""
3002
- if name != "typer_app" and self .typer_app :
3003
- init = getattr (
3004
- self .typer_app .registered_callback ,
3005
- "callback" ,
3006
- self .typer_app .info .callback ,
3007
- )
3008
- if init and init and name == init .__name__ :
3009
- return MethodType (init , self ) if is_method (init ) else staticmethod (init )
3010
- found = depth_first_match (self .typer_app , name )
3011
- if found :
3012
- if isinstance (found , Typer ):
3013
- # todo shouldn't be needed
3014
- found ._local .object = self
3015
- else :
3016
- return _get_direct_function (self , found )
3017
- return found
2995
+ init = getattr (
2996
+ self .typer_app .registered_callback ,
2997
+ "callback" ,
2998
+ self .typer_app .info .callback ,
2999
+ )
3000
+ if init and init and name == init .__name__ :
3001
+ return MethodType (init , self ) if is_method (init ) else staticmethod (init )
3002
+ found = depth_first_match (self .typer_app , name )
3003
+ if found :
3004
+ if isinstance (found , Typer ):
3005
+ # todo shouldn't be needed
3006
+ found ._local .object = self
3007
+ else :
3008
+ return _get_direct_function (self , found )
3009
+ return found
3018
3010
raise AttributeError (
3019
3011
"{cls} object has no attribute {name}" .format (
3020
3012
cls = self .__class__ .__name__ , name = name
0 commit comments