Skip to content

Commit

Permalink
Merge pull request #25 from rbm/warning-fix
Browse files Browse the repository at this point in the history
Suppress type comparison warning in RichEnum.lookup.
  • Loading branch information
Dale Hui committed Nov 5, 2013
2 parents 80bff47 + 490362e commit 84811af
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.0.2 (2013-11-05)
------------------
- Suppress warnings from mismatched type comparisons when generated
in RichEnum.lookup.

1.0.1 (2013-09-20)
------------------
- Raise warnings when comparing enum values to other types, but not
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='richenum',
version='1.0.1',
version='1.0.2',
description='Enum library for python.',
long_description=(
open('README.rst').read() + '\n\n' +
Expand Down
14 changes: 12 additions & 2 deletions src/richenum/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,18 @@ def members(cls):
def lookup(cls, field, value):
for member in cls:
member_value = getattr(member, field)
if member_value == value:
return member

# We filter warnings below; in the case where the field is a list
# of RichEnumValues and the value we're looking for is a RichEnum,
# this generates a warning due to mismatched type comparison even
# though the calling pattern is acceptable.
# n.b. There's likely a cleaner way to fix this but I was unsure if
# doing so would break the semantics of this method.
with warnings.catch_warnings():
warnings.simplefilter('ignore')
if member_value == value:
return member

if (
not isinstance(member_value, str) and
not isinstance(member_value, unicode) and
Expand Down

0 comments on commit 84811af

Please sign in to comment.