-
-
Notifications
You must be signed in to change notification settings - Fork 845
✨ Add option to use Enum names instead of values on the commandline #224
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
Open
sirex
wants to merge
64
commits into
fastapi:master
Choose a base branch
from
sirex:enum-names
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+428
−10
Open
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
afc9cf4
Add option to use Enum names
sirex 2e079f2
Fix black issues
sirex b0f534e
Fix isort issues
sirex 2a18e31
Merge branch 'master' into enum-names
svlandeg 5769c4a
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] f4a99b2
rename 004 to 005
svlandeg 0ce6ade
Merge branch 'enum-names' of https://github.com/sirex/typer into enum…
svlandeg 5d63dbd
restore the original 003 as 004
svlandeg 9a00cd9
Merge branch 'master' into enum-names
svlandeg 2c9192f
Merge branch 'master' into enum-names
svlandeg ad046fc
Merge branch 'master' into enum-names
svlandeg a77dddd
fix issues
svlandeg dfaf7b3
rename to enum_by_name and fix the convertor code order
svlandeg d77ac5b
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] c39a5ea
Add console example for IntEnum
svlandeg b77a29f
Merge remote-tracking branch 'upstream_sirex/enum-names' into enum-names
svlandeg 8312bb8
Fix default values
svlandeg b5648ed
Add additional unit tests combining enums with list/tuple
svlandeg e24f446
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 0644919
pass along enum_by_name parameter to generate_X_convertor functions
svlandeg 478d183
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] eee3a4c
add Annotated versions of the new tests
svlandeg b5bd589
Merge remote-tracking branch 'upstream_sirex/enum-names' into enum-names
svlandeg e2053b1
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 221a865
ignore 006 tutorial just like 003 (mutable default argument)
svlandeg 7b59934
update enum.md to use the annotated versions as well
svlandeg 5759360
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 87ae5ad
add tests with Argument
svlandeg e13a083
Merge remote-tracking branch 'upstream_sirex/enum-names' into enum-names
svlandeg adb7c03
fix printing of enum value (needed for Python 3.11 and 3.12)
svlandeg 9ad26c2
remove lowercasing from generator function - should be done with case…
svlandeg 6ae6c9b
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] bfae3ef
fix hl_lines
svlandeg 825b477
Merge branch 'master' into enum-names
svlandeg 7f4f4e1
use new format
svlandeg c0107fb
fix
svlandeg 857e2ea
fix tests by checking output instead of stdout
svlandeg 4558ebf
Merge branch 'master' into enum-names
svlandeg 8582aee
Merge branch 'master' into enum-names
svlandeg 727f06c
move 007 to 004 to avoid conflict with master
svlandeg 2a5e8c4
fix mod
svlandeg b33239e
Merge branch 'master' into enum-names
svlandeg 3d6df84
update the tutorial examples to use explicit Typer() instance
svlandeg a18aea0
Merge branch 'master' into enum-names
svlandeg 82ae00a
🎨 Auto format
pre-commit-ci-lite[bot] d516837
use tuple, type and list
svlandeg dc2cbac
update test format to latest from master
svlandeg a877e98
🎨 Auto format
pre-commit-ci-lite[bot] ccbac9c
Merge branch 'master' into enum-names
svlandeg 04c68a6
fix lint and formatting
svlandeg 445d4c3
Merge remote-tracking branch 'upstream_sirex/enum-names' into enum-names
svlandeg bf153ff
fix wrongly resolved merge conflict
svlandeg 85e2a4b
fix docs
svlandeg dbce376
remove unnecessary (and unmentioned) extra files
svlandeg 8f92309
Merge branch 'master' into enum-names
svlandeg 0e62f69
🎨 Auto format
pre-commit-ci-lite[bot] cb75b2f
fix
svlandeg dfb1bc9
Merge remote-tracking branch 'upstream_sirex/enum-names' into enum-names
svlandeg cdffa1d
Merge branch 'master' into enum-names
svlandeg ced68ca
refactor to py310
svlandeg cacc67f
🎨 Auto format
pre-commit-ci-lite[bot] 2ab49cf
Merge branch 'master' into enum-names
svlandeg 406e291
format
svlandeg 5ce497b
Merge branch 'master' into enum-names
svlandeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import enum | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Access(enum.IntEnum): | ||
| private = 1 | ||
| protected = 2 | ||
| public = 3 | ||
| open = 4 | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(access: Annotated[Access, typer.Option(enum_by_name=True)] = "private"): | ||
| typer.echo(f"Access level: {access.name} ({access.value})") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import enum | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Access(enum.IntEnum): | ||
| private = 1 | ||
| protected = 2 | ||
| public = 3 | ||
| open = 4 | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(access: Access = typer.Option("private", enum_by_name=True)): | ||
| typer.echo(f"Access level: {access.name} ({access.value})") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from enum import Enum | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Food(str, Enum): | ||
| f1 = "Eggs" | ||
| f2 = "Bacon" | ||
| f3 = "Cheese" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| groceries: Annotated[list[Food], typer.Option(enum_by_name=True)] = ["f1", "f3"], | ||
| ): | ||
| print(f"Buying groceries: {', '.join([f.value for f in groceries])}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from enum import Enum | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Food(str, Enum): | ||
| f1 = "Eggs" | ||
| f2 = "Bacon" | ||
| f3 = "Cheese" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(groceries: list[Food] = typer.Option(["f1", "f3"], enum_by_name=True)): | ||
| print(f"Buying groceries: {', '.join([f.value for f in groceries])}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import enum | ||
| import logging | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class LogLevel(enum.Enum): | ||
| debug = logging.DEBUG | ||
| info = logging.INFO | ||
| warning = logging.WARNING | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(log_level: Annotated[LogLevel, typer.Option(enum_by_name=True)] = "warning"): | ||
| typer.echo(f"Log level set to: {logging.getLevelName(log_level.value)}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import enum | ||
| import logging | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class LogLevel(enum.Enum): | ||
| debug = logging.DEBUG | ||
| info = logging.INFO | ||
| warning = logging.WARNING | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(log_level: LogLevel = typer.Option("warning", enum_by_name=True)): | ||
| typer.echo(f"Log level set to: {logging.getLevelName(log_level.value)}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/test_tutorial/test_parameter_types/test_enum/test_tutorial005.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import importlib | ||
| import subprocess | ||
| import sys | ||
| from types import ModuleType | ||
|
|
||
| import pytest | ||
| from typer.testing import CliRunner | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| name="mod", | ||
| params=[ | ||
| pytest.param("tutorial005_py310"), | ||
| pytest.param("tutorial005_an_py310"), | ||
| ], | ||
| ) | ||
| def get_mod(request: pytest.FixtureRequest) -> ModuleType: | ||
| module_name = f"docs_src.parameter_types.enum.{request.param}" | ||
| mod = importlib.import_module(module_name) | ||
| return mod | ||
|
|
||
|
|
||
| def test_int_enum_default(mod: ModuleType): | ||
| result = runner.invoke(mod.app) | ||
| assert result.exit_code == 0 | ||
| assert "Access level: private (1)" in result.output | ||
|
|
||
|
|
||
| def test_int_enum(mod: ModuleType): | ||
| result = runner.invoke(mod.app, ["--access", "open"]) | ||
| assert result.exit_code == 0 | ||
| assert "Access level: open (4)" in result.output | ||
|
|
||
|
|
||
| def test_script(mod: ModuleType): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout |
57 changes: 57 additions & 0 deletions
57
tests/test_tutorial/test_parameter_types/test_enum/test_tutorial006.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import importlib | ||
| import subprocess | ||
| import sys | ||
| from types import ModuleType | ||
|
|
||
| import pytest | ||
| from typer.testing import CliRunner | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| name="mod", | ||
| params=[ | ||
| pytest.param("tutorial006_py310"), | ||
| pytest.param("tutorial006_an_py310"), | ||
| ], | ||
| ) | ||
| def get_mod(request: pytest.FixtureRequest) -> ModuleType: | ||
| module_name = f"docs_src.parameter_types.enum.{request.param}" | ||
| mod = importlib.import_module(module_name) | ||
| return mod | ||
|
|
||
|
|
||
| def test_help(mod: ModuleType): | ||
| result = runner.invoke(mod.app, ["--help"]) | ||
| assert result.exit_code == 0 | ||
| assert "--groceries" in result.output | ||
| assert "[f1|f2|f3]" in result.output | ||
| assert "default: f1, f3" in result.output | ||
|
|
||
|
|
||
| def test_call_no_arg(mod: ModuleType): | ||
| result = runner.invoke(mod.app) | ||
| assert result.exit_code == 0 | ||
| assert "Buying groceries: Eggs, Cheese" in result.output | ||
|
|
||
|
|
||
| def test_call_single_arg(mod: ModuleType): | ||
| result = runner.invoke(mod.app, ["--groceries", "f2"]) | ||
| assert result.exit_code == 0 | ||
| assert "Buying groceries: Bacon" in result.output | ||
|
|
||
|
|
||
| def test_call_multiple_arg(mod: ModuleType): | ||
| result = runner.invoke(mod.app, ["--groceries", "f1", "--groceries", "f2"]) | ||
| assert result.exit_code == 0 | ||
| assert "Buying groceries: Eggs, Bacon" in result.output | ||
|
|
||
|
|
||
| def test_script(mod: ModuleType): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout |
43 changes: 43 additions & 0 deletions
43
tests/test_tutorial/test_parameter_types/test_enum/test_tutorial007.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import importlib | ||
| import subprocess | ||
| import sys | ||
| from types import ModuleType | ||
|
|
||
| import pytest | ||
| from typer.testing import CliRunner | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| name="mod", | ||
| params=[ | ||
| pytest.param("tutorial007_py310"), | ||
| pytest.param("tutorial007_an_py310"), | ||
| ], | ||
| ) | ||
| def get_mod(request: pytest.FixtureRequest) -> ModuleType: | ||
| module_name = f"docs_src.parameter_types.enum.{request.param}" | ||
| mod = importlib.import_module(module_name) | ||
| return mod | ||
|
|
||
|
|
||
| def test_enum_names_default(mod: ModuleType): | ||
| result = runner.invoke(mod.app) | ||
| assert result.exit_code == 0 | ||
| assert "Log level set to: WARNING" in result.output | ||
|
|
||
|
|
||
| def test_enum_names(mod: ModuleType): | ||
| result = runner.invoke(mod.app, ["--log-level", "debug"]) | ||
| assert result.exit_code == 0 | ||
| assert "Log level set to: DEBUG" in result.output | ||
|
|
||
|
|
||
| def test_script(mod: ModuleType): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.