Type hints in namedtuple Type hints in namedtuple python-3.x python-3.x

Type hints in namedtuple


The prefered Syntax for a typed named tuple since 3.6 is

from typing import NamedTupleclass Point(NamedTuple):    x: int    y: int = 1  # Set default valuePoint(3)  # -> Point(x=3, y=1)

EditStarting Python 3.7, consider using dataclasses (your IDE may not yet support them for static type checking):

from dataclasses import dataclass@dataclassclass Point:    x: int    y: int = 1  # Set default valuePoint(3)  # -> Point(x=3, y=1)


You can use typing.NamedTuple

From the docs

Typed version of namedtuple.

>>> import typing>>> Point = typing.NamedTuple("Point", [('x', int), ('y', int)])

This is present only in Python 3.5 onwards