Skip to content

Commit

Permalink
remove lowercasing from generator function - should be done with case…
Browse files Browse the repository at this point in the history
…_sensitive flag
  • Loading branch information
svlandeg committed Sep 12, 2024
1 parent adb7c03 commit 9ad26c2
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def main(
names: Tuple[str, str, str, SuperHero] = typer.Argument(
("Harry", "Hermione", "Ron", "hero3"),
enum_by_name=True,
case_sensitive=False,
help="Select 4 characters to play with",
),
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SuperHero(str, Enum):
def main(
names: Annotated[
Tuple[str, str, str, SuperHero],
typer.Argument(enum_by_name=True, help="Select 4 characters to play with"),
typer.Argument(enum_by_name=True, help="Select 4 characters to play with", case_sensitive=False),
] = ("Harry", "Hermione", "Ron", "hero3"),
):
for name in names:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_invalid_args():


def test_valid_args():
result = runner.invoke(app, ["Draco", "Hagrid", "Dobby", "hero1"])
result = runner.invoke(app, ["Draco", "Hagrid", "Dobby", "HERO1"])
assert result.exit_code == 0
assert "Hello Draco" in result.stdout
assert "Hello Hagrid" in result.stdout
Expand Down
8 changes: 4 additions & 4 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,13 +650,13 @@ def convertor(value: Any) -> Any:


def generate_enum_name_convertor(enum: Type[Enum]) -> Callable[..., Any]:
lower_name_map = {str(item.name).lower(): item for item in enum}
val_map = {str(item.name): item for item in enum}

def convertor(value: Any) -> Any:
if value is not None:
low = str(value).lower()
if low in lower_name_map:
return lower_name_map[low]
val = str(value)
if val in val_map:
return val_map[val]

return convertor

Expand Down

0 comments on commit 9ad26c2

Please sign in to comment.