classification
Title: annotation for class being defined
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andyharrington, benjamin.peterson, daniel.urban, eric.araujo, eric.snow, levkivskyi, rhettinger, ryanhiebert, stutzbach, terry.reedy
Priority: normal Keywords:

Created on 2011-02-26 20:23 by andyharrington, last changed 2017-02-13 23:18 by levkivskyi.

Files
File name Uploaded Description Edit
annotations_workaround.py rhettinger, 2011-02-27 22:59 Work-around for self-referencing function annotations.
Messages (5)
msg129587 - (view) Author: Andy Harrington (andyharrington) Date: 2011-02-26 20:23
You cannot make a self-referential annotation like

class Graph:
    def reverse(self) -> Graph:
       # ...

This corresponds to a common coding situation.
msg129591 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-02-26 20:36
This has been true forever. I suggest you try python-ideas.
msg129660 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-02-27 21:44
This would be an important fix-up if we could find some way to implement it.

The basic problem is that the class object is defined after the class definitions have been made, so the target of the Graph reference isn't known when the method definitions are being compiled.

One approach to solving this problem is to use a deferred/resolved pattern (creating a valid reference to Graph that is visible during method compilation, but gets filled-in with the other methods afterwards).

I haven't thought this through completely but think it could be done if we let the compiler write to the class dictionary (this is normally off-limits and protected by a dict_proxy).  If we let the system punch a hole in the proxy, it is possible to resolve deferred class definitions.


class Prepared(type):
    'Preload the class with a reference to itself'

    @classmethod
    def __prepare__(mcl, name, bases):
        return {name: type(name, bases, {})}

    def __new__(mcl, name, bases, mapping):
        tmpcls = super().__new__(mcl, name, bases, mapping)
        deferred_class = mapping[name]
        deferred_class.__dict__.update(tmpcls.__dict__)  # XXX need writeable dict_proxy
        return deferred_class

class Graph(metaclass=Prepared):
    def reverse(self) -> Graph:
        ...
msg129665 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-02-27 22:59
Until and unless this gets fixed, perhaps we should document some sort of workaround.  One possibility is attached.
msg130075 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-03-04 21:06
>The basic problem is that the class object is defined after the class definitions have been made,

I have sometimes thought the the class statement could start with binding the name to a blank object or new, blank class object. But then people might try to grab attributes (and fail!) outside of method defs.
History
Date User Action Args
2017-02-13 23:18:59levkivskyisetnosy: + levkivskyi
2017-01-13 00:23:43ryanhiebertsetnosy: + ryanhiebert
2011-07-09 20:51:20eric.snowsetnosy: + eric.snow
2011-07-09 14:40:26eric.araujosetnosy: + eric.araujo
2011-03-04 21:06:04terry.reedysetnosy: + terry.reedy
messages: + msg130075
2011-02-27 22:59:30rhettingersetfiles: + annotations_workaround.py
nosy: rhettinger, andyharrington, benjamin.peterson, stutzbach, daniel.urban
messages: + msg129665
2011-02-27 21:53:22daniel.urbansetnosy: + daniel.urban
2011-02-27 21:50:02arigosetnosy: - arigo
2011-02-27 21:44:13rhettingersetnosy: arigo, rhettinger, andyharrington, benjamin.peterson, stutzbach
messages: + msg129660
2011-02-27 21:43:31rhettingersetnosy: arigo, rhettinger, andyharrington, benjamin.peterson, stutzbach
messages: - msg129659
2011-02-27 21:42:39rhettingersetnosy: arigo, rhettinger, andyharrington, benjamin.peterson, stutzbach
messages: + msg129659
2011-02-27 21:42:27rhettingersetnosy: arigo, rhettinger, andyharrington, benjamin.peterson, stutzbach
messages: - msg129657
2011-02-27 21:37:53rhettingersetnosy: arigo, rhettinger, andyharrington, benjamin.peterson, stutzbach
messages: + msg129657
2011-02-27 21:37:36rhettingersetnosy: arigo, rhettinger, andyharrington, benjamin.peterson, stutzbach
messages: - msg129656
2011-02-27 21:37:06rhettingersetstatus: closed -> open

nosy: + rhettinger, stutzbach, arigo
messages: + msg129656

resolution: rejected ->
2011-02-26 20:36:15benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg129591

resolution: rejected
2011-02-26 20:23:39andyharringtoncreate