256

Consider following piece of code:

from collections import namedtuple
point = namedtuple("Point", ("x:int", "y:int"))

The Code above is just a way to demonstrate as to what I am trying to achieve. I would like to make namedtuple with type hints.

Do you know any elegant way how to achieve result as intended?

1

3 Answers 3

344

The preferred syntax for a typed namedtuple since Python 3.6 is

from typing import NamedTuple

class Point(NamedTuple):
    x: int
    y: int = 1  # Set default value

Point(3)  # -> Point(x=3, y=1)

Starting with Python 3.7, consider using a dataclasses:

from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int = 1  # Set default value

Point(3)  # -> Point(x=3, y=1)
7
  • 40
    @JohnE; The OP specifically asked for named tuples. Yes, many use cases of named tuples will be better served by data classes. But to quote the excellent Why not namedtuples: If you want a tuple with names, by all means: go for a namedtuple Commented Jul 30, 2018 at 14:14
  • 11
    Using dataclasses, it is not possible to deconstruct the resulting object like you could a Tuple
    – VARAK
    Commented Oct 24, 2019 at 12:33
  • 37
    A tuple is immutable. A dataclass is not (by default) It does have the frozen flag which gets close to tuple's behaviour. Just something to be aware of.
    – shao.lo
    Commented Dec 2, 2019 at 22:22
  • 4
    if dataclass works for you, you can go further and use pydantic package to enforce type checking on runtime in elegant way.
    – izkeros
    Commented Feb 18, 2021 at 7:42
  • 6
    Dataclasses aren't subscribable, and they neither are unpackable while iterating as named tuples do so I think they are far from being a perfect alternative.
    – Vichoko
    Commented Oct 18, 2021 at 21:56
184

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

3
  • 17
    In newer versions you may declare NamedTuples as Point = typing.NamedTuple("Point", x=int, y=int), which is much cleaner and shorter. Commented Oct 11, 2021 at 20:56
  • 2
    ^ Documentation, discussion, and examples floating around for this syntax is nearly non-existent. I only found it in the cpython typing.py file. It states the syntax is available from 3.6+, and it's at least in the 3.7+ code. Though there doesn't seem to be a cleaner version of this for setting defaults adjacent to the member/type declaration.
    – JWCS
    Commented Feb 10, 2022 at 19:46
  • ^^ While the keyword-argument syntax mentioned by @MarkedasDuplicate still works (tested on Python 3.10.9 (tags/v3.10.9:1dd9be6)), it was removed from the docstrings in pull 105287 because according to the pull author, "this way of creating NamedTuples isn't supported by any [known] type checker". Commented Jan 17 at 16:32
17

Just to be fair, NamedTuple from typing:

>>> from typing import NamedTuple
>>> class Point(NamedTuple):
...     x: int
...     y: int = 1  # Set default value
...
>>> Point(3)
Point(x=3, y=1)

equals to classic namedtuple:

>>> from collections import namedtuple
>>> p = namedtuple('Point', 'x,y', defaults=(1, ))
>>> p.__annotations__ = {'x': int, 'y': int}
>>> p(3)
Point(x=3, y=1)

So, NamedTuple is just syntax sugar for namedtuple

Below, you can find a creating NamedTuple function from the source code of python 3.10. As we can see, it uses collections.namedtuple constructor and adds __annotations__ from extracted types:

def _make_nmtuple(name, types, module, defaults = ()):
    fields = [n for n, t in types]
    types = {n: _type_check(t, f"field {n} annotation must be a type")
             for n, t in types}
    nm_tpl = collections.namedtuple(name, fields,
                                    defaults=defaults, module=module)
    nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
    return nm_tpl
3
  • 1
    Syntactic sugar is something the parser can replace with more fundamental syntax. NamedTuple is a bit more complicated than that, being a function that actually does something at runtime.
    – chepner
    Commented Nov 17, 2021 at 18:05
  • Yes, I know what it does do during runtime. It is extracting types and adds them to __annotations__ attr of just created namedtuple using constructor collections.namedtuple. I added that code to the answer for better understanding.
    – mrvol
    Commented Nov 17, 2021 at 20:47
  • 1
    @mrvol Thanks for this. it really helped me understand what is actually happening, and that the resulting object is the same in both cases. Commented Sep 14, 2023 at 16:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.