All classes need to be defined before they can be used as a type hint. To get around it in some scenarios, the __future__
import is recommended. That's why the following code works fine (in Python 3.7):
from __future__ import annotations
class Person:
def get_relative(name: str) -> Person:
...
Without the __future__
import it would raise a NameError
. But this code works fine too:
from __future__ import annotations
class Person:
def get_relative(name: BlahBlahTypoWhatever) -> Person:
...
I expected something like NameError: name 'BlahBlahTypoWhatever' is not defined
. Is this an expected behavior?