Skip to content

Commit e307691

Browse files
committed
Use ruff for formatting
1 parent e080fe6 commit e307691

19 files changed

+68
-56
lines changed

.github/workflows/python.yml

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
uses: abatilo/actions-poetry@v2
2424
- name: Install poetry project
2525
run: poetry install
26+
- name: Format
27+
run: poetry run ruff format --check
2628
- name: Lint
2729
run: poetry run ruff check --extend-select I
2830
- name: Run pyright

arcparse/_argument_helpers.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
def _check_short_format(short: str) -> None:
1919
if not re.match(r"^-[^-]+$", short):
20-
raise InvalidArgument(f"Invalid argument short-hand \"{short}\", expected a dash followed by non-dash characters")
20+
raise InvalidArgument(f'Invalid argument short-hand "{short}", expected a dash followed by non-dash characters')
2121

2222

2323
def positional[T](
@@ -203,6 +203,7 @@ class Args:
203203
"""
204204
...
205205

206+
206207
@overload
207208
def subparsers[T](**kwargs: type[T]) -> T:
208209
"""
@@ -218,6 +219,7 @@ class Args:
218219
"""
219220
...
220221

222+
221223
def subparsers(*args, **kwargs) -> Any:
222224
if args:
223225
assert all(isinstance(arg, str) for arg in args)
@@ -252,7 +254,6 @@ def dict_positional[T](
252254
)
253255

254256

255-
256257
def dict_option[T](
257258
dict_: dict[str, T],
258259
*,

arcparse/_partial_arguments.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class BasePartialArgument[R: ContainerApplicable](ABC):
3737
mx_group: PartialMxGroup | None = None
3838

3939
@abstractmethod
40-
def resolve_with_typehint(self, name: str, typehint: type) -> R:
41-
...
40+
def resolve_with_typehint(self, name: str, typehint: type) -> R: ...
4241

4342
def resolve_to_kwargs(self, name: str, typehint: type) -> dict[str, Any]:
4443
return {}
@@ -75,7 +74,9 @@ def resolve_to_kwargs(self, name: str, typehint: type) -> dict[str, Any]:
7574
if get_origin(typehint) in {Union, UnionType}:
7675
union_args = get_args(typehint)
7776
if len(union_args) > 2 or NoneType not in union_args:
78-
raise InvalidTypehint("Union can be used only for optional arguments (length of 2, 1 of them being None)")
77+
raise InvalidTypehint(
78+
"Union can be used only for optional arguments (length of 2, 1 of them being None)"
79+
)
7980

8081
if type_ is not str and get_origin(type_) != Literal:
8182
if extract_collection_type(typehint):
@@ -87,7 +88,7 @@ def resolve_to_kwargs(self, name: str, typehint: type) -> dict[str, Any]:
8788
elif callable(type_):
8889
self.converter = cast(Callable[[str], T], type_)
8990
else:
90-
raise InvalidTypehint(f"Type of argument \"{name}\" is not callable")
91+
raise InvalidTypehint(f'Type of argument "{name}" is not callable')
9192

9293
choices = self.choices
9394
if literal_choices := extract_literal_strings(type_):
@@ -145,7 +146,9 @@ class PartialOption[T](BasePartialValueArgument[T, Option]):
145146

146147
def resolve_with_typehint(self, name: str, typehint: type) -> Option:
147148
if self.short_only and self.short is None and len(name) > 1:
148-
raise InvalidArgument(f"Argument \"{name}\" requested short_only but name is longer than 1 character and no short-hand was specified")
149+
raise InvalidArgument(
150+
f'Argument "{name}" requested short_only but name is longer than 1 character and no short-hand was specified'
151+
)
149152

150153
kwargs = self.resolve_to_kwargs(name, typehint)
151154
return Option(**kwargs)
@@ -190,7 +193,9 @@ class PartialFlag(BaseSinglePartialArgument[Flag]):
190193

191194
def resolve_with_typehint(self, name: str, typehint: type) -> Flag:
192195
if self.short_only and self.short is None and len(name) > 1:
193-
raise InvalidArgument(f"Argument \"{name}\" requested short_only but name is longer than 1 character and no short-hand was specified")
196+
raise InvalidArgument(
197+
f'Argument "{name}" requested short_only but name is longer than 1 character and no short-hand was specified'
198+
)
194199

195200
kwargs = self.resolve_to_kwargs(name, typehint)
196201
kwargs["short"] = self.short

arcparse/_typehints.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def extract_subparsers_from_typehint(typehint: type) -> list[type]:
2929
origin = get_origin(typehint)
3030
if origin in {Union, UnionType}:
3131
return list(get_args(typehint))
32-
raise InvalidTypehint(f"Unable to extract subparser types from {typehint}, expected a non-empty union of ArcParser types")
32+
raise InvalidTypehint(
33+
f"Unable to extract subparser types from {typehint}, expected a non-empty union of ArcParser types"
34+
)
3335

3436

3537
def extract_type_from_typehint(typehint: type) -> type:

arcparse/arguments.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
class Void:
2727
pass
2828

29+
2930
void = Void()
3031

3132

3233
class ContainerApplicable(Protocol):
33-
def apply(self, actions_container: _ActionsContainer, name: str) -> None:
34-
...
34+
def apply(self, actions_container: _ActionsContainer, name: str) -> None: ...
3535

3636

3737
@dataclass(kw_only=True)
@@ -44,8 +44,7 @@ def apply(self, actions_container: _ActionsContainer, name: str) -> None:
4444
actions_container.add_argument(*args, **kwargs)
4545

4646
@abstractmethod
47-
def get_argparse_args(self, name: str) -> list[str]:
48-
...
47+
def get_argparse_args(self, name: str) -> list[str]: ...
4948

5049
def get_argparse_kwargs(self, name: str) -> dict[str, Any]:
5150
kwargs = {}

arcparse/converters.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class itemwise[T]:
1010
arguments accepting multiple values (nargs="*"), the return type should
1111
always be wrapped in a list.
1212
"""
13+
1314
def __init__(self, converter: Callable[[str], T]) -> None:
1415
self._converter = converter
1516

@@ -31,7 +32,13 @@ def csv[T](type_: type[T] = str, /) -> Callable[[str], list[T]]:
3132
return sv(",", type_)
3233

3334

34-
def sv_dict[K, V](item_separator: str, key_value_separator: str, *, key_type: Callable[[str], K] = str, value_type: Callable[[str], V] = str) -> Callable[[str], dict[K, V]]:
35+
def sv_dict[K, V](
36+
item_separator: str,
37+
key_value_separator: str,
38+
*,
39+
key_type: Callable[[str], K] = str,
40+
value_type: Callable[[str], V] = str,
41+
) -> Callable[[str], dict[K, V]]:
3542
def conv(arg: str) -> dict[K, V]:
3643
items = arg.split(item_separator)
3744
d = {}

arcparse/errors.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
class InvalidParser(Exception):
42
pass
53

arcparse/parser.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ def _construct_object_with_parsed(self, parsed: dict[str, Any]) -> T:
9191
# optional subparsers will result in `dict[name]` being `None`
9292
if chosen_subparser := parsed.get(name):
9393
if chosen_subparser not in subparsers.sub_parsers:
94-
raise InvalidParser(f"`{self.shape.__name__}.{name}` was overriden by argument/subparser with same name, got \"{chosen_subparser}\" but should be one of {set(subparsers.sub_parsers.keys())}")
94+
raise InvalidParser(
95+
f'`{self.shape.__name__}.{name}` was overriden by argument/subparser with same name, got "{chosen_subparser}" but should be one of {set(subparsers.sub_parsers.keys())}'
96+
)
9597
sub_parser = subparsers.sub_parsers[chosen_subparser]
9698
parsed[name] = sub_parser._construct_object_with_parsed(parsed)
9799

@@ -103,10 +105,7 @@ def _construct_object_with_parsed(self, parsed: dict[str, Any]) -> T:
103105
value = parsed.get(name, argument.default)
104106
if isinstance(argument.converter, itemwise):
105107
assert isinstance(value, list)
106-
parsed[name] = [
107-
argument.converter(item) if isinstance(item, str) else item
108-
for item in value
109-
]
108+
parsed[name] = [argument.converter(item) if isinstance(item, str) else item for item in value]
110109
else:
111110
parsed[name] = argument.converter(value) if isinstance(value, str) else value
112111

@@ -219,8 +218,7 @@ def _make_parser[T](shape: type[T]) -> Parser[T]:
219218
case (name, typehint, partial_subparsers):
220219
subshapes = partial_subparsers.shapes or extract_subparsers_from_typehint(typehint)
221220
subparsers_by_name = {
222-
name: _make_parser(subshape)
223-
for name, subshape in zip(partial_subparsers.names, subshapes)
221+
name: _make_parser(subshape) for name, subshape in zip(partial_subparsers.names, subshapes)
224222
}
225223
subparsers = (name, Subparsers(subparsers_by_name, required=not union_contains_none(typehint)))
226224
case _:

examples/subparsers.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55

66
class Action(Protocol):
7-
def run_action(self) -> None:
8-
...
7+
def run_action(self) -> None: ...
98

109

1110
class HiAction(Action):

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pyright = "^1.1.335"
2626
pytest = "^7.4.3"
2727
ruff = "^0.3.7"
2828

29+
[tool.ruff]
30+
line-length = 120
31+
2932
[tool.ruff.lint.isort]
3033
from-first = true
3134
lines-after-imports = 2

tests/test_auto_converter.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Args:
3030
"literal": None,
3131
}
3232

33+
3334
@pytest.mark.parametrize(
3435
"arg_string,provided",
3536
[
@@ -40,7 +41,7 @@ class Args:
4041
("--regex ^\\d+$", {"regex": re.compile(r"^\d+$")}),
4142
("--literal yes", {"literal": "yes"}),
4243
("--literal no", {"literal": "no"}),
43-
]
44+
],
4445
)
4546
def test_auto_converter_valid(arg_string: str, provided: dict[str, Any]) -> None:
4647
parsed = Args.parse(arg_string.split())
@@ -49,17 +50,10 @@ def test_auto_converter_valid(arg_string: str, provided: dict[str, Any]) -> None
4950
assert getattr(parsed, k) == v
5051

5152

52-
@pytest.mark.parametrize(
53-
"arg_string",
54-
[
55-
"--num foo",
56-
"--result bar",
57-
"--regex '('"
58-
]
59-
)
53+
@pytest.mark.parametrize("arg_string", ["--num foo", "--result bar", "--regex '('"])
6054
def test_option_invalid(arg_string: str) -> None:
6155
with pytest.raises(BaseException):
62-
Args.parse(args = arg_string.split())
56+
Args.parse(args=arg_string.split())
6357

6458

6559
def test_enum_positional() -> None:

tests/test_converter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def from_int(cls, arg: str) -> "Result":
1818

1919

2020
def test_itemwise() -> None:
21-
2221
# @arcparser
2322
class Xd:
2423
results: list[Result] = option(converter=itemwise(Result.from_int))
24+
2525
Args = arcparser(Xd)
2626
args = Args.parse("--results 0 1 0".split())
2727
assert isinstance(args.results, list)

tests/test_dict_helpers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_dict_positional_default_in_dict() -> None:
2020
[
2121
("foo", 1),
2222
("bar", 0),
23-
]
23+
],
2424
)
2525
def test_dict_positional(arg_string: str, value: int) -> None:
2626
@arcparser
@@ -41,7 +41,7 @@ def test_dict_option_default_in_dict() -> None:
4141
[
4242
("--foo-bar foo", 1),
4343
("--foo-bar bar", 0),
44-
]
44+
],
4545
)
4646
def test_dict_option(arg_string: str, value: int) -> None:
4747
@arcparser

tests/test_flag.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Args:
3939
("-z", {"baz": True}),
4040
("-o", {"boo": True}),
4141
("-c", {"c": True}),
42-
]
42+
],
4343
)
4444
def test_flag_valid(arg_string: str, provided: dict[str, Any]) -> None:
4545
parsed = Args.parse(arg_string.split())
@@ -56,12 +56,11 @@ def test_flag_valid(arg_string: str, provided: dict[str, Any]) -> None:
5656
"--no-barrr",
5757
"--no-foo",
5858
"--boo",
59-
]
59+
],
6060
)
6161
def test_flag_invalid(arg_string: str) -> None:
6262
with pytest.raises(SystemExit):
63-
Args.parse(args = arg_string.split())
64-
63+
Args.parse(args=arg_string.split())
6564

6665

6766
@pytest.mark.parametrize(

tests/test_inheritance.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
("--bar", defaults | {"bar": True}),
1616
("--baz", defaults | {"baz": True}),
1717
("--foo --bar --baz", {"foo": True, "bar": True, "baz": True}),
18-
]
18+
],
1919
)
2020
def test_inheritance(arg_string: str, result: dict[str, str]) -> None:
2121
class Parent1:
@@ -36,9 +36,11 @@ class Argsi(Parent1, Parent2):
3636
class Common:
3737
debug: bool
3838

39+
3940
class FooArgs(Common):
4041
foo: bool
4142

43+
4244
class BarArgs(Common):
4345
bar: bool
4446

tests/test_option.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Args:
3131
("--foo A --bar B -z C", {"foo": "A", "bar": "B", "baz": "C"}),
3232
("--foo A -z C --boo D", {"foo": "A", "baz": "C", "boo": "D"}),
3333
("--foo A -c B", {"foo": "A", "c": "B"}),
34-
]
34+
],
3535
)
3636
def test_option_valid(arg_string: str, provided: dict[str, Any]) -> None:
3737
parsed = Args.parse(arg_string.split())
@@ -45,8 +45,8 @@ def test_option_valid(arg_string: str, provided: dict[str, Any]) -> None:
4545
[
4646
"--foo foo --bar bar --baz baz",
4747
"--bar bar",
48-
]
48+
],
4949
)
5050
def test_option_invalid(arg_string: str) -> None:
5151
with pytest.raises(SystemExit):
52-
Args.parse(args = arg_string.split())
52+
Args.parse(args=arg_string.split())

tests/test_positional.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Args:
2424
("A", {"foo": "A"}),
2525
("A B", {"foo": "A", "bar": "B"}),
2626
("A B C", {"foo": "A", "bar": "B", "baz": "C"}),
27-
]
27+
],
2828
)
2929
def test_positional_valid(arg_string: str, provided: dict[str, Any]) -> None:
3030
parsed = Args.parse(arg_string.split())
@@ -38,8 +38,8 @@ def test_positional_valid(arg_string: str, provided: dict[str, Any]) -> None:
3838
[
3939
"",
4040
"A B C D",
41-
]
41+
],
4242
)
4343
def test_positional_invalid(arg_string: str) -> None:
4444
with pytest.raises(SystemExit):
45-
Args.parse(args = arg_string.split())
45+
Args.parse(args=arg_string.split())

tests/test_short.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def test_short_option_invalid() -> None:
99
option("-fo-o")
1010

1111
with pytest.raises(InvalidArgument):
12+
1213
@arcparser
1314
class Args:
1415
foo: str = option(short_only=True)
@@ -19,6 +20,7 @@ def test_short_flag_invalid() -> None:
1920
flag("-fo-o")
2021

2122
with pytest.raises(InvalidArgument):
23+
2224
@arcparser
2325
class Args:
2426
foo: bool = flag(short_only=True)

0 commit comments

Comments
 (0)