Skip to content

Commit 35a792d

Browse files
authored
Merge pull request #122 from loggi/fgm-support-contains
Membership test returning valid results for primitive values
2 parents 53d7f8b + 5a4a2b5 commit 35a792d

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed

README.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ Use ``.get(value, default=None)`` method to receive ``default`` if ``value`` is
234234

235235
.. code:: python
236236
237-
Colors.get(Colors.RED) == Colors.RED
238-
Colors.get('#f00') == Colors.RED
239-
Colors.get('undefined_color') is None
240-
Colors.get('undefined_color', Colors.RED) == Colors.RED
237+
assert Colors.get(Colors.RED) == Colors.RED
238+
assert Colors.get('#f00') == Colors.RED
239+
assert Colors.get('undefined_color') is None
240+
assert Colors.get('undefined_color', Colors.RED) == Colors.RED
241241
242242
Compatibility
243243
-------------
@@ -272,6 +272,15 @@ There's also optimistic casting of inner types:
272272
assert str(HttpStatuses.BAD_REQUEST) == "400"
273273
274274
275+
Check membership:
276+
277+
.. code:: python
278+
279+
assert HttpStatuses.OK in HttpStatuses
280+
assert 200 in HttpStatuses
281+
assert 999 not in HttpStatuses
282+
283+
275284
JSON
276285
....
277286

choicesenum/enums.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
# coding: utf-8
22
from __future__ import absolute_import, unicode_literals
33

4-
from enum import Enum
4+
import six
5+
from enum import Enum, EnumMeta
56

67

7-
class ChoicesEnum(Enum):
8+
class ChoicesMetaClass(EnumMeta):
9+
10+
def __contains__(cls, member):
11+
if not isinstance(member, cls):
12+
try:
13+
member = cls(member)
14+
except Exception:
15+
return False
16+
17+
return member._name_ in cls._member_map_
18+
19+
20+
class ChoicesEnum(six.with_metaclass(ChoicesMetaClass, Enum)):
821

922
def __new__(cls, value, display=None):
1023
obj = object.__new__(cls)
@@ -90,7 +103,7 @@ def __json__(self):
90103

91104
@property
92105
def display(self):
93-
return self._display_ if self._display_ is not None else\
106+
return self._display_ if self._display_ is not None else \
94107
self._name_.replace('_', ' ').capitalize()
95108

96109
@property

requirements_dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# test
22
-r requirements_test.txt
33
pytest-django==3.4.4
4-
Django==2.1.4
4+
Django==2.1.5
55
tox==3.6.1
66

77
# docs

requirements_test.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
mock==2.0.0
33
pytest==3.10.1
44
pytest-cov==2.6.0
5-
pytest-flake8==1.0.2
65
pytest-watch==4.2.0
76
pytest-sugar==0.9.2

tests/test_choicesenum.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,9 @@ def test_should_return_default_value(colors):
296296
assert colors.get('#f00') == colors.RED
297297
assert colors.get('undefined_color') is None
298298
assert colors.get('undefined_color', colors.RED) == colors.RED
299+
300+
301+
def test_should_support_membership_vertification(colors):
302+
assert colors.RED in colors
303+
assert colors.RED.value in colors
304+
assert 'non-existent-color' not in colors

tox.ini

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ envlist =
33
py{27}-django-{16,17,18,19,110,111}
44
py{34}-django-{110,111,20}
55
py{35,36,37}-django-{110,111,20,21}
6+
py36-flake8
67

78
[pytest]
89
filterwarnings =
@@ -36,11 +37,16 @@ deps =
3637
-r{toxinidir}/requirements_test.txt
3738
commands =
3839
pip install -U pip
39-
py.test --basetemp={envtmpdir} --flake8 --cov
40+
py.test --basetemp={envtmpdir} --cov
4041

4142
basepython =
4243
py37: python3.7
4344
py36: python3.6
4445
py35: python3.5
4546
py34: python3.4
4647
py27: python2.7
48+
49+
50+
[testenv:py36-flake8]
51+
commands = flake8 choicesenum/ tests/
52+
deps = flake8

0 commit comments

Comments
 (0)