5.1. Type Annotation

5.1.1. What are Type Annotations?

New in version Python: 3.5 See PEP 484

  • Also known as Type Hinting

  • Accessed by __annotations__ attribute

  • No type checking happens at runtime

  • Use separate off-line type checker which users can run over their source code voluntarily

  • Supports unions, generic types, and a special type named

  • Any is consistent with (i.e. assignable to and from) all types

  • This latter feature is taken from the idea of gradual typing.

  • Gradual typing and the full type system are explained in PEP 483

Warning

It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.

5.1.2. Data types

name: str = 'Jan Twardowski'
leet: int = 1337
pi: float = 3.14

5.1.3. Final

New in version Python: 3.8 See PEP 591

from typing import final

@final
class Base:
    ...

class Derived(Base):  # Error: Cannot inherit from final class "Base"
    ...
from typing import Final


ID: Final = 1
ID: Final[float] = 1
from typing import Final

class Window:
    BORDER_WIDTH: Final = 2.5

class ListView(Window):
    BORDER_WIDTH = 3  # Error: can't override a final attribute
from typing import Final

class ImmutablePoint:
    x: Final[int]
    y: Final[int]  # Error: final attribute without an initializer

    def __init__(self) -> None:
        self.x = 1  # Good
from typing import Final

RATE: Final = 3000

class Base:
    DEFAULT_ID: Final = 0

RATE = 300  # Error: can't assign to final attribute
Base.DEFAULT_ID = 1  # Error: can't override a final attribute