Skip to content

Commit 075eaf0

Browse files
author
Adam DePue
committed
Merge pull request #39 from rbm/convenient-exceptions
Add LookupError members to RichEnums to simplify catching lookup errors, and bump version to 1.2
2 parents 3a22cde + f58a90e commit 075eaf0

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
1.2.0 (2016-04-15)
5+
------------------
6+
- added simple ``LookupError`` members that are thrown when
7+
``RichEnum.lookup`` is called for a nonexistent attr/val pair.
8+
Users can choose to catch either the specific ``LookupError`` or
9+
continue to catch ``EnumLookupError``.
10+
411
1.1.0 (2014-04-17)
512
------------------
613
- support for Python 3 and PyPy

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setup(
1515
name='richenum',
16-
version='1.1.2',
16+
version='1.2.0',
1717
description='Enum library for python.',
1818
long_description=(
1919
open('README.rst').read() + '\n\n' +

src/richenum/enums.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def __new__(cls, cls_name, cls_parents, cls_attrs):
231231
members = _setup_members(cls_attrs, cls_parents, RichEnumValue)
232232
# Use tuple when possible when setting internal attributes to prevent modification
233233
cls_attrs['_MEMBERS'] = tuple(members)
234+
cls_attrs['LookupError'] = type('LookupError', (EnumLookupError,), {})
234235
return super(_RichEnumMetaclass, cls).__new__(cls, cls_name, cls_parents, cls_attrs)
235236

236237

@@ -241,6 +242,7 @@ def __new__(cls, cls_name, cls_parents, cls_attrs):
241242

242243
# Use tuple when possible when setting internal attributes to prevent modification
243244
cls_attrs['_MEMBERS'] = tuple(members)
245+
cls_attrs['LookupError'] = type('LookupError', (EnumLookupError,), {})
244246

245247
# we want to validate that there are not two items at the same index, so lets do that here
246248
seen = set()
@@ -278,7 +280,7 @@ def lookup(cls, field, value):
278280
value in member_value
279281
):
280282
return member
281-
raise EnumLookupError('Could not find member matching %s = %s in enum %s'
283+
raise cls.LookupError('Could not find member matching %s = %s in enum %s' # pylint: disable=no-member
282284
% (field, value, cls)
283285
)
284286

tests/richenum/test_rich_enums.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# -*- coding: utf-8 -*-
23
# pylint: disable=E1101
34

@@ -147,3 +148,16 @@ def test_unicode_handling(self):
147148
self.assertEqual(str(poop_okra), "Okra💩")
148149
if not PY3:
149150
self.assertEqual(unicode(poop_okra), u"Okra💩")
151+
152+
def test_specific_lookup_error_is_caught(self):
153+
with self.assertRaises(Vegetable.LookupError):
154+
Vegetable.lookup('canonical_name', 'meat')
155+
156+
def test_other_specific_lookup_error_is_not_caught(self):
157+
class Meat(RichEnum):
158+
COW = RichEnumValue("cow", "Cow")
159+
160+
with self.assertRaises(EnumLookupError) as cm:
161+
Vegetable.lookup('canonical_name', 'meat')
162+
163+
self.assertNotIsInstance(cm.exception, Meat.LookupError)

0 commit comments

Comments
 (0)