Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inspect() changes in Python3.4 are not compatible with objects that implement special __bool__, __eq__ #68086

Closed
zzzeek mannequin opened this issue Apr 9, 2015 · 12 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@zzzeek
Copy link
Mannequin

zzzeek mannequin commented Apr 9, 2015

BPO 23898
Nosy @bitdancer, @1st1
Files
  • issue23898.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/1st1'
    closed_at = <Date 2015-05-21.19:47:07.370>
    created_at = <Date 2015-04-09.14:34:46.137>
    labels = ['library']
    title = 'inspect() changes in Python3.4 are not compatible with objects that implement special __bool__, __eq__'
    updated_at = <Date 2015-05-21.20:05:39.452>
    user = 'https://bugs.python.org/zzzeek'

    bugs.python.org fields:

    activity = <Date 2015-05-21.20:05:39.452>
    actor = 'zzzeek'
    assignee = 'yselivanov'
    closed = True
    closed_date = <Date 2015-05-21.19:47:07.370>
    closer = 'yselivanov'
    components = ['Library (Lib)']
    creation = <Date 2015-04-09.14:34:46.137>
    creator = 'zzzeek'
    dependencies = []
    files = ['38882']
    hgrepos = []
    issue_num = 23898
    keywords = ['patch']
    message_count = 12.0
    messages = ['240331', '240335', '240337', '240344', '240370', '240373', '240374', '240400', '241100', '243783', '243784', '243785']
    nosy_count = 4.0
    nosy_names = ['r.david.murray', 'zzzeek', 'python-dev', 'yselivanov']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue23898'
    versions = ['Python 3.4', 'Python 3.5']

    @zzzeek
    Copy link
    Mannequin Author

    zzzeek mannequin commented Apr 9, 2015

    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"

    @zzzeek zzzeek mannequin added the stdlib Python modules in the Lib dir label Apr 9, 2015
    @bitdancer
    Copy link
    Member

    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.

    @zzzeek
    Copy link
    Mannequin Author

    zzzeek mannequin commented Apr 9, 2015

    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.

    @zzzeek
    Copy link
    Mannequin Author

    zzzeek mannequin commented Apr 9, 2015

    patch w/ test

    @1st1
    Copy link
    Member

    1st1 commented Apr 9, 2015

    The patch looks good, Mike. Could you please sign PSF Contributor Agreement?

    @zzzeek
    Copy link
    Mannequin Author

    zzzeek mannequin commented Apr 9, 2015

    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.

    @1st1
    Copy link
    Member

    1st1 commented Apr 9, 2015

    Mike, please ping me when they process it. I'll commit your patch.

    @zzzeek
    Copy link
    Mannequin Author

    zzzeek mannequin commented Apr 9, 2015

    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?

    @zzzeek
    Copy link
    Mannequin Author

    zzzeek mannequin commented Apr 15, 2015

    my star went through.

    let's merge.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 21, 2015

    New changeset fb9addfdfc35 by Yury Selivanov in branch '3.4':
    bpo-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':
    bpo-23898: Fix inspect.classify_class_attrs() to work with __eq__
    https://hg.python.org/cpython/rev/d6a9d225413a

    @1st1
    Copy link
    Member

    1st1 commented May 21, 2015

    Merged. Thanks for the bug report and patch, Mike!

    @1st1 1st1 closed this as completed May 21, 2015
    @1st1 1st1 self-assigned this May 21, 2015
    @zzzeek
    Copy link
    Mannequin Author

    zzzeek mannequin commented May 21, 2015

    thanks for the merge!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants