ChatGPT解决这个技术问题 Extra ChatGPT

putting current class as return type annotation [duplicate]

This question already has answers here: How do I type hint a method with the type of the enclosing class? (7 answers) Closed 3 years ago.

In python 3 I can make arguments and return type annotations. Example:

class Graph:
    def __init__(self, V: int, E: int, edges: list):
        pass

    @classmethod
    def fromfile(cls, readobj: type(sys.stdin)):
        pass

    def V(self) -> int:
        pass

    def E(self) -> int:
        pass

The problem is I can't make an annotation with return type of the current class (Graph), which is not defined yet. Example:

class Graph:
   def reverse(self) -> Graph:
       pass

This code goes with error

def reverse(self) -> Graph:
NameError: name 'Graph' is not defined

These annotations are really useful both for documenting and allowing IDE to recognize argument and return types => enable autocomplete

UPD: So what I came up is this is either impossible or requires some hacks I don't like, so I decided to use just def reverse (self) -> 'Graph': which is understandable for documentation although breaks the rule. The downside is that it doesn't work for IDE autocomplete.

Have you tried : def reverse(self) -> type(self)
@deceze Why do you mark the older question as a duplicate of a newer question?
@Shlomo Because the answer there is a lot better.

M
Mazdak

In python-3.7 this issue has been resolved by not evaluating the annotations at function definition time. Instead, they are preserved in __annotations__ in string form. This is called Postponed Evaluation of Annotations, introduced in PEP 563.

Also note:

Deprecation policy Starting with Python 3.7, a __future__ import is required to use the described functionality. No warnings are raised. In Python 3.8 a PendingDeprecationWarning is raised by the compiler in the presence of type annotations in modules without the __future__ import. Starting with Python 3.9 the warning becomes a DeprecationWarning. In Python 4.0 this will become the default behavior. Use of annotations incompatible with this PEP is no longer supported.

Here is an example:

In [7]: from __future__ import annotations

In [8]: class C:
   ...:     def func(cls, arg:str) -> C:
   ...:         pass
   ...:     

In [9]: c = C()

Even though this question is very old and original author answered to himself I'd make this answer an accepted answer.
This is the best answer and provided precisely the correct usage.
To the point. Thanks
s
sasha.sochka

So now after a while I can say that decision I took was using -> 'Graph' instead of -> Graph. It does not make my IDE (PyCharm) able to recognize a type this way but it just works well enough for documentation purposes.

Another possible solution I could use was changing annotation at runtime but that doesn't solve the problem with documentation - you won't want to look for type declarations somewhere in the middle of sources...

The problem has roots in recognizing class object before the class was actually defined. That is simply impossible to do in python.


Actually, this is a correct solution to a problem known as forward references - python.org/dev/peps/pep-0484/#forward-references. And current version of PyCharm (in time of writing this comment) recognizes a typy written this was and offers all the needed support.
Update from 2020 - PyCharm does recognize the type in the -> 'Graph' format now - you can CMD+Click to go to definition, etc