Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.

This commit is contained in:
Serhiy Storchaka 2015-07-18 23:21:16 +03:00
commit 44a5cd654d
3 changed files with 76 additions and 45 deletions

View File

@ -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."""

View File

@ -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

View File

@ -41,6 +41,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 #24608: chunk.Chunk.read() now always returns bytes, not str.