Skip to content

Commit

Permalink
Support for optional as UnionType (Python > 3.10)
Browse files Browse the repository at this point in the history
Signed-off-by: Meir Komet <[email protected]>
  • Loading branch information
mkomet committed Jan 27, 2024
1 parent 3a7264c commit 92d1179
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
21 changes: 21 additions & 0 deletions tests/test_type_conversion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from enum import Enum
from pathlib import Path
from typing import Any, List, Optional, Tuple
Expand Down Expand Up @@ -29,6 +30,26 @@ def opt(user: Optional[str] = None):
assert "User: Camila" in result.output


@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
def test_optional_uniontype():
app = typer.Typer()

@app.command()
def opt(user: str | None = None):
if user:
print(f"User: {user}")
else:
print("No user")

result = runner.invoke(app)
assert result.exit_code == 0
assert "No user" in result.output

result = runner.invoke(app, ["--user", "Camila"])
assert result.exit_code == 0
assert "User: Camila" in result.output


def test_no_type():
app = typer.Typer()

Expand Down
20 changes: 19 additions & 1 deletion typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
except ImportError: # pragma: nocover
rich = None # type: ignore

if sys.version_info >= (3, 10):
from types import UnionType
else:
# Python < 3.10 doesn't have UnionType, so we define it manually as non inheritable type
UnionType = type("", (), {})

_original_except_hook = sys.excepthook
_typer_developer_exception_attr_name = "__typer_developer_exception__"

Expand Down Expand Up @@ -816,8 +822,9 @@ def get_click_param(
is_tuple = False
parameter_type: Any = None
is_flag = None
is_union_type = lenient_issubclass(type(main_type), UnionType)
origin = getattr(main_type, "__origin__", None)
if origin is not None:
if origin is not None or is_union_type:
# Handle Optional[SomeType]
if origin is Union:
types = []
Expand All @@ -828,6 +835,17 @@ def get_click_param(
assert len(types) == 1, "Typer Currently doesn't support Union types"
main_type = types[0]
origin = getattr(main_type, "__origin__", None)
# Handle (SomeType | None)
elif is_union_type:
types = []
for type_ in main_type.__args__:
if type_ is NoneType:
continue
types.append(type_)
assert (
len(types) == 1
), "Typer Currently doesn't support UnionType other than (T | None)"
main_type = types[0]
# Handle Tuples and Lists
if lenient_issubclass(origin, List):
main_type = main_type.__args__[0]
Expand Down

0 comments on commit 92d1179

Please sign in to comment.