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

Descriptor protocol documentation for super bindings is incorrect #55197

Closed
JoshuaArnold mannequin opened this issue Jan 23, 2011 · 4 comments
Closed

Descriptor protocol documentation for super bindings is incorrect #55197

JoshuaArnold mannequin opened this issue Jan 23, 2011 · 4 comments
Assignees
Labels
docs Documentation in the Doc dir

Comments

@JoshuaArnold
Copy link
Mannequin

JoshuaArnold mannequin commented Jan 23, 2011

BPO 10988
Nosy @rhettinger

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/rhettinger'
closed_at = <Date 2011-03-22.22:34:40.528>
created_at = <Date 2011-01-23.18:38:03.466>
labels = ['docs']
title = 'Descriptor protocol documentation for super bindings is incorrect'
updated_at = <Date 2011-03-22.22:34:40.526>
user = 'https://bugs.python.org/JoshuaArnold'

bugs.python.org fields:

activity = <Date 2011-03-22.22:34:40.526>
actor = 'rhettinger'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2011-03-22.22:34:40.528>
closer = 'rhettinger'
components = ['Documentation']
creation = <Date 2011-01-23.18:38:03.466>
creator = 'Joshua.Arnold'
dependencies = []
files = []
hgrepos = []
issue_num = 10988
keywords = []
message_count = 4.0
messages = ['126895', '131795', '131796', '131797']
nosy_count = 4.0
nosy_names = ['rhettinger', 'docs@python', 'Joshua.Arnold', 'python-dev']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue10988'
versions = ['Python 3.1', 'Python 2.7', 'Python 3.2', 'Python 3.3']

@JoshuaArnold
Copy link
Mannequin Author

JoshuaArnold mannequin commented Jan 23, 2011

In 'Doc/reference/datamodel.rst', the 'Invoking Descriptors' documentation specifies the following behavior for super objects:

[snip]
Super Binding
If ``a`` is an instance of :class:`super`, then the binding ``super(B,
obj).m()`` searches ``obj.__class__.__mro__`` for the base class ``A``
immediately preceding ``B`` and then invokes the descriptor with the call:
``A.__dict__['m'].__get__(obj, A)``.
[snip]

In the above paragrah, the call:

A.__dict__['m'].__get__(obj, A)

is incorrect. In reality, the descriptor is invoked via:

A.__dict__['m'].__get__(obj, obj.__class__)

Loosely speaking, the 'owner' argument is set to the subclass associated with the super object, not the superclass. There is a similar error in the 'Descriptor HowTo Guide' under 'Invoking Descriptors'

(Strictly speaking, the specification is inaccurate on some other points. It assumes obj is not a class and doesn't state that the entire mro preceding 'B' is searched for 'm'. But those may be considered simplifications for the sake of brevity)

I considered reporting this as a bug in the implementation rather than the specification. But I think the implementation's algorithm is the correct one. In particular, it yields the desired behavior for classmethods In any case, the current behavior has been around for a while (it was the fix for issue bpo-535444, dating back to 2.2)

Code demonstrating the current behavior:

class Desc(object):
    def __get__(self, instance, owner):
        return owner

class A(object):
    attr = Desc()

class B(A):
    pass

class C(B):
    pass

instance = C()

assert super(B,instance).attr == C
assert super(B,C).attr == C

#According to the specification, the assertions should be:
#assert super(B,instance).attr == A
#assert super(B,C).attr == A

@JoshuaArnold JoshuaArnold mannequin assigned docspython Jan 23, 2011
@JoshuaArnold JoshuaArnold mannequin added the docs Documentation in the Doc dir label Jan 23, 2011
@rhettinger rhettinger assigned rhettinger and unassigned docspython Jan 23, 2011
@python-dev
Copy link
Mannequin

python-dev mannequin commented Mar 22, 2011

New changeset 50cc60852a76 by Raymond Hettinger in branch '2.7':
bpo-10988: fix description of super's descriptor call.
http://hg.python.org/cpython/rev/50cc60852a76

@python-dev
Copy link
Mannequin

python-dev mannequin commented Mar 22, 2011

New changeset 3e3c46a3dce8 by Raymond Hettinger in branch '3.1':
bpo-10988: fix description of super's descriptor call.
http://hg.python.org/cpython/rev/3e3c46a3dce8

New changeset 40698c68a32c by Raymond Hettinger in branch '3.2':
bpo-10988: fix description of super's descriptor call.
http://hg.python.org/cpython/rev/40698c68a32c

New changeset 29c8eb206076 by Raymond Hettinger in branch 'default':
bpo-10988: fix description of super's descriptor call.
http://hg.python.org/cpython/rev/29c8eb206076

@rhettinger
Copy link
Contributor

Fixed.
Thanks for the bug report.

@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
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

1 participant