-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from mpkocher/add-enum
Version 3.4.0. Improve support for simple Enums.
- Loading branch information
Showing
6 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "3.3.0" | ||
__version__ = "3.4.0" |
This file contains 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,37 @@ | ||
from enum import Enum, auto | ||
from typing import Set | ||
from pydantic import BaseModel | ||
|
||
from pydantic_cli import run_and_exit | ||
|
||
|
||
class Mode(str, Enum): | ||
"""Note that if you use `auto`, you must specify the enum | ||
using the int value (as a string). | ||
""" | ||
|
||
alpha = auto() | ||
beta = auto() | ||
|
||
|
||
class State(str, Enum): | ||
"""Note, this is case sensitive when providing it from the commandline""" | ||
|
||
RUNNING = "RUNNING" | ||
FAILED = "FAILED" | ||
SUCCESSFUL = "SUCCESSFUL" | ||
|
||
|
||
class Options(BaseModel): | ||
states: Set[State] | ||
mode: Mode | ||
max_records: int = 100 | ||
|
||
|
||
def example_runner(opts: Options) -> int: | ||
print(f"Mock example running with {opts}") | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
run_and_exit(Options, example_runner, description=__doc__, version="0.1.0") |
This file contains 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 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 . import _TestHarness, HarnessConfig | ||
|
||
from pydantic_cli.examples.simple_with_enum import Options, example_runner | ||
|
||
|
||
class TestExamples(_TestHarness[Options]): | ||
|
||
CONFIG = HarnessConfig(Options, example_runner) | ||
|
||
def test_simple_01(self): | ||
args = ["--states", "RUNNING", "FAILED", "--max_records", "1234", "--mode", "1"] | ||
self.run_config(args) | ||
|
||
def test_bad_enum_value(self): | ||
args = [ | ||
"--states", | ||
"RUNNING", | ||
"BAD_STATE", | ||
"--max_records", | ||
"1234", | ||
"--mode", | ||
"1", | ||
] | ||
self.run_config(args, exit_code=2) |