Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix preserving case in enum values #571

Merged
merged 18 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
17 changes: 17 additions & 0 deletions docs_src/parameter_types/enum/tutorial003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from enum import Enum

import typer


class Interval(Enum):
ONE_MINUTE = "1m"
ONE_MONTH = "1M"
OTHER = "o"


def main(interval: Interval = typer.Option(Interval.OTHER, case_sensitive=True)):
print(f"Found interval: {interval.value}")


if __name__ == "__main__":
typer.run(main)
30 changes: 30 additions & 0 deletions tests/test_enum_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Regression test for
Enum values that differ in case get conflated #570
https://github.com/tiangolo/typer/discussions/570
"""
from enum import Enum

import pytest
import typer
from typer.testing import CliRunner

runner = CliRunner()
app = typer.Typer()


class Interval(Enum):
ONE_MINUTE = "1m"
ONE_MONTH = "1M"


@app.command()
def enum_case(interval: Interval):
print(interval.value)


@pytest.mark.parametrize("interval", ["1M", "1m"])
def test_enum_case(interval: str):
result = runner.invoke(app, [interval])
assert result.exit_code == 0
assert interval in result.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import subprocess
import sys

import pytest
import typer
from typer.testing import CliRunner

from docs_src.parameter_types.enum import tutorial003 as mod

runner = CliRunner()

app = typer.Typer()
app.command()(mod.main)


@pytest.mark.parametrize("interval", ["1M", "1m"])
def test_case(interval):
result = runner.invoke(app, ["--interval", interval])
assert result.exit_code == 0
assert f"Found interval: {interval}" in result.output


def test_script():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout
8 changes: 4 additions & 4 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,13 @@ def param_path_convertor(value: Optional[str] = None) -> Optional[Path]:


def generate_enum_convertor(enum: Type[Enum]) -> Callable[[Any], Any]:
lower_val_map = {str(val.value).lower(): val for val in enum}
val_map = {str(val.value): val for val in enum}

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

return convertor
Expand Down
Loading