Issue 9732: fetch the method resolution order from the type metaclass directly in getattr_static
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 97e99aa..2f05829 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1060,6 +1060,9 @@
_sentinel = object()
+def _static_getmro(klass):
+ return type.__dict__['__mro__'].__get__(klass)
+
def _check_instance(obj, attr):
instance_dict = {}
try:
@@ -1070,7 +1073,7 @@
def _check_class(klass, attr):
- for entry in getmro(klass):
+ for entry in _static_getmro(klass):
try:
return entry.__dict__[attr]
except KeyError:
@@ -1110,7 +1113,7 @@
if obj is klass:
# for types we check the metaclass too
- for entry in getmro(type(klass)):
+ for entry in _static_getmro(type(klass)):
try:
return entry.__dict__[attr]
except KeyError:
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index e320c68..b3e131c 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -867,6 +867,22 @@
self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3)
self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)
+ def test_mro_as_property(self):
+ class Meta(type):
+ @property
+ def __mro__(self):
+ return (object,)
+
+ class Base(object):
+ foo = 3
+
+ class Something(Base, metaclass=Meta):
+ pass
+
+ self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3)
+ self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)
+
+
def test_main():
run_unittest(
TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,