'Python type hint given as a string?
I have a function of this form:
def foo(o: "hello") -> dict:
# pass
I understand that the "-> dict" means that foo returns a dictionary. What I don't understand is the "hello" part. Why is this type hint given as a string? What is "hello"?
Possibly relevant - this is an autogenerated file.
Solution 1:[1]
type annotations sometimes need to be set before the object has been created, for example:
class Node:
def next() -> Node:
pass
This piece of code actually fails, because Node
is referenced as an annotation of Node.next
while the class Node
is still being created. This is the same reason why the following fails:
class T:
t = T()
To get around this, you can use a string instead
class Node:
def next() -> 'Node':
pass
so the typechecker will only evaluate Node
later (a forward reference).
This was actually decided to be a design flaw so in python 3.7 you can use from __future__ import annotations
and the first example will work.
Edit April 2022
So a lot has changed (and yet a lot hasn't) since this was made. The most important difference is that string annotations
was deferred from py 3.10 and it sounds like it will never be stabilized. Instead the steering committee will probably end up stabilizing PEP 649 instead which has slightly more complex behavior, but still supports self-referential type hints. See the PEP for details.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |