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

📝 Add StrEnum example for python 3.11+ #745

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
22 changes: 19 additions & 3 deletions docs/tutorial/parameter-types/enum.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
To define a *CLI parameter* that can take a value from a predefined set of values you can use a standard Python <a href="https://docs.python.org/3/library/enum.html" class="external-link" target="_blank">`enum.Enum`</a>:

```Python hl_lines="1 6 7 8 9 12 13"
{!../docs_src/parameter_types/enum/tutorial001.py!}
```

=== "Python 3.11+"

```Python hl_lines="1 6 7 8 9 12 13"
{!> ../docs_src/parameter_types/enum/tutorial001_str_enum.py!}
```

=== "Python 3.7+"

```Python hl_lines="1 6 7 8 9 12 13"
{!> ../docs_src/parameter_types/enum/tutorial001.py!}
```


!!! tip
Notice that the function parameter `network` will be an `Enum`, not a `str`.
Expand Down Expand Up @@ -51,6 +61,12 @@ Error: Invalid value for '--network': invalid choice: CONV. (choose from simple,

You can make an `Enum` (choice) *CLI parameter* be case-insensitive with the `case_sensitive` parameter:

=== "Python 3.11+"

```Python hl_lines="15"
{!> ../docs_src/parameter_types/enum/tutorial002_str_enum.py!}
```

=== "Python 3.7+"

```Python hl_lines="15"
Expand Down
17 changes: 17 additions & 0 deletions docs_src/parameter_types/enum/tutorial001_str_enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from enum import StrEnum

import typer


class NeuralNetwork(StrEnum):
simple = "simple"
conv = "conv"
lstm = "lstm"


def main(network: NeuralNetwork = NeuralNetwork.simple):
print(f"Training neural network of type: {network.value}")


if __name__ == "__main__":
typer.run(main)
22 changes: 22 additions & 0 deletions docs_src/parameter_types/enum/tutorial002_str_enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from enum import StrEnum

import typer
from typing_extensions import Annotated


class NeuralNetwork(StrEnum):
simple = "simple"
conv = "conv"
lstm = "lstm"


def main(
network: Annotated[
NeuralNetwork, typer.Option(case_sensitive=False)
] = NeuralNetwork.simple,
):
print(f"Training neural network of type: {network.value}")


if __name__ == "__main__":
typer.run(main)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import subprocess
import sys

import typer
from typer.testing import CliRunner

from docs_src.parameter_types.enum import tutorial001_str_enum as mod

runner = CliRunner()

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


def test_help():
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "--network" in result.output
assert "[simple|conv|lstm]" in result.output
assert "default: simple" in result.output


def test_main():
result = runner.invoke(app, ["--network", "conv"])
assert result.exit_code == 0
assert "Training neural network of type: conv" in result.output


def test_invalid_case():
result = runner.invoke(app, ["--network", "CONV"])
assert result.exit_code != 0
assert "Invalid value for '--network': 'CONV' is not one of" in result.output
assert "simple" in result.output
assert "conv" in result.output
assert "lstm" in result.output


def test_invalid_other():
result = runner.invoke(app, ["--network", "capsule"])
assert result.exit_code != 0
assert "Invalid value for '--network': 'capsule' is not one of" in result.output
assert "simple" in result.output
assert "conv" in result.output
assert "lstm" in result.output


def test_script():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
capture_output=True,
encoding="utf-8",
)
assert "Usage" in result.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import subprocess
import sys

import typer
from typer.testing import CliRunner

from docs_src.parameter_types.enum import tutorial002_str_enum as mod

runner = CliRunner()

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


def test_upper():
result = runner.invoke(app, ["--network", "CONV"])
assert result.exit_code == 0
assert "Training neural network of type: conv" in result.output


def test_mix():
result = runner.invoke(app, ["--network", "LsTm"])
assert result.exit_code == 0
assert "Training neural network of type: lstm" in result.output


def test_script():
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],
capture_output=True,
encoding="utf-8",
)
assert "Usage" in result.stdout