Skip to content

Commit ed7ca1e

Browse files
committed
Support enums
1 parent c3dbf72 commit ed7ca1e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

hatch_build/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from argparse import ArgumentParser
2+
from enum import Enum
23
from logging import Formatter, StreamHandler, getLogger
34
from pathlib import Path
45
from typing import TYPE_CHECKING, Callable, Dict, List, Literal, Optional, Tuple, Type, Union, get_args, get_origin
@@ -98,6 +99,11 @@ def _recurse_add_fields(parser: ArgumentParser, model: Union["BaseModel", Type["
9899
except TypeError:
99100
# TODO: handle more complex types if needed
100101
parser.add_argument(arg_name, type=str, default=default_value)
102+
elif isinstance(field_type, type) and issubclass(field_type, Enum):
103+
#############
104+
# MARK: Enum
105+
enum_choices = [e.value for e in field_type]
106+
parser.add_argument(arg_name, type=type(enum_choices[0]), choices=enum_choices, default=default_value)
101107
elif isinstance(field_type, type) and issubclass(field_type, Path):
102108
#############
103109
# MARK: Path

hatch_build/tests/test_cli_model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from enum import Enum
23
from pathlib import Path
34
from typing import Dict, List, Literal, Optional
45
from unittest.mock import patch
@@ -8,6 +9,11 @@
89
from hatch_build.cli import hatchling, parse_extra_args_model
910

1011

12+
class MyEnum(Enum):
13+
OPTION_A = "option_a"
14+
OPTION_B = "option_b"
15+
16+
1117
class SubModel(BaseModel, validate_assignment=True):
1218
sub_arg: int = 42
1319
sub_arg_with_value: str = "sub_default"
@@ -19,6 +25,7 @@ class MyTopLevelModel(BaseModel, validate_assignment=True):
1925
extra_arg_with_value_equals: Optional[str] = "default_equals"
2026
extra_arg_literal: Literal["a", "b", "c"] = "a"
2127

28+
enum_arg: MyEnum = MyEnum.OPTION_A
2229
list_arg: List[int] = [1, 2, 3]
2330
dict_arg: Dict[str, str] = {}
2431
dict_arg_default_values: Dict[str, str] = {"existing-key": "existing-value"}
@@ -33,6 +40,8 @@ class MyTopLevelModel(BaseModel, validate_assignment=True):
3340
submodel_dict: Dict[str, SubModel] = {}
3441
submodel_dict_instanced: Dict[str, SubModel] = {"a": SubModel()}
3542

43+
unsupported_literal: Literal[b"test"] = b"test"
44+
3645

3746
class TestCLIMdel:
3847
def test_get_arg_from_model(self):
@@ -49,6 +58,8 @@ def test_get_arg_from_model(self):
4958
"--extra-arg-not-in-parser",
5059
"--extra-arg-literal",
5160
"b",
61+
"--enum-arg",
62+
"option_b",
5263
"--list-arg",
5364
"1,2,3",
5465
"--dict-arg",
@@ -80,6 +91,7 @@ def test_get_arg_from_model(self):
8091
assert model.extra_arg_with_value == "value"
8192
assert model.extra_arg_with_value_equals == "value2"
8293
assert model.extra_arg_literal == "b"
94+
assert model.enum_arg == MyEnum.OPTION_B
8395
assert model.list_arg == [1, 2, 3]
8496
assert model.dict_arg == {"key1": "value1", "key2": "value2"}
8597
assert model.dict_arg_default_values == {"existing-key": "new-value"}

0 commit comments

Comments
 (0)