Commit 095fb84f authored by Yury Selivanov's avatar Yury Selivanov

Issue 23898: Fix inspect.classify_class_attrs() to work with __eq__

Patch by Mike Bayer.
parents c9745e5f bf341fb5
...@@ -395,7 +395,7 @@ def classify_class_attrs(cls): ...@@ -395,7 +395,7 @@ def classify_class_attrs(cls):
# first look in the classes # first look in the classes
for srch_cls in class_bases: for srch_cls in class_bases:
srch_obj = getattr(srch_cls, name, None) srch_obj = getattr(srch_cls, name, None)
if srch_obj == get_obj: if srch_obj is get_obj:
last_cls = srch_cls last_cls = srch_cls
# then check the metaclasses # then check the metaclasses
for srch_cls in metamro: for srch_cls in metamro:
...@@ -403,7 +403,7 @@ def classify_class_attrs(cls): ...@@ -403,7 +403,7 @@ def classify_class_attrs(cls):
srch_obj = srch_cls.__getattr__(cls, name) srch_obj = srch_cls.__getattr__(cls, name)
except AttributeError: except AttributeError:
continue continue
if srch_obj == get_obj: if srch_obj is get_obj:
last_cls = srch_cls last_cls = srch_cls
if last_cls is not None: if last_cls is not None:
homecls = last_cls homecls = last_cls
...@@ -417,7 +417,7 @@ def classify_class_attrs(cls): ...@@ -417,7 +417,7 @@ def classify_class_attrs(cls):
# unable to locate the attribute anywhere, most likely due to # unable to locate the attribute anywhere, most likely due to
# buggy custom __dir__; discard and move on # buggy custom __dir__; discard and move on
continue continue
obj = get_obj or dict_obj obj = get_obj if get_obj is not None else dict_obj
# Classify the object or its descriptor. # Classify the object or its descriptor.
if isinstance(dict_obj, staticmethod): if isinstance(dict_obj, staticmethod):
kind = "static method" kind = "static method"
......
...@@ -888,6 +888,21 @@ class TestClassesAndFunctions(unittest.TestCase): ...@@ -888,6 +888,21 @@ class TestClassesAndFunctions(unittest.TestCase):
should_find_ga = inspect.Attribute('ham', 'data', Meta, 'spam') should_find_ga = inspect.Attribute('ham', 'data', Meta, 'spam')
self.assertIn(should_find_ga, inspect.classify_class_attrs(VA)) self.assertIn(should_find_ga, inspect.classify_class_attrs(VA))
def test_classify_overrides_bool(self):
class NoBool(object):
def __eq__(self, other):
return NoBool()
def __bool__(self):
raise NotImplementedError(
"This object does not specify a boolean value")
class HasNB(object):
dd = NoBool()
should_find_attr = inspect.Attribute('dd', 'data', HasNB, HasNB.dd)
self.assertIn(should_find_attr, inspect.classify_class_attrs(HasNB))
def test_classify_metaclass_class_attribute(self): def test_classify_metaclass_class_attribute(self):
class Meta(type): class Meta(type):
fish = 'slap' fish = 'slap'
......
...@@ -52,6 +52,9 @@ Core and Builtins ...@@ -52,6 +52,9 @@ Core and Builtins
- Issue #24017: PEP 492: Coroutines with async and await syntax. - Issue #24017: PEP 492: Coroutines with async and await syntax.
- Issue #23898: Fix inspect.classify_class_attrs() to support attributes
with overloaded __eq__ and __bool__. Patch by Mike Bayer.
Library Library
------- -------
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment