Skip to content

Commit 4990e72

Browse files
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
1 parent 290387b commit 4990e72

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

tests/test_type_conversion.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,14 @@ def opt(user: str | None = None):
5050
assert result.exit_code == 0
5151
assert "User: Camila" in result.output
5252

53+
5354
@pytest.mark.parametrize(
5455
("value", "expected"),
55-
[
56-
("0", "ROOTED!"),
57-
("12", "ID: 12"),
58-
("name", "USER: name")
59-
],
56+
[("0", "ROOTED!"), ("12", "ID: 12"), ("name", "USER: name")],
6057
)
6158
def test_union(value, expected):
6259
app = typer.Typer()
63-
60+
6461
@app.command()
6562
def opt(id_or_name: Union[int, str]):
6663
if isinstance(id_or_name, int):
@@ -70,11 +67,11 @@ def opt(id_or_name: Union[int, str]):
7067
print(f"ID: {id_or_name}")
7168
else:
7269
print(f"USER: {id_or_name}")
73-
70+
7471
result = runner.invoke(app, [value])
7572
assert result.exit_code == 0
76-
assert expected in result.output
77-
73+
assert expected in result.output
74+
7875

7976
def test_optional_tuple():
8077
app = typer.Typer()

typer/main.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -700,16 +700,19 @@ def wrapper(**kwargs: Any) -> Any:
700700
update_wrapper(wrapper, callback)
701701
return wrapper
702702

703+
703704
class UnionParamType(click.ParamType):
704705
@property
705-
def name(self) -> str: # type: ignore
706-
return ' | '.join(_type.name for _type in self._types)
706+
def name(self) -> str: # type: ignore
707+
return " | ".join(_type.name for _type in self._types)
707708

708709
def __init__(self, types: List[click.ParamType]):
709710
super().__init__()
710711
self._types = types
711712

712-
def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[click.Context]) -> Any:
713+
def convert(
714+
self, value: Any, param: Optional[click.Parameter], ctx: Optional[click.Context]
715+
) -> Any:
713716
# *types, last = self._types
714717
error_messages = []
715718
for _type in self._types:
@@ -719,7 +722,7 @@ def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[cl
719722
print(type(e))
720723
error_messages.append(str(e))
721724
# return last.convert(value, param, ctx)
722-
raise self.fail('\n' + '\nbut also\n'.join(error_messages), param, ctx)
725+
raise self.fail("\n" + "\nbut also\n".join(error_messages), param, ctx)
723726

724727

725728
def get_click_type(
@@ -813,7 +816,10 @@ def get_click_type(
813816
case_sensitive=parameter_info.case_sensitive,
814817
)
815818
elif get_origin(annotation) is not None and is_union(get_origin(annotation)):
816-
types = [get_click_type(annotation=arg, parameter_info=parameter_info) for arg in get_args(annotation)]
819+
types = [
820+
get_click_type(annotation=arg, parameter_info=parameter_info)
821+
for arg in get_args(annotation)
822+
]
817823
return UnionParamType(types)
818824
raise RuntimeError(f"Type not yet supported: {annotation}") # pragma: no cover
819825

@@ -866,7 +872,7 @@ def get_click_param(
866872
continue
867873
types.append(type_)
868874
if len(types) == 1:
869-
main_type, = types
875+
(main_type,) = types
870876
origin = get_origin(main_type)
871877
else:
872878
for type_ in get_args(main_type):

0 commit comments

Comments
 (0)