Remove strict from is_same_callback()

The strict argument has been deprecated since 4.6.0 because it has no
effect in Python >=3.8 [1].

Because now this library supports only Python >=3.8, the logic for old
python versions can be removed.

[1] https://review.opendev.org/c/openstack/oslo.utils/+/750216

Related-Bug: #1841072
Change-Id: I5873c0df347a4e9a8a1fbfcf9f1212af14f7aef2
This commit is contained in:
Takashi Kajinami 2022-07-18 22:18:27 +09:00
parent 067a0dbd10
commit 2319e6d328
3 changed files with 10 additions and 43 deletions

View File

@ -160,13 +160,8 @@ def get_method_self(method):
return None return None
def is_same_callback(callback1, callback2, strict=True): def is_same_callback(callback1, callback2):
"""Returns if the two callbacks are the same. """Returns if the two callbacks are the same."""
'strict' arg has no meaning for python 3.8 onwards and will
always return the equality of both callback based on 'self'
comparison only.
"""
if callback1 is callback2: if callback1 is callback2:
# This happens when plain methods are given (or static/non-bound # This happens when plain methods are given (or static/non-bound
# methods). # methods).
@ -175,31 +170,8 @@ def is_same_callback(callback1, callback2, strict=True):
# NOTE(gmann): python3.8 onward, comparison of bound methods is # NOTE(gmann): python3.8 onward, comparison of bound methods is
# changed. It no longer decide the bound method's equality based # changed. It no longer decide the bound method's equality based
# on their bounded objects equality instead it checks the identity # on their bounded objects equality instead it checks the identity
# of their '__self__'. So python3.8 onward, two different bound # of their '__self__'.
# methods are no longer equal even __eq__ method return True. return True
# Or in other term, 'strict' arg has no meaning from python 3.8
# onwards above if condition never satisfy if both callback are
# bounded to two different objects.
# For backward compatibility for python <3.8, we can keep the 'strict'
# arg and the below code of comparing 'self' and once minimum
# supported python version is 3.8 we can remove both because python
# 3.8 onward == operator itself checks identity of 'self'.
# Ref bug: https://bugs.launchpad.net/oslo.utils/+bug/1841072
if not strict:
LOG.warning('"strict" arg is deprecated because it no '
'longer work for python 3.8 onwards')
return True
# Until python 3.7, two bound methods are equal if functions
# themselves are equal and objects they are applied to are equal.
# This means that a bound method could be the same bound method on
# another object if the objects have __eq__ methods that return true
# (when in fact it is a different bound method). Python u so crazy!
try:
self1 = operator.attrgetter("__self__")(callback1)
self2 = operator.attrgetter("__self__")(callback2)
return self1 is self2
except AttributeError: # nosec
pass
return False return False

View File

@ -15,7 +15,6 @@
# under the License. # under the License.
import functools import functools
import sys
from oslotest import base as test_base from oslotest import base as test_base
@ -151,16 +150,7 @@ class CallbackEqualityTest(test_base.BaseTestCase):
c = A() c = A()
self.assertFalse(reflection.is_same_callback(b.b, c.b)) self.assertFalse(reflection.is_same_callback(b.b, c.b))
# NOTE(gmann): python3.8 onwards, comparision of bound methods is self.assertTrue(reflection.is_same_callback(b.b, b.b))
# changed and 'strict' arg has no meaning.
# Ref bug: https://bugs.launchpad.net/oslo.utils/+bug/1841072
if sys.version_info < (3, 8):
self.assertTrue(reflection.is_same_callback(b.b, c.b,
strict=False))
else:
self.assertFalse(reflection.is_same_callback(b.b, c.b,
strict=False))
self.assertTrue(reflection.is_same_callback(b.b, b.b))
class BoundMethodTest(test_base.BaseTestCase): class BoundMethodTest(test_base.BaseTestCase):

View File

@ -0,0 +1,5 @@
---
upgrade:
- |
The ``strict`` argument has been removed from the ``is_same_callback``
function. The argument has had no effect in Python 3.8.