summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-07-18 20:19:05 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-07-18 20:19:05 (GMT)
commit3018cc49e88721ef50c80b2d832b25dd4f0fe9d7 (patch)
treea889b4c4d7bfde2e1802643f05177ba8674a8dc8
parentd113c967b4810b504d97ec97459dc85bd3a3facd (diff)
downloadcpython-3018cc49e88721ef50c80b2d832b25dd4f0fe9d7.zip
cpython-3018cc49e88721ef50c80b2d832b25dd4f0fe9d7.tar.gz
cpython-3018cc49e88721ef50c80b2d832b25dd4f0fe9d7.tar.bz2
Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.
-rw-r--r--Lib/inspect.py26
-rw-r--r--Lib/test/test_inspect.py76
-rw-r--r--Misc/NEWS2
3 files changed, 66 insertions, 38 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 49e66ce..4298de6 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2206,15 +2206,13 @@ class Parameter:
id(self), self.name)
def __eq__(self, other):
- return (issubclass(other.__class__, Parameter) and
- self._name == other._name and
+ if not isinstance(other, Parameter):
+ return NotImplemented
+ return (self._name == other._name and
self._kind == other._kind and
self._default == other._default and
self._annotation == other._annotation)
- def __ne__(self, other):
- return not self.__eq__(other)
-
class BoundArguments:
'''Result of `Signature.bind` call. Holds the mapping of arguments
@@ -2295,13 +2293,11 @@ class BoundArguments:
return kwargs
def __eq__(self, other):
- return (issubclass(other.__class__, BoundArguments) and
- self.signature == other.signature and
+ if not isinstance(other, BoundArguments):
+ return NotImplemented
+ return (self.signature == other.signature and
self.arguments == other.arguments)
- def __ne__(self, other):
- return not self.__eq__(other)
-
class Signature:
'''A Signature object represents the overall signature of a function.
@@ -2493,9 +2489,10 @@ class Signature:
return_annotation=return_annotation)
def __eq__(self, other):
- if (not issubclass(type(other), Signature) or
- self.return_annotation != other.return_annotation or
- len(self.parameters) != len(other.parameters)):
+ if not isinstance(other, Signature):
+ return NotImplemented
+ if (self.return_annotation != other.return_annotation or
+ len(self.parameters) != len(other.parameters)):
return False
other_positions = {param: idx
@@ -2522,9 +2519,6 @@ class Signature:
return True
- def __ne__(self, other):
- return not self.__eq__(other)
-
def _bind(self, args, kwargs, *, partial=False):
'''Private method. Don't use directly.'''
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 58a6cd2..1a124b5 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -76,6 +76,10 @@ def generator_function_example(self):
for i in range(2):
yield i
+class EqualsToAll:
+ def __eq__(self, other):
+ return True
+
class TestPredicates(IsTestBase):
def test_sixteen(self):
count = len([x for x in dir(inspect) if x.startswith('is')])
@@ -2459,47 +2463,62 @@ class TestSignatureObject(unittest.TestCase):
def test_signature_equality(self):
def foo(a, *, b:int) -> float: pass
- self.assertNotEqual(inspect.signature(foo), 42)
+ self.assertFalse(inspect.signature(foo) == 42)
+ self.assertTrue(inspect.signature(foo) != 42)
+ self.assertTrue(inspect.signature(foo) == EqualsToAll())
+ self.assertFalse(inspect.signature(foo) != EqualsToAll())
def bar(a, *, b:int) -> float: pass
- self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) == inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) != inspect.signature(bar))
def bar(a, *, b:int) -> int: pass
- self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) == inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) != inspect.signature(bar))
def bar(a, *, b:int): pass
- self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) == inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) != inspect.signature(bar))
def bar(a, *, b:int=42) -> float: pass
- self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) == inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) != inspect.signature(bar))
def bar(a, *, c) -> float: pass
- self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) == inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) != inspect.signature(bar))
def bar(a, b:int) -> float: pass
- self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) == inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) != inspect.signature(bar))
def spam(b:int, a) -> float: pass
- self.assertNotEqual(inspect.signature(spam), inspect.signature(bar))
+ self.assertFalse(inspect.signature(spam) == inspect.signature(bar))
+ self.assertTrue(inspect.signature(spam) != inspect.signature(bar))
def foo(*, a, b, c): pass
def bar(*, c, b, a): pass
- self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) == inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) != inspect.signature(bar))
def foo(*, a=1, b, c): pass
def bar(*, c, b, a=1): pass
- self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) == inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) != inspect.signature(bar))
def foo(pos, *, a=1, b, c): pass
def bar(pos, *, c, b, a=1): pass
- self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) == inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) != inspect.signature(bar))
def foo(pos, *, a, b, c): pass
def bar(pos, *, c, b, a=1): pass
- self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) == inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) != inspect.signature(bar))
def foo(pos, *args, a=42, b, c, **kwargs:int): pass
def bar(pos, *args, c, b, a=42, **kwargs:int): pass
- self.assertEqual(inspect.signature(foo), inspect.signature(bar))
+ self.assertTrue(inspect.signature(foo) == inspect.signature(bar))
+ self.assertFalse(inspect.signature(foo) != inspect.signature(bar))
def test_signature_unhashable(self):
def foo(a): pass
@@ -2629,11 +2648,17 @@ class TestParameterObject(unittest.TestCase):
P = inspect.Parameter
p = P('foo', default=42, kind=inspect.Parameter.KEYWORD_ONLY)
- self.assertEqual(p, p)
- self.assertNotEqual(p, 42)
+ self.assertTrue(p == p)
+ self.assertFalse(p != p)
+ self.assertFalse(p == 42)
+ self.assertTrue(p != 42)
+ self.assertTrue(p == EqualsToAll())
+ self.assertFalse(p != EqualsToAll())
- self.assertEqual(p, P('foo', default=42,
- kind=inspect.Parameter.KEYWORD_ONLY))
+ self.assertTrue(p == P('foo', default=42,
+ kind=inspect.Parameter.KEYWORD_ONLY))
+ self.assertFalse(p != P('foo', default=42,
+ kind=inspect.Parameter.KEYWORD_ONLY))
def test_signature_parameter_unhashable(self):
p = inspect.Parameter('foo', default=42,
@@ -2936,19 +2961,26 @@ class TestBoundArguments(unittest.TestCase):
def test_signature_bound_arguments_equality(self):
def foo(a): pass
ba = inspect.signature(foo).bind(1)
- self.assertEqual(ba, ba)
+ self.assertTrue(ba == ba)
+ self.assertFalse(ba != ba)
+ self.assertTrue(ba == EqualsToAll())
+ self.assertFalse(ba != EqualsToAll())
ba2 = inspect.signature(foo).bind(1)
- self.assertEqual(ba, ba2)
+ self.assertTrue(ba == ba2)
+ self.assertFalse(ba != ba2)
ba3 = inspect.signature(foo).bind(2)
- self.assertNotEqual(ba, ba3)
+ self.assertFalse(ba == ba3)
+ self.assertTrue(ba != ba3)
ba3.arguments['a'] = 1
- self.assertEqual(ba, ba3)
+ self.assertTrue(ba == ba3)
+ self.assertFalse(ba != ba3)
def bar(b): pass
ba4 = inspect.signature(bar).bind(1)
- self.assertNotEqual(ba, ba4)
+ self.assertFalse(ba == ba4)
+ self.assertTrue(ba != ba4)
class TestSignaturePrivateHelpers(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
index 2c669f3..ad5fef3 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,8 @@ Core and Builtins
Library
-------
+- Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.
+
- Issue #21750: mock_open.read_data can now be read from each instance, as it
could in Python 3.3.