Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions qiskit/utils/classtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
_MAGIC_STATICMETHODS = {"__new__"}
_MAGIC_CLASSMETHODS = {"__init_subclass__", "__prepare__"}

# `type` itself has several methods (mostly dunders). When we are wrapping those names, we need to
# make sure that we don't interfere with `type.__getattribute__`'s handling that circumvents the
# normal inheritance rules when appropriate.
_TYPE_METHODS = set(dir(type))


class _lift_to_method: # pylint: disable=invalid-name
"""A decorator that ensures that an input callable object implements ``__get__``. It is
Expand Down Expand Up @@ -146,16 +141,6 @@ def wrap_method(cls: Type, name: str, *, before: Callable = None, after: Callabl
# The best time to apply decorators to methods is before they are bound (e.g. by using function
# decorators during the class definition), but if we're making a class decorator, we can't do
# that. We need the actual definition of the method, so we have to dodge the normal output of
# `type.__getattribute__`, which evalutes descriptors if it finds them, unless the name we're
# looking for is defined on `type` itself. In that case, we need the attribute getter to
# correctly return the underlying object, not the one that `type` defines for its own purposes.
attribute_getter = type.__getattribute__ if name in _TYPE_METHODS else object.__getattribute__
for cls_ in inspect.getmro(cls):
try:
method = attribute_getter(cls_, name)
break
except AttributeError:
pass
else:
raise ValueError(f"Method '{name}' is not defined for class '{cls.__name__}'")
# `type.__getattribute__`, which evalutes descriptors if it finds them.
method = inspect.getattr_static(cls, name)
setattr(cls, name, _WrappedMethod(method, before, after))
11 changes: 11 additions & 0 deletions releasenotes/notes/wrap-method-311-147d254d4b40e805.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
fixes:
- |
Fixed handling of some ``classmethod``s by
:func:`~qiskit.utils.wrap_method` in Python 3.11. Previously, in Python
3.11, ``wrap_method`` would wrap the unbounded function associated with the
``classmethod`` and then fail when invoked because the class object usually
bound to the ``classmethod`` was not passed to the function. Starting in
Python 3.11.1, this issue affected :class:`~qiskit.test.QiskitTestCase`,
preventing it from being imported by other test code. Fixed `#9291
<https://github.com/Qiskit/qiskit-terra/issues/9291>`__.
2 changes: 1 addition & 1 deletion test/python/utils/test_classtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,5 +529,5 @@ def test_raises_on_invalid_name(self):
class Dummy:
pass

with self.assertRaisesRegex(ValueError, "Method 'bad' is not defined for class 'Dummy'"):
with self.assertRaisesRegex(AttributeError, "bad"):
wrap_method(Dummy, "bad", before=lambda self: None)