1
1
from __future__ import annotations
2
2
3
3
import abc
4
- import attr
5
4
import types
6
5
import typing
7
6
7
+ import attr
8
+
8
9
from sail import errors
9
10
from sail .internal import empty
10
11
from sail .traits import argument_parser_trait
@@ -136,9 +137,7 @@ def __type__(self) -> typing.Type[ContainerT]:
136
137
return self .collection_type
137
138
138
139
def parse (
139
- self ,
140
- argument : typing .Iterable [object ],
141
- default : ContainerT | empty .Empty = empty .EMPTY
140
+ self , argument : typing .Iterable [object ], default : ContainerT | empty .Empty = empty .EMPTY
142
141
) -> ContainerT :
143
142
try :
144
143
return self .collection_type .__call__ (argument )
@@ -151,7 +150,6 @@ def parse(
151
150
152
151
@attr .define ()
153
152
class SequenceParser (_ContainerParser [SequenceT ]):
154
-
155
153
def __attrs_post_init__ (self ) -> None :
156
154
# In case some non-instantiable generic was passed, default to list.
157
155
if isinstance (self .collection_type , abc .ABCMeta ):
@@ -160,7 +158,6 @@ def __attrs_post_init__(self) -> None:
160
158
161
159
@attr .define ()
162
160
class SetParser (_ContainerParser [SetT ]):
163
-
164
161
def __attrs_post_init__ (self ) -> None :
165
162
# In case some non-instantiable generic was passed, default to set.
166
163
if isinstance (self .collection_type , abc .ABCMeta ):
@@ -178,29 +175,26 @@ def __type__(self) -> typing.Type[None]:
178
175
return types .NoneType
179
176
180
177
def parse (
181
- self ,
182
- argument : typing .Sequence [object ],
183
- default : typing .Any | empty .Empty = empty .EMPTY
178
+ self , argument : typing .Sequence [object ], default : typing .Any | empty .Empty = empty .EMPTY
184
179
) -> typing .Any :
185
180
if len (argument ) == 1 :
186
181
return argument [0 ]
187
-
182
+
188
183
elif len (argument ) > 1 :
189
184
raise errors .ConversionError (
190
185
argument ,
191
186
self .__type__ ,
192
- TypeError ("Got more than 1 argument for a parameter without a container type." )
187
+ TypeError ("Got more than 1 argument for a parameter without a container type." ),
193
188
)
194
-
189
+
195
190
elif empty .is_nonempty (default ):
196
191
return default
197
192
198
193
raise errors .ConversionError (
199
- argument ,
200
- self .__type__ ,
201
- TypeError ("Got 0 arguments for required parameter." )
194
+ argument , self .__type__ , TypeError ("Got 0 arguments for required parameter." )
202
195
) from None
203
196
197
+
204
198
@attr .define ()
205
199
class JoinedStringParser (argument_parser_trait .ContainerParser [str ]):
206
200
@@ -211,16 +205,14 @@ def __type__(self) -> typing.Type[str]:
211
205
return str
212
206
213
207
def parse (
214
- self ,
215
- argument : typing .Sequence [object ],
216
- default : str | empty .Empty = empty .EMPTY
208
+ self , argument : typing .Sequence [object ], default : str | empty .Empty = empty .EMPTY
217
209
) -> str :
218
210
if not argument :
219
211
if empty .is_nonempty (default ):
220
212
return default
221
213
222
214
raise TypeError ("Got 0 arguments for required parameter." )
223
-
215
+
224
216
assert all (isinstance (arg , str ) for arg in argument )
225
217
226
218
return self .separator .join (typing .cast (typing .Sequence [str ], argument ))
0 commit comments