Skip to content

Commit b572358

Browse files
authored
Merge pull request #18 from mreiche/bugfix/enum-type
Fix mapping enum types
2 parents d13c0ac + 0ff61f8 commit b572358

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

tests/test_opt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from enum import Enum
2+
13
import pytest
24

35
from test_streams import Node
@@ -169,3 +171,11 @@ def test_stream():
169171
def test_dict_map_kwargs():
170172
opt = Opt({"name": "First"})
171173
assert opt.map_kwargs(Node).get().name == "First"
174+
175+
176+
def test_map_enum():
177+
class AnalysisAnalysisState(str, Enum):
178+
NOT_SET = "NOT_SET"
179+
RESOLVED = "RESOLVED"
180+
181+
assert Opt(AnalysisAnalysisState.RESOLVED).map_key("value").get() == "RESOLVED"

tinystream.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import enum
12
import functools
23
import itertools
34
from typing import Iterable, TypeVar, Callable, List, Dict, Tuple, Iterator, Generic, Type
@@ -18,22 +19,27 @@
1819

1920

2021
def _key_exists(x: any, key: Key, invert: bool = False):
22+
def _hasattr():
23+
if invert:
24+
return not hasattr(x, key)
25+
else:
26+
return hasattr(x, key)
27+
2128
if isinstance(x, (list, tuple)):
2229
size = len(x)
2330
if invert:
2431
return key >= size
2532
else:
2633
return key < size
34+
elif isinstance(x, enum.Enum):
35+
return _hasattr()
2736
elif isinstance(x, (dict, Iterable)):
2837
if invert:
2938
return key not in x
3039
else:
3140
return key in x
3241
else:
33-
if invert:
34-
return not hasattr(x, key)
35-
else:
36-
return hasattr(x, key)
42+
return _hasattr()
3743

3844

3945
def _get_key_value(x: any, key: Key) -> any:

0 commit comments

Comments
 (0)