Skip to content

Commit

Permalink
🐛 Fix typing & imports for python < 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoBouvier committed Nov 26, 2024
1 parent 8bb363d commit 9e7afc3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions tests/test_annotated.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import StrEnum, auto
from enum import Enum
from typing import Union

import typer
from typer.testing import CliRunner
Expand Down Expand Up @@ -84,12 +85,12 @@ class TestAnnotatedOptionAcceptsOptionalValue:
def test_enum(self):
app = typer.Typer()

class OptEnum(StrEnum):
val1 = auto()
val2 = auto()
class OptEnum(str, Enum):
val1 = "val1"
val2 = "val2"

@app.command()
def cmd(opt: Annotated[bool | OptEnum, typer.Option()] = OptEnum.val1):
def cmd(opt: Annotated[Union[bool, OptEnum], typer.Option()] = OptEnum.val1):
if opt is False:
print("False")
elif opt is True:
Expand Down Expand Up @@ -121,7 +122,7 @@ def test_int(self):
app = typer.Typer()

@app.command()
def cmd(opt: Annotated[bool | int, typer.Option()] = 1):
def cmd(opt: Annotated[Union[bool, int], typer.Option()] = 1):
print(opt)

result = runner.invoke(app)
Expand Down
2 changes: 1 addition & 1 deletion typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ def get_click_param(
else:
annotation = str
main_type = annotation
secondary_type: type[bool] | None = None
secondary_type: Union[Type[bool], None] = None
is_list = False
is_tuple = False
parameter_type: Any = None
Expand Down

0 comments on commit 9e7afc3

Please sign in to comment.