From eb36e75b89c025124b169a4eabc7e613125f0988 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 18 Jun 2013 14:16:06 +0100 Subject: [PATCH] Fixed comparison of `Range` with non-range objects Fixes ticket #164. Patch from Chris Withers on master. --- NEWS | 2 ++ lib/_range.py | 2 ++ tests/test_types_extras.py | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/NEWS b/NEWS index a2b94ce0..e2f6b7fe 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,8 @@ What's new in psycopg 2.5.1 - Fixed build on Solaris 10 and 11 where the round() function is already declared (:ticket:`#146`). +- Fixed comparison of `Range` with non-range objects (:ticket:`#164`). + Thanks to Chris Withers for the patch. What's new in psycopg 2.5 diff --git a/lib/_range.py b/lib/_range.py index 306f1f73..0f159908 100644 --- a/lib/_range.py +++ b/lib/_range.py @@ -121,6 +121,8 @@ class Range(object): return self._bounds is not None def __eq__(self, other): + if not isinstance(other, Range): + return False return (self._lower == other._lower and self._upper == other._upper and self._bounds == other._bounds) diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 98fbbadf..96ffcd3c 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -1212,6 +1212,19 @@ class RangeTestCase(unittest.TestCase): assert_not_equal(Range(10, 20), Range(11, 20)) assert_not_equal(Range(10, 20, '[)'), Range(10, 20, '[]')) + def test_eq_wrong_type(self): + from psycopg2.extras import Range + self.assertNotEqual(Range(10, 20), ()) + + def test_eq_subclass(self): + from psycopg2.extras import Range, NumericRange + + class IntRange(NumericRange): pass + class PositiveIntRange(IntRange): pass + + self.assertEqual(Range(10, 20), IntRange(10, 20)) + self.assertEqual(PositiveIntRange(10, 20), IntRange(10, 20)) + def test_not_ordered(self): from psycopg2.extras import Range self.assertRaises(TypeError, lambda: Range(empty=True) < Range(0,4))