76
76
from django .core .management import get_commands
77
77
from django .core .management .base import BaseCommand , CommandError
78
78
from django .core .management .base import OutputWrapper as BaseOutputWrapper
79
+ from django .core .management .color import Style as ColorStyle
79
80
from django .db .models import Model
80
81
from django .utils .translation import gettext as _
81
82
89
90
from typer .core import TyperGroup as CoreTyperGroup
90
91
from typer .main import get_command as get_typer_command
91
92
from typer .main import get_params_convertors_ctx_param_name_from_function
92
- from typer .models import CommandFunctionType
93
93
from typer .models import Context as TyperContext
94
94
from typer .models import Default , DefaultPlaceholder
95
95
96
96
from .completers import ModelObjectCompleter
97
97
from .parsers import ModelObjectParser
98
- from .types import ForceColor , NoColor , PythonPath , Settings , SkipChecks
99
- from .types import Style as ColorStyle
100
- from .types import Traceback , Verbosity , Version
98
+ from .types import (
99
+ ForceColor ,
100
+ NoColor ,
101
+ PythonPath ,
102
+ Settings ,
103
+ SkipChecks ,
104
+ Traceback ,
105
+ Verbosity ,
106
+ Version ,
107
+ )
101
108
from .utils import _command_context , traceback_config , with_typehint
102
109
103
110
VERSION = (1 , 0 , 1 )
@@ -713,7 +720,7 @@ def subcommand(self):
713
720
this can be used to group commands into panels in the help output.
714
721
"""
715
722
716
- def create_app (func : CommandFunctionType ):
723
+ def create_app (func : t . Callable [..., t . Any ] ):
717
724
grp = GroupFunction ( # type: ignore
718
725
name = name ,
719
726
cls = cls ,
@@ -855,7 +862,7 @@ def divide(
855
862
this can be used to group commands into panels in the help output.
856
863
"""
857
864
858
- def decorator (func : CommandFunctionType ):
865
+ def decorator (func : t . Callable [..., t . Any ] ):
859
866
setattr (
860
867
func ,
861
868
"_typer_callback_" ,
@@ -958,7 +965,7 @@ def other_command(self):
958
965
this can be used to group commands into panels in the help output.
959
966
"""
960
967
961
- def decorator (func : CommandFunctionType ):
968
+ def decorator (func : t . Callable [..., t . Any ] ):
962
969
setattr (
963
970
func ,
964
971
"_typer_command_" ,
@@ -1071,7 +1078,7 @@ def subcommand(self):
1071
1078
this can be used to group commands into panels in the help output.
1072
1079
"""
1073
1080
1074
- def create_app (func : CommandFunctionType ):
1081
+ def create_app (func : t . Callable [..., t . Any ] ):
1075
1082
grp = GroupFunction ( # type: ignore
1076
1083
name = name ,
1077
1084
cls = cls ,
@@ -1199,8 +1206,9 @@ def __new__(
1199
1206
This method is called when a new class is created.
1200
1207
"""
1201
1208
try :
1202
- TyperCommand # pylint: disable=pointless-statement
1203
- is_base_init = False
1209
+ # avoid unnecessary work creating the TyperCommand class
1210
+ # is there a less weird way to do this?
1211
+ is_base_init = not TyperCommand
1204
1212
except NameError :
1205
1213
is_base_init = True
1206
1214
typer_app = None
@@ -1267,7 +1275,7 @@ def __init__(cls, name, bases, attrs, **kwargs):
1267
1275
for arg in cls .suppressed_base_arguments or []
1268
1276
} # per django docs - allow these to be specified by either the option or param name
1269
1277
1270
- def get_ctor (attr : str ) -> t .Optional [t .Callable [..., t .Any ]]:
1278
+ def get_ctor (attr : t . Any ) -> t .Optional [t .Callable [..., t .Any ]]:
1271
1279
return getattr (
1272
1280
attr , "_typer_command_" , getattr (attr , "_typer_callback_" , None )
1273
1281
)
@@ -1671,19 +1679,20 @@ def command2(self, option: t.Optional[str] = None):
1671
1679
style : ColorStyle
1672
1680
stdout : BaseOutputWrapper
1673
1681
stderr : BaseOutputWrapper
1674
- requires_system_checks : t .Union [t .Sequence [str ], str ]
1682
+ # requires_system_checks: t.Union[t.List [str], t.Tuple[ str, ...], t.Literal['__all__'] ]
1675
1683
1676
1684
# we do not use verbosity because the base command does not do anything with it
1677
1685
# if users want to use a verbosity flag like the base django command adds
1678
1686
# they can use the type from django_typer.types.Verbosity
1679
- suppressed_base_arguments : t . Optional [ t . Iterable [ str ]] = {"verbosity" }
1687
+ suppressed_base_arguments = {"verbosity" }
1680
1688
1681
1689
typer_app : Typer
1682
1690
no_color : bool = False
1683
1691
force_color : bool = False
1684
1692
_num_commands : int = 0
1685
1693
_has_callback : bool = False
1686
1694
_root_groups : int = 0
1695
+ _handle : t .Callable [..., t .Any ]
1687
1696
1688
1697
command_tree : CommandNode
1689
1698
@@ -1701,8 +1710,8 @@ def __exit__(self, exc_type, exc_val, exc_tb):
1701
1710
1702
1711
def __init__ (
1703
1712
self ,
1704
- stdout : t .Optional [t .IO [ str ] ] = None ,
1705
- stderr : t .Optional [t .IO [ str ] ] = None ,
1713
+ stdout : t .Optional [t .TextIO ] = None ,
1714
+ stderr : t .Optional [t .TextIO ] = None ,
1706
1715
no_color : bool = no_color ,
1707
1716
force_color : bool = force_color ,
1708
1717
** kwargs ,
@@ -1795,7 +1804,9 @@ def __init_subclass__(cls, **_):
1795
1804
"""Avoid passing typer arguments up the subclass init chain"""
1796
1805
return super ().__init_subclass__ ()
1797
1806
1798
- def create_parser (self , prog_name : str , subcommand : str , ** _ ):
1807
+ def create_parser ( # pyright: ignore[reportIncompatibleMethodOverride]
1808
+ self , prog_name : str , subcommand : str , ** _
1809
+ ):
1799
1810
"""
1800
1811
Create a parser for this command. This also sets the command
1801
1812
context, so any functions below this call on the stack may
@@ -1862,7 +1873,7 @@ def handle(self, option1: bool, option2: bool):
1862
1873
:param kwargs: the options to directly pass to handle()
1863
1874
"""
1864
1875
with self :
1865
- if getattr (self , "_handle" , None ):
1876
+ if getattr (self , "_handle" , None ) and callable ( self . _handle ) :
1866
1877
return self ._handle (* args , ** kwargs )
1867
1878
raise NotImplementedError (
1868
1879
_ (
0 commit comments