Skip to content

Commit 3736646

Browse files
committed
fixes #45
1 parent c7cecad commit 3736646

File tree

5 files changed

+82
-17
lines changed

5 files changed

+82
-17
lines changed

doc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sphinxcontrib-htmlhelp==2.0.1; python_version >= "3.5"
66
sphinxcontrib-jsmath==1.0.1; python_version >= "3.5"
77
sphinxcontrib-qthelp==1.0.3; python_version >= "3.5"
88
sphinxcontrib-serializinghtml==1.1.5; python_version >= "3.5"
9-
enum-properties==1.5.0
9+
enum-properties==1.5.1

doc/source/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Change Log
33
==========
44

5+
v1.5.1
6+
======
7+
8+
* Fixed `Symmetric string 'none' values enable coercion from None despite match_none=False <https://github.com/bckohan/enum-properties/issues/45>`_
9+
510
v1.5.0
611
======
712

enum_properties/__init__.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
cached_property = property # pylint: disable=C0103
3232

3333

34-
VERSION = (1, 5, 0)
34+
VERSION = (1, 5, 1)
3535

3636
__title__ = 'Enum Properties'
3737
__version__ = '.'.join(str(i) for i in VERSION)
@@ -218,24 +218,25 @@ def _missing_(cls, value): # pylint: disable=R0911
218218
except KeyError:
219219
pass
220220

221-
for coerce_to in cls._ep_coerce_types_:
222-
try:
223-
val = coerce_to(value)
221+
if value is not None:
222+
for coerce_to in cls._ep_coerce_types_:
224223
try:
225-
return cls._value2member_map_[val]
226-
except KeyError:
227-
pass
224+
val = coerce_to(value)
225+
try:
226+
return cls._value2member_map_[val]
227+
except KeyError:
228+
pass
228229

229-
try:
230-
return cls._ep_symmetric_map_[val]
231-
except KeyError:
232-
pass
230+
try:
231+
return cls._ep_symmetric_map_[val]
232+
except KeyError:
233+
pass
233234

234-
if isinstance(val, str):
235-
return cls._ep_isymmetric_map_[_do_casenorm(val)]
235+
if isinstance(val, str):
236+
return cls._ep_isymmetric_map_[_do_casenorm(val)]
236237

237-
except (KeyError, TypeError, ValueError):
238-
pass
238+
except (KeyError, TypeError, ValueError):
239+
pass
239240

240241
return super()._missing_(value)
241242

enum_properties/tests/tests.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,65 @@ def test(self, count=2):
19331933
self.assertEqual(SpecializedEnum.THREE.test(), 'threethree')
19341934

19351935

1936+
class NoneCoercionTests(TestCase):
1937+
1938+
def test_string_to_none_coercion_disabled(self):
1939+
1940+
class EnumWithNones(EnumProperties, s('prop', match_none=True)):
1941+
VALUE1 = 1, None
1942+
VALUE2 = 2, 'label'
1943+
1944+
self.assertRaises(ValueError, EnumWithNones, 'None')
1945+
1946+
class EnumWithNones(
1947+
EnumProperties, s('prop', case_fold=True, match_none=False)
1948+
):
1949+
VALUE1 = 1, None
1950+
VALUE2 = 2, 'label'
1951+
1952+
self.assertRaises(ValueError, EnumWithNones, 'None')
1953+
1954+
class EnumWithNones(EnumProperties):
1955+
VALUE1 = None
1956+
VALUE2 = 'label'
1957+
1958+
self.assertRaises(ValueError, EnumWithNones, 'None')
1959+
self.assertEqual(EnumWithNones(None), EnumWithNones.VALUE1)
1960+
1961+
def test_none_to_string_coercion_disabled(self):
1962+
1963+
class EnumWithNones(EnumProperties, s('prop', match_none=True)):
1964+
VALUE1 = 1, 'None'
1965+
VALUE2 = 2, 'label'
1966+
1967+
self.assertRaises(ValueError, EnumWithNones, None)
1968+
self.assertEqual(EnumWithNones('None'), EnumWithNones.VALUE1)
1969+
1970+
class EnumWithNones(
1971+
EnumProperties, s('prop', case_fold=True, match_none=True)
1972+
):
1973+
VALUE1 = 1, 'None'
1974+
VALUE2 = 2, 'label'
1975+
1976+
self.assertRaises(ValueError, EnumWithNones, None)
1977+
self.assertEqual(EnumWithNones('none'), EnumWithNones.VALUE1)
1978+
1979+
class EnumWithNones(EnumProperties, s('prop', match_none=False)):
1980+
VALUE1 = 1, 'None'
1981+
VALUE2 = 2, 'label'
1982+
1983+
self.assertRaises(ValueError, EnumWithNones, None)
1984+
self.assertEqual(EnumWithNones('None'), EnumWithNones.VALUE1)
1985+
1986+
class EnumWithNones(EnumProperties):
1987+
VALUE1 = 'None'
1988+
VALUE2 = 'label'
1989+
1990+
self.assertRaises(ValueError, EnumWithNones, None)
1991+
self.assertRaises(KeyError, lambda x: EnumWithNones[x], None)
1992+
self.assertEqual(EnumWithNones('None'), EnumWithNones.VALUE1)
1993+
1994+
19361995
class PerformanceAndMemoryChecks(TestCase):
19371996

19381997
from enum_properties.tests.big_enum import ISOCountry

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "enum-properties"
3-
version = "1.5.0"
3+
version = "1.5.1"
44
description = "Add properties and method specializations to Python enumeration values with a simple declarative syntax."
55
authors = ["Brian Kohan <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)