Python - Type hints

annotation

Posted by Rico's Nerd Cluster on March 11, 2019

from __future__ import annotations

A rough mental model:

  • Without it: annotations are evaluated immediately at definition time
  • With it: annotations are stored as strings and resolved later

The Problem

Without from __future__ import annotations, forward references fail because the name isn’t defined yet:

1
2
3
class Node:  
    def __init__(self, next: Node | None = None):  # NameError: 'Node' is not defined
        self.next = next

This fails in older Python versions because Node is used before the class is fully defined.

The Fix

1
2
3
4
5
from __future__ import annotations

class Node:  
    def __init__(self, next: Node | None = None):  
        self.next = next

With this import, Python stores annotations as deferred strings ("Node | None") instead of evaluating them immediately at function/class definition time. Node | None is kept as annotation data first, not resolved immediately, so the forward reference works.