classification
Title: inspect() changes in Python3.4 are not compatible with objects that implement special __bool__, __eq__
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.5, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: yselivanov Nosy List: python-dev, r.david.murray, yselivanov, zzzeek
Priority: normal Keywords: patch

Created on 2015-04-09 14:34 by zzzeek, last changed 2015-05-21 20:05 by zzzeek. This issue is now closed.

Files
File name Uploaded Description Edit
issue23898.patch zzzeek, 2015-04-09 15:42 review
Messages (12)
msg240331 - (view) Author: mike bayer (zzzeek) * Date: 2015-04-09 14:34
this bug appeared in Python 3.4.    The inspect.classify_class_attrs compares the identity objects of unknown type using the `==` operator unnecessarily and also evaluates objects of unknown type assuming they return `True` for a straight boolean evaluation.  This breaks among other things the ability to use the help() function with SQLAlchemy mapped objects.

Demo:

class MySpecialObject(object):

    def __eq__(self, other):
        return MySpecialObject()

    def __bool__(self):
        raise NotImplementedError(
            "This object does not specify a boolean value")


class MyClass(object):
    some_thing = MySpecialObject()

import inspect

print(inspect.classify_class_attrs(MyClass))

# ultimate goal:  help(MyClass)

A patch here would be to compare unknown objects for identity using the `is` operator as well as using `is not None` when asserting that an object of unknown type is non-None.   This patch resolves:

--- inspect_orig.py	2015-04-09 10:28:46.000000000 -0400
+++ inspect.py	2015-04-09 10:29:37.000000000 -0400
@@ -380,7 +380,7 @@
                     # 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 @@
                             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 @@
             # 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"
msg240335 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-09 15:20
This looks reasonable to me.  It would be great if the patch could be attached to the issue as a patch file, including some tests.
msg240337 - (view) Author: mike bayer (zzzeek) * Date: 2015-04-09 15:23
> It would be great if the patch could be attached to the issue as a patch file, including some tests.

the mantra we all share.   I'll take a look.
msg240344 - (view) Author: mike bayer (zzzeek) * Date: 2015-04-09 15:42
patch w/ test
msg240370 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-04-09 19:45
The patch looks good, Mike. Could you please sign PSF Contributor Agreement?
msg240373 - (view) Author: mike bayer (zzzeek) * Date: 2015-04-09 19:54
hi Yury - 

I did sign it earlier today.   It should have been sent off to the person that manages that, at least that's what the email receipt said.
msg240374 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-04-09 19:56
Mike, please ping me when they process it. I'll commit your patch.
msg240400 - (view) Author: mike bayer (zzzeek) * Date: 2015-04-09 22:24
hi Yury -

I have a confirmation including a PDF attachment of the agreement, the message reads:

" Python Contributor Agreement Form between Python Software Foundation and Michael Bayer is Signed and Filed!

From: Ewa Jodlowska (Python Software Foundation)
To: Ewa Jodlowska and Michael Bayer "

can I forward that to you?
msg241100 - (view) Author: mike bayer (zzzeek) * Date: 2015-04-15 13:42
my star went through.

let's merge.
msg243783 - (view) Author: Roundup Robot (python-dev) Date: 2015-05-21 19:46
New changeset fb9addfdfc35 by Yury Selivanov in branch '3.4':
Issue 23898: Fix inspect.classify_class_attrs() to work with __eq__
https://hg.python.org/cpython/rev/fb9addfdfc35

New changeset d6a9d225413a by Yury Selivanov in branch 'default':
Issue 23898: Fix inspect.classify_class_attrs() to work with __eq__
https://hg.python.org/cpython/rev/d6a9d225413a
msg243784 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-05-21 19:47
Merged. Thanks for the bug report and patch, Mike!
msg243785 - (view) Author: mike bayer (zzzeek) * Date: 2015-05-21 20:05
thanks for the merge!
History
Date User Action Args
2015-05-21 20:05:39zzzeeksetmessages: + msg243785
2015-05-21 19:47:07yselivanovsetstatus: open -> closed
messages: + msg243784

assignee: yselivanov
resolution: fixed
stage: commit review -> resolved
2015-05-21 19:46:19python-devsetnosy: + python-dev
messages: + msg243783
2015-04-15 13:42:06zzzeeksetmessages: + msg241100
2015-04-09 22:24:52zzzeeksetmessages: + msg240400
2015-04-09 19:56:23yselivanovsetmessages: + msg240374
2015-04-09 19:54:40zzzeeksetmessages: + msg240373
2015-04-09 19:45:42yselivanovsetmessages: + msg240370
2015-04-09 16:24:32r.david.murraysetstage: needs patch -> commit review
2015-04-09 15:42:56zzzeeksetfiles: + issue23898.patch
keywords: + patch
messages: + msg240344
2015-04-09 15:23:41zzzeeksetmessages: + msg240337
2015-04-09 15:20:43r.david.murraysetversions: + Python 3.5
nosy: + r.david.murray

messages: + msg240335

stage: needs patch
2015-04-09 14:39:59berker.peksagsetnosy: + yselivanov
2015-04-09 14:34:46zzzeekcreate