Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
65a07e9
Add documentation for functional API enums
Torvaney May 17, 2022
e3f2d03
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] May 17, 2022
4fe60e7
Merge branch 'master' into enum-functional-api-docs
tiangolo Dec 16, 2022
3c0fc7d
Merge branch 'master' into enum-functional-api-docs
svlandeg Jul 19, 2024
10c9aa8
Move new tutorial file to 004
svlandeg Jul 19, 2024
7abe600
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] Jul 19, 2024
4700981
Add annotated version
svlandeg Jul 19, 2024
b91c009
Fix line numbers
svlandeg Jul 19, 2024
acdbc27
Merge branch 'master' into enum-functional-api-docs
svlandeg Aug 9, 2024
411ba56
Merge branch 'master' into enum-functional-api-docs
svlandeg Aug 28, 2025
478e2eb
update docs to new format
svlandeg Aug 28, 2025
20501a4
Merge branch 'master' into enum-functional-api-docs
svlandeg Sep 1, 2025
f7db506
Merge branch 'master' into enum-functional-api-docs
svlandeg Sep 22, 2025
e74b6f0
Merge branch 'enum-functional-api-docs' of https://github.com/Torvane…
svlandeg Sep 22, 2025
44f6a95
add test files
svlandeg Nov 3, 2025
f2426ba
Merge branch 'master' into enum-functional-api-docs
svlandeg Nov 25, 2025
d000e96
update tutorial files to use explicit Typer()
svlandeg Nov 25, 2025
ae656f2
Merge branch 'master' into enum-functional-api-docs
svlandeg Nov 25, 2025
38a0ac1
Merge branch 'master' into enum-functional-api-docs
svlandeg Mar 24, 2026
556376a
🎨 Auto format
pre-commit-ci-lite[bot] Mar 24, 2026
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
11 changes: 11 additions & 0 deletions docs/tutorial/parameter-types/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,14 @@ Error: Invalid value for '--network': 'capsule' is not one of 'simple', 'conv',
```

</div>


### Functional API

In order to use an `Enum` created using the <a href="https://docs.python.org/3/library/enum.html#functional-api" class="external-link" target="_blank">functional API</a>, you need to create an enum with string values.

You also need to supply the default value as a string (not the enum):

{* docs_src/parameter_types/enum/tutorial005_an.py hl[6,13] *}

Alternatively, you can create an `Enum` that extends both `str` and `Enum`. In Python 3.11+, there is <a href="https://docs.python.org/3.11/library/enum.html#enum.StrEnum" class="external-link" target="_blank">`enum.StrEnum`</a>. For Python 3.10 or earlier, there is the <a href="https://github.com/irgeek/StrEnum" class="external-link" target="_blank">StrEnum package</a>.
16 changes: 16 additions & 0 deletions docs_src/parameter_types/enum/tutorial005.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from enum import Enum

import typer

NeuralNetwork = Enum("NeuralNetwork", {k: k for k in ["simple", "conv", "lstm"]})

app = typer.Typer()


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


if __name__ == "__main__":
app()
19 changes: 19 additions & 0 deletions docs_src/parameter_types/enum/tutorial005_an.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from enum import Enum
from typing import Annotated

import typer

NeuralNetwork = Enum("NeuralNetwork", {k: k for k in ["simple", "conv", "lstm"]})

app = typer.Typer()


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


if __name__ == "__main__":
app()
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import subprocess
import sys

from typer.testing import CliRunner

from docs_src.parameter_types.enum import tutorial005 as mod

runner = CliRunner()
app = mod.app


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


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():
result = runner.invoke(app, ["--network", "capsule"])
assert result.exit_code != 0
assert "Invalid value for '--network'" in result.output
assert (
"invalid choice: capsule. (choose from" in result.output
or "'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,43 @@
import subprocess
import sys

from typer.testing import CliRunner

from docs_src.parameter_types.enum import tutorial005_an as mod

runner = CliRunner()
app = mod.app


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


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():
result = runner.invoke(app, ["--network", "capsule"])
assert result.exit_code != 0
assert "Invalid value for '--network'" in result.output
assert (
"invalid choice: capsule. (choose from" in result.output
or "'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
Loading