Skip to content

Commit b3862c9

Browse files
committed
refact: use enums
1 parent 9d9e67b commit b3862c9

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/api/options.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
# ----------------------------------------------------------------------
1111

1212
import json
13+
import enum
1314

1415
from typing import Dict
1516
from typing import List
1617
from typing import Any
1718

18-
from .errors import Error
19+
from src.api.errors import Error
1920

2021
__all__ = ['Option', 'Options', 'ANYTYPE', 'Action']
2122

@@ -161,13 +162,16 @@ def pop(self) -> Any:
161162
# ----------------------------------------------------------------------
162163
# Options commands
163164
# ----------------------------------------------------------------------
164-
class Action:
165+
@enum.unique
166+
class Action(str, enum.Enum):
165167
ADD = 'add'
166168
ADD_IF_NOT_DEFINED = 'add_if_not_defined'
167169
CLEAR = 'clear'
168170
LIST = 'list'
169171

170-
allowed = {ADD, CLEAR, LIST, ADD_IF_NOT_DEFINED}
172+
@classmethod
173+
def valid(cls, action: str) -> bool:
174+
return action in list(cls)
171175

172176

173177
# ----------------------------------------------------------------------
@@ -258,9 +262,9 @@ def check_allowed_args(action: str, kwargs_, allowed_args, required_args=None):
258262
if not args or args == (Action.LIST,):
259263
return {x: y for x, y in self._options.items()}
260264

261-
assert args, f"Missing one action of {', '.join(Action.allowed)}"
262-
assert len(args) == 1 and args[0] in Action.allowed, \
263-
f"Only one action of {', '.join(Action.allowed)} can be specified"
265+
assert args, f"Missing one action of {', '.join(Action)}"
266+
assert len(args) == 1 and Action.valid(args[0]), \
267+
f"Only one action of {', '.join(Action)} can be specified"
264268

265269
# clear
266270
if args[0] == Action.CLEAR:
@@ -282,6 +286,7 @@ def check_allowed_args(action: str, kwargs_, allowed_args, required_args=None):
282286
kwargs['type_'] = kwargs['type']
283287
del kwargs['type']
284288
self.__add_option(**kwargs)
289+
return
285290

286291
if args[0] == Action.ADD_IF_NOT_DEFINED:
287292
kwargs['type'] = kwargs.get('type')

0 commit comments

Comments
 (0)