Skip to content

Commit

Permalink
fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Apr 18, 2023
1 parent c7cecad commit 3736646
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ sphinxcontrib-htmlhelp==2.0.1; python_version >= "3.5"
sphinxcontrib-jsmath==1.0.1; python_version >= "3.5"
sphinxcontrib-qthelp==1.0.3; python_version >= "3.5"
sphinxcontrib-serializinghtml==1.1.5; python_version >= "3.5"
enum-properties==1.5.0
enum-properties==1.5.1
5 changes: 5 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Change Log
==========

v1.5.1
======

* Fixed `Symmetric string 'none' values enable coercion from None despite match_none=False <https://github.com/bckohan/enum-properties/issues/45>`_

v1.5.0
======

Expand Down
31 changes: 16 additions & 15 deletions enum_properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
cached_property = property # pylint: disable=C0103


VERSION = (1, 5, 0)
VERSION = (1, 5, 1)

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

for coerce_to in cls._ep_coerce_types_:
try:
val = coerce_to(value)
if value is not None:
for coerce_to in cls._ep_coerce_types_:
try:
return cls._value2member_map_[val]
except KeyError:
pass
val = coerce_to(value)
try:
return cls._value2member_map_[val]
except KeyError:
pass

try:
return cls._ep_symmetric_map_[val]
except KeyError:
pass
try:
return cls._ep_symmetric_map_[val]
except KeyError:
pass

if isinstance(val, str):
return cls._ep_isymmetric_map_[_do_casenorm(val)]
if isinstance(val, str):
return cls._ep_isymmetric_map_[_do_casenorm(val)]

except (KeyError, TypeError, ValueError):
pass
except (KeyError, TypeError, ValueError):
pass

return super()._missing_(value)

Expand Down
59 changes: 59 additions & 0 deletions enum_properties/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,65 @@ def test(self, count=2):
self.assertEqual(SpecializedEnum.THREE.test(), 'threethree')


class NoneCoercionTests(TestCase):

def test_string_to_none_coercion_disabled(self):

class EnumWithNones(EnumProperties, s('prop', match_none=True)):
VALUE1 = 1, None
VALUE2 = 2, 'label'

self.assertRaises(ValueError, EnumWithNones, 'None')

class EnumWithNones(
EnumProperties, s('prop', case_fold=True, match_none=False)
):
VALUE1 = 1, None
VALUE2 = 2, 'label'

self.assertRaises(ValueError, EnumWithNones, 'None')

class EnumWithNones(EnumProperties):
VALUE1 = None
VALUE2 = 'label'

self.assertRaises(ValueError, EnumWithNones, 'None')
self.assertEqual(EnumWithNones(None), EnumWithNones.VALUE1)

def test_none_to_string_coercion_disabled(self):

class EnumWithNones(EnumProperties, s('prop', match_none=True)):
VALUE1 = 1, 'None'
VALUE2 = 2, 'label'

self.assertRaises(ValueError, EnumWithNones, None)
self.assertEqual(EnumWithNones('None'), EnumWithNones.VALUE1)

class EnumWithNones(
EnumProperties, s('prop', case_fold=True, match_none=True)
):
VALUE1 = 1, 'None'
VALUE2 = 2, 'label'

self.assertRaises(ValueError, EnumWithNones, None)
self.assertEqual(EnumWithNones('none'), EnumWithNones.VALUE1)

class EnumWithNones(EnumProperties, s('prop', match_none=False)):
VALUE1 = 1, 'None'
VALUE2 = 2, 'label'

self.assertRaises(ValueError, EnumWithNones, None)
self.assertEqual(EnumWithNones('None'), EnumWithNones.VALUE1)

class EnumWithNones(EnumProperties):
VALUE1 = 'None'
VALUE2 = 'label'

self.assertRaises(ValueError, EnumWithNones, None)
self.assertRaises(KeyError, lambda x: EnumWithNones[x], None)
self.assertEqual(EnumWithNones('None'), EnumWithNones.VALUE1)


class PerformanceAndMemoryChecks(TestCase):

from enum_properties.tests.big_enum import ISOCountry
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "enum-properties"
version = "1.5.0"
version = "1.5.1"
description = "Add properties and method specializations to Python enumeration values with a simple declarative syntax."
authors = ["Brian Kohan <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 3736646

Please sign in to comment.