9
9
import sys
10
10
from argparse import SUPPRESS , Action , ArgumentDefaultsHelpFormatter , ArgumentParser , Namespace
11
11
from pathlib import Path
12
- from typing import Any , Callable , Dict , List , Optional , Sequence , Tuple , Type , TypeVar , cast
12
+ from typing import TYPE_CHECKING , Any , Callable , Dict , List , Optional , Sequence , Tuple , Type , TypeVar , cast
13
13
14
14
from tox .config .loader .str_convert import StrConvert
15
15
from tox .plugin import NAME
16
- from tox .session .state import State
17
16
18
17
from .env_var import get_env_var
19
18
from .ini import IniConfig
23
22
else : # pragma: no cover (py38+)
24
23
from typing_extensions import Literal
25
24
25
+ if TYPE_CHECKING :
26
+ from tox .session .state import State
27
+
26
28
27
29
class ArgumentParserWithEnvAndConfig (ArgumentParser ):
28
30
"""
@@ -93,8 +95,6 @@ def _get_help_string(self, action: Action) -> str | None:
93
95
return text
94
96
95
97
96
- Handler = Callable [[State ], int ]
97
-
98
98
ToxParserT = TypeVar ("ToxParserT" , bound = "ToxParser" )
99
99
DEFAULT_VERBOSITY = 2
100
100
@@ -122,7 +122,7 @@ class ToxParser(ArgumentParserWithEnvAndConfig):
122
122
123
123
def __init__ (self , * args : Any , root : bool = False , add_cmd : bool = False , ** kwargs : Any ) -> None :
124
124
self .of_cmd : str | None = None
125
- self .handlers : dict [str , tuple [Any , Handler ]] = {}
125
+ self .handlers : dict [str , tuple [Any , Callable [[ State ], int ] ]] = {}
126
126
self ._arguments : list [ArgumentArgs ] = []
127
127
self ._groups : list [tuple [Any , dict [str , Any ], list [tuple [dict [str , Any ], list [ArgumentArgs ]]]]] = []
128
128
super ().__init__ (* args , ** kwargs )
@@ -136,7 +136,13 @@ def __init__(self, *args: Any, root: bool = False, add_cmd: bool = False, **kwar
136
136
else :
137
137
self ._cmd = None
138
138
139
- def add_command (self , cmd : str , aliases : Sequence [str ], help_msg : str , handler : Handler ) -> ArgumentParser :
139
+ def add_command (
140
+ self ,
141
+ cmd : str ,
142
+ aliases : Sequence [str ],
143
+ help_msg : str ,
144
+ handler : Callable [[State ], int ],
145
+ ) -> ArgumentParser :
140
146
if self ._cmd is None :
141
147
raise RuntimeError ("no sub-command group allowed" )
142
148
sub_parser : ToxParser = self ._cmd .add_parser (
@@ -217,7 +223,7 @@ def _add_base_options(self) -> None:
217
223
218
224
def parse_known_args ( # type: ignore[override]
219
225
self ,
220
- args : Sequence [str ] | None ,
226
+ args : Sequence [str ] | None = None ,
221
227
namespace : Parsed | None = None ,
222
228
) -> tuple [Parsed , list [str ]]:
223
229
if args is None :
@@ -315,5 +321,4 @@ def add_core_arguments(parser: ArgumentParser) -> None:
315
321
"DEFAULT_VERBOSITY" ,
316
322
"Parsed" ,
317
323
"ToxParser" ,
318
- "Handler" ,
319
324
)
0 commit comments