From 8a3fb970846de98b5800c30907b35d464becb0d2 Mon Sep 17 00:00:00 2001 From: Robert MacCloy Date: Wed, 30 Oct 2013 22:33:51 +0000 Subject: [PATCH 1/2] Suppress type comparison warning in RichEnum.lookup. A reasonably common pattern is a RichEnumValue that has an attriibute that's a list of some other RichEnumValue, usually related values. lookup() can then be used to search said attribute for a specific member. We always check up front to see if the object we're searching for and the attribute value in question are exactly matching; in this case that generates a type comparison warning that's spurious, since the next case (checking if the attribute value is an iterable and seeing if the object we're searching for is in it) does the correct thing. --- src/richenum/enums.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/richenum/enums.py b/src/richenum/enums.py index a01373e..57ccd43 100644 --- a/src/richenum/enums.py +++ b/src/richenum/enums.py @@ -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 From 490362e29484b83c91b166ca23b4817bacb7d1c5 Mon Sep 17 00:00:00 2001 From: Robert MacCloy Date: Tue, 5 Nov 2013 20:52:37 +0000 Subject: [PATCH 2/2] Update CHANGELOG and package version --- CHANGELOG.rst | 5 +++++ setup.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8e8f565..274707f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 diff --git a/setup.py b/setup.py index f13e581..2c129a2 100755 --- a/setup.py +++ b/setup.py @@ -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' +