summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-07-18 20:20:50 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-07-18 20:20:50 (GMT)
commit2489bd5d4e45b2a1d90f9336bf528ab1bba2d796 (patch)
tree807b68918b59cbadf99e71afa796ba6fda4c02cf
parentf12e3e2b695d56ca6ac8a4fe0a74016dacbab478 (diff)
parent3018cc49e88721ef50c80b2d832b25dd4f0fe9d7 (diff)
downloadcpython-2489bd5d4e45b2a1d90f9336bf528ab1bba2d796.zip
cpython-2489bd5d4e45b2a1d90f9336bf528ab1bba2d796.tar.gz
cpython-2489bd5d4e45b2a1d90f9336bf528ab1bba2d796.tar.bz2
Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.
-rw-r--r--Lib/inspect.py41
-rw-r--r--Lib/test/test_inspect.py78
-rw-r--r--Misc/NEWS2
3 files changed, 76 insertions, 45 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 45679cf..42f24cd 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2488,15 +2488,14 @@ class Parameter:
return hash((self.name, self.kind, self.annotation, self.default))
def __eq__(self, other):
- return (self is other or
- (issubclass(other.__class__, Parameter) and
- 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)
+ if self is other:
+ return True
+ 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)
class BoundArguments:
@@ -2610,13 +2609,12 @@ class BoundArguments:
self.arguments = OrderedDict(new_arguments)
def __eq__(self, other):
- return (self is other or
- (issubclass(other.__class__, BoundArguments) and
- self.signature == other.signature and
- self.arguments == other.arguments))
-
- def __ne__(self, other):
- return not self.__eq__(other)
+ if self is other:
+ return True
+ if not isinstance(other, BoundArguments):
+ return NotImplemented
+ return (self.signature == other.signature and
+ self.arguments == other.arguments)
def __setstate__(self, state):
self._signature = state['_signature']
@@ -2775,12 +2773,11 @@ class Signature:
return hash((params, kwo_params, return_annotation))
def __eq__(self, other):
- return (self is other or
- (isinstance(other, Signature) and
- self._hash_basis() == other._hash_basis()))
-
- def __ne__(self, other):
- return not self.__eq__(other)
+ if self is other:
+ return True
+ if not isinstance(other, Signature):
+ return NotImplemented
+ return self._hash_basis() == other._hash_basis()
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 a02f2e1..8e0e73c 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -89,6 +89,9 @@ def gen_coroutine_function_example(self):
yield
return 'spam'
+class EqualsToAll:
+ def __eq__(self, other):
+ return True
class TestPredicates(IsTestBase):
@@ -2672,69 +2675,84 @@ 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))
self.assertEqual(
hash(inspect.signature(foo)), hash(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))
self.assertNotEqual(
hash(inspect.signature(foo)), hash(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))
self.assertNotEqual(
hash(inspect.signature(foo)), hash(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))
self.assertNotEqual(
hash(inspect.signature(foo)), hash(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))
self.assertNotEqual(
hash(inspect.signature(foo)), hash(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))
self.assertNotEqual(
hash(inspect.signature(foo)), hash(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))
self.assertNotEqual(
hash(inspect.signature(spam)), hash(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))
self.assertEqual(
hash(inspect.signature(foo)), hash(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))
self.assertEqual(
hash(inspect.signature(foo)), hash(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))
self.assertEqual(
hash(inspect.signature(foo)), hash(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))
self.assertNotEqual(
hash(inspect.signature(foo)), hash(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))
self.assertEqual(
hash(inspect.signature(foo)), hash(inspect.signature(bar)))
@@ -2907,11 +2925,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_replace(self):
p = inspect.Parameter('foo', default=42,
@@ -3215,25 +3239,33 @@ 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)
def foo(*, a, b): pass
sig = inspect.signature(foo)
ba1 = sig.bind(a=1, b=2)
ba2 = sig.bind(b=2, a=1)
- self.assertEqual(ba1, ba2)
+ self.assertTrue(ba1 == ba2)
+ self.assertFalse(ba1 != ba2)
def test_signature_bound_arguments_pickle(self):
def foo(a, b, *, c:1={}, **kw) -> {42:'ham'}: pass
diff --git a/Misc/NEWS b/Misc/NEWS
index ba01a06..b400a91 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,8 @@ Core and Builtins
Library
-------
+- Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.
+
- Issue #24631: Fixed regression in the timeit module with multiline setup.
- Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.