Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/test_type_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,50 @@ def custom_parser(
assert result.exit_code == 0


def test_custom_parse_with_union_type():
"""parser= should bypass the 'no Union types' assertion."""
app = typer.Typer()

@app.command()
def cmd(
value: int | str = typer.Argument(
None, parser=lambda x: int(x) if x.isdigit() else x
),
):
print(repr(value))

result = runner.invoke(app, ["42"])
assert result.exit_code == 0
assert "42" in result.output


def test_custom_click_type_with_union_type():
"""click_type= should bypass the 'no Union types' assertion."""

class FlexType(click.ParamType):
name = "flex"

def convert(
self, value: Any, param: click.Parameter | None, ctx: click.Context | None
) -> Any:
try:
return int(value)
except ValueError:
return value

app = typer.Typer()

@app.command()
def cmd(
value: int | str = typer.Argument(None, click_type=FlexType()),
):
print(repr(value))

result = runner.invoke(app, ["hello"])
assert result.exit_code == 0
assert "hello" in result.output


def test_custom_click_type():
class BaseNumberParamType(click.ParamType):
name = "base_integer"
Expand Down
11 changes: 8 additions & 3 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1665,9 +1665,14 @@ def get_click_param(
if type_ is NoneType:
continue
types.append(type_)
assert len(types) == 1, "Typer Currently doesn't support Union types"
main_type = types[0]
origin = get_origin(main_type)
if not (
parameter_info.parser is not None
or parameter_info.click_type is not None
):
assert len(types) == 1, "Typer Currently doesn't support Union types"
if len(types) == 1:
main_type = types[0]
origin = get_origin(main_type)
# Handle Tuples and Lists
if lenient_issubclass(origin, list):
main_type = get_args(main_type)[0]
Expand Down
Loading