6
6
import os
7
7
import sys
8
8
from abc import ABC , abstractmethod
9
- from argparse import ArgumentDefaultsHelpFormatter , ArgumentParser , ArgumentTypeError , Namespace
9
+ from argparse import (
10
+ ArgumentDefaultsHelpFormatter ,
11
+ ArgumentParser ,
12
+ ArgumentTypeError ,
13
+ Namespace ,
14
+ _ArgumentGroup , # noqa: PLC2701
15
+ )
10
16
from collections import deque
11
17
from copy import deepcopy
12
18
from dataclasses import dataclass
16
22
from typing import TYPE_CHECKING , Any , Generic , TypeVar
17
23
18
24
if TYPE_CHECKING :
19
- from collections .abc import Iterable , Sequence
25
+ from collections .abc import Callable , Iterable , Mapping , Sequence
20
26
21
27
if sys .version_info >= (3 , 11 ): # pragma: >=3.11 cover
22
28
import tomllib
23
29
else : # pragma: <3.11 cover
24
30
import tomli as tomllib
25
31
32
+ ArgumentGroup = _ArgumentGroup
33
+
26
34
27
35
class FmtNamespace (Namespace ):
28
36
"""Options for pyproject-fmt tool."""
@@ -63,7 +71,7 @@ def filename(self) -> str:
63
71
raise NotImplementedError
64
72
65
73
@abstractmethod
66
- def add_format_flags (self , parser : ArgumentParser ) -> None :
74
+ def add_format_flags (self , parser : ArgumentGroup ) -> None :
67
75
"""
68
76
Add any additional flags to configure the formatter.
69
77
@@ -126,7 +134,7 @@ def _cli_args(info: TOMLFormatter[T], args: Sequence[str]) -> list[_Config[T]]:
126
134
:param args: CLI arguments
127
135
:return: the parsed options
128
136
"""
129
- parser = _build_cli (info )
137
+ parser , type_conversion = _build_cli (info )
130
138
parser .parse_args (namespace = info .opt , args = args )
131
139
res = []
132
140
for pyproject_toml in info .opt .inputs :
@@ -144,7 +152,9 @@ def _cli_args(info: TOMLFormatter[T], args: Sequence[str]) -> list[_Config[T]]:
144
152
if isinstance (config , dict ):
145
153
for key in set (vars (override_opt ).keys ()) - {"inputs" , "stdout" , "check" , "no_print_diff" }:
146
154
if key in config :
147
- setattr (override_opt , key , config [key ])
155
+ raw = config [key ]
156
+ converted = type_conversion [key ](raw ) if key in type_conversion else raw
157
+ setattr (override_opt , key , converted )
148
158
res .append (
149
159
_Config (
150
160
toml_filename = pyproject_toml ,
@@ -159,7 +169,7 @@ def _cli_args(info: TOMLFormatter[T], args: Sequence[str]) -> list[_Config[T]]:
159
169
return res
160
170
161
171
162
- def _build_cli (of : TOMLFormatter [T ]) -> ArgumentParser :
172
+ def _build_cli (of : TOMLFormatter [T ]) -> tuple [ ArgumentParser , Mapping [ str , Callable [[ Any ], Any ]]] :
163
173
parser = ArgumentParser (
164
174
formatter_class = ArgumentDefaultsHelpFormatter ,
165
175
prog = of .prog ,
@@ -200,15 +210,16 @@ def _build_cli(of: TOMLFormatter[T]) -> ArgumentParser:
200
210
help = "number of spaces to use for indentation" ,
201
211
metavar = "count" ,
202
212
)
203
- of .add_format_flags (format_group ) # type: ignore[arg-type]
213
+ of .add_format_flags (format_group )
214
+ type_conversion = {a .dest : a .type for a in format_group ._actions if a .type and a .dest } # noqa: SLF001
204
215
msg = "pyproject.toml file(s) to format, use '-' to read from stdin"
205
216
parser .add_argument (
206
217
"inputs" ,
207
218
nargs = "+" ,
208
219
type = partial (_toml_path_creator , of .filename ),
209
220
help = msg ,
210
221
)
211
- return parser
222
+ return parser , type_conversion
212
223
213
224
214
225
def _toml_path_creator (filename : str , argument : str ) -> Path | None :
@@ -289,6 +300,7 @@ def _color_diff(diff: Iterable[str]) -> Iterable[str]:
289
300
290
301
291
302
__all__ = [
303
+ "ArgumentGroup" ,
292
304
"FmtNamespace" ,
293
305
"TOMLFormatter" ,
294
306
"run" ,
0 commit comments