]> granicus.if.org Git - python/commitdiff
Issue 23898: Fix inspect.classify_class_attrs() to work with __eq__
authorYury Selivanov <yselivanov@sprymix.com>
Thu, 21 May 2015 19:41:57 +0000 (15:41 -0400)
committerYury Selivanov <yselivanov@sprymix.com>
Thu, 21 May 2015 19:41:57 +0000 (15:41 -0400)
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index 1641824237cba91fa6b6232786f77b7a98a93c6c..a91f749d520d0bf2d4882a27ae19c60f4a58b7a3 100644 (file)
@@ -380,7 +380,7 @@ def classify_class_attrs(cls):
                     # first look in the classes
                     for srch_cls in class_bases:
                         srch_obj = getattr(srch_cls, name, None)
-                        if srch_obj == get_obj:
+                        if srch_obj is get_obj:
                             last_cls = srch_cls
                     # then check the metaclasses
                     for srch_cls in metamro:
@@ -388,7 +388,7 @@ def classify_class_attrs(cls):
                             srch_obj = srch_cls.__getattr__(cls, name)
                         except AttributeError:
                             continue
-                        if srch_obj == get_obj:
+                        if srch_obj is get_obj:
                             last_cls = srch_cls
                     if last_cls is not None:
                         homecls = last_cls
@@ -402,7 +402,7 @@ def classify_class_attrs(cls):
             # unable to locate the attribute anywhere, most likely due to
             # buggy custom __dir__; discard and move on
             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.
         if isinstance(dict_obj, staticmethod):
             kind = "static method"
index a2bb9b6a2a7a5f85229c3d95875a0a59103037ee..8d92f8230e39d4fa5bc943a7333bdd896252cde5 100644 (file)
@@ -782,6 +782,21 @@ class TestClassesAndFunctions(unittest.TestCase):
         should_find_ga = inspect.Attribute('ham', 'data', Meta, 'spam')
         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):
         class Meta(type):
             fish = 'slap'
index 88e76433815a2040b44af72a45b3cd57c9a1e18e..0593d89c43d29eb7fa800e330d0301e853203d56 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -260,6 +260,9 @@ Library
 
 - asyncio: async() function is deprecated in favour of ensure_future().
 
+- Issue 23898: Fix inspect.classify_class_attrs() to support attributes
+  with overloaded __eq__ and __bool__.  Patch by Mike Bayer.
+
 Tests
 -----