Type hints in namedtuple
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over
--
Chapters
00:00 Type Hints In Namedtuple
00:23 Answer 1 Score 184
00:40 Accepted Answer Score 347
01:02 Answer 3 Score 17
01:48 Thank you
--
Full question
https://stackoverflow.com/questions/3426...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #typehinting #pythontyping #namedtuple #pythondataclasses
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over
--
Chapters
00:00 Type Hints In Namedtuple
00:23 Answer 1 Score 184
00:40 Accepted Answer Score 347
01:02 Answer 3 Score 17
01:48 Thank you
--
Full question
https://stackoverflow.com/questions/3426...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #typehinting #pythontyping #namedtuple #pythondataclasses
#avk47
ACCEPTED ANSWER
Score 347
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)
ANSWER 2
Score 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
ANSWER 3
Score 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