classification
Title: Descriptors get invoked in old-style objects and classes
Type: behavior Stage: needs patch
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, ezio.melotti, icecrime, pconnell, rhettinger
Priority: normal Keywords:

Created on 2013-05-24 05:39 by icecrime, last changed 2013-05-29 06:00 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
desc.py icecrime, 2013-05-24 05:39
Messages (4)
msg189896 - (view) Author: Arnaud Porterie (icecrime) Date: 2013-05-24 05:39
In the Data Model section of the documentation regarding descriptors invokations (http://docs.python.org/2/reference/datamodel.html#invoking-descriptors), it is said:

    Note that descriptors are only invoked for new style objects or classes (ones that subclass object() or type()).

However, it seems this restriction isn't enforced in practice:

    Python 2.7.4 (default, May 16 2013, 13:28:03)
    [GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.60))] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class Desc(object):
    ...     def __get__(self, obj, cls):
    ...             return 'test'
    ...
    >>> class A:  # Not inheriting object here
    ...     desc = Desc()
    ...
    >>> A().desc
    'test'

I dived into CPython's code and saw no trace of a test for new-style classes in the descriptor invokation code path (down in classobject.c / instance_getattr2).

Unfortunately, fixing this behavior doesn't seem trivial as class methods appear to be implemented as descriptor themselves. In other words, and from my understanding, restricting descriptor invokation to new-style objects and classes would prevent method calls on old-style classes.
msg190135 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-05-27 08:34
IMO nothing should change here (either the docs or the implementation).

The OP has observed an implementation detail of old-style classes (which were reimplemented in Python 2.2 using descriptor logic instead of their former hard-wired behaviors).
msg190237 - (view) Author: Arnaud Porterie (icecrime) Date: 2013-05-28 19:46
Sorry if I am missing something, but it seems that the documentation doesn't match the behavior: the doc says that descriptors are invoked only for new styles objects and classes, while the attached code seems to prove the contrary.

If my understanding is correct, either the doc or the code should be changed accordingly.
msg190273 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-05-29 06:00
We document what we're willing to guarantee.  The exposure of descriptors in old-style classes was an incidental implementation detail.

Sorry, I'm going to close this one.   Besides, it's time to start forgetting Python 2 and move along :-)
History
Date User Action Args
2013-05-29 06:00:40rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg190273
2013-05-28 19:46:46icecrimesetmessages: + msg190237
2013-05-27 08:34:49rhettingersetnosy: + rhettinger
messages: + msg190135
2013-05-26 13:40:32ezio.melottisetnosy: + ezio.melotti

stage: needs patch
2013-05-26 03:20:23eric.araujosetnosy: + eric.araujo
2013-05-25 09:33:46pconnellsetnosy: + pconnell
2013-05-24 05:39:43icecrimecreate