This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2011-02-26 20:23 by andyharrington, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
annotations_workaround.py rhettinger, 2011-02-27 22:59 Work-around for self-referencing function annotations.
Messages (8)
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.
msg388770 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-03-15 19:46
Is this issue resolved with PEP 563 and the behavior becoming default in Python 3.10? https://www.python.org/dev/peps/pep-0563/#forward-references
msg415980 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-03-24 23:09
This looks like PEP 673 – Self Type.
msg416014 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-03-25 19:03
Agree. typing.Self from PEP 673 fixes this specific case, and PEP 563 or 649 will provide a general solution. No need to keep this issue open.
History
Date User Action Args
2022-04-11 14:57:13adminsetgithub: 55548
2022-03-25 19:07:46iritkatrielsetstatus: open -> closed
stage: resolved
2022-03-25 19:03:24JelleZijlstrasetstatus: pending -> open
nosy: + JelleZijlstra
messages: + msg416014

2022-03-24 23:09:21iritkatrielsetstatus: open -> pending

nosy: + iritkatriel
messages: + msg415980

resolution: out of date
2021-03-15 19:54:31kamiltureksetnosy: + kamilturek
2021-03-15 19:46:43xtreaksetnosy: + xtreak
messages: + msg388770
2021-03-15 18:57:58Socobsetnosy: + Socob
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 -> (no value)
2011-02-26 20:36:15benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg129591

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