-
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 #50 from Julian-Brendel/feature/enum-by-name
Add support for Enum by name
- Loading branch information
Showing
8 changed files
with
98 additions
and
14 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__ = "4.2.1" | ||
__version__ = "4.3.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,54 @@ | ||
from enum import Enum, auto, IntEnum | ||
from typing import Set | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from pydantic_cli import run_and_exit | ||
|
||
|
||
class CastAbleEnum(Enum): | ||
"""Example enum mixin that will cast enum from case-insensitive name""" | ||
|
||
@classmethod | ||
def __get_validators__(cls): | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate(cls, v): | ||
try: | ||
lookup = {k.lower(): item.value for k, item in cls.__members__.items()} | ||
return lookup[v.lower()] | ||
except KeyError: | ||
raise ValueError(f"Invalid value {v}. {cls.cli_help()}") | ||
|
||
@classmethod | ||
def cli_help(cls) -> str: | ||
return f"Allowed={list(cls.__members__.keys())}" | ||
|
||
|
||
class Mode(CastAbleEnum, IntEnum): | ||
alpha = auto() | ||
beta = auto() | ||
|
||
|
||
class State(CastAbleEnum, str, Enum): | ||
RUNNING = "RUNNING" | ||
FAILED = "FAILED" | ||
SUCCESSFUL = "SUCCESSFUL" | ||
|
||
|
||
class Options(BaseModel): | ||
states: Set[State] = Field( | ||
..., description=f"States to filter on. {State.cli_help()}" | ||
) | ||
mode: Mode = Field(..., description=f"Processing Mode to select. {Mode.cli_help()}") | ||
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
34 changes: 34 additions & 0 deletions
34
pydantic_cli/tests/test_examples_simple_with_enum_by_name.py
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,34 @@ | ||
from . import _TestHarness, HarnessConfig | ||
|
||
from pydantic_cli.examples.simple_with_enum_by_name import Options, example_runner | ||
|
||
|
||
class TestExamples(_TestHarness[Options]): | ||
|
||
CONFIG = HarnessConfig(Options, example_runner) | ||
|
||
def test_simple_01(self): | ||
args = ["--states", "RUNNING", "FAILED", "--mode", "alpha"] | ||
self.run_config(args) | ||
|
||
def test_case_insensitive(self): | ||
args = ["--states", "successful", "failed", "--mode", "ALPHA"] | ||
self.run_config(args) | ||
|
||
def test_bad_enum_by_value(self): | ||
args = [ | ||
"--states", | ||
"RUNNING", | ||
"--mode", | ||
"1", | ||
] | ||
self.run_config(args, exit_code=1) | ||
|
||
def test_bad_enum_value(self): | ||
args = [ | ||
"--states", | ||
"RUNNING", | ||
"--mode", | ||
"DRAGON", | ||
] | ||
self.run_config(args, exit_code=1) |
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