MCPcopy Index your code
hub / github.com/python/cpython / NamedTuple

Function NamedTuple

Lib/typing.py:3090–3114  ·  view source on GitHub ↗

Typed version of namedtuple. Usage:: class Employee(NamedTuple): name: str id: int This is equivalent to:: Employee = collections.namedtuple('Employee', ['name', 'id']) The resulting class has an extra __annotations__ attribute, giving a d

(typename, fields, /)

Source from the content-addressed store, hash-verified

3088
3089
3090def NamedTuple(typename, fields, /):
3091 """Typed version of namedtuple.
3092
3093 Usage::
3094
3095 class Employee(NamedTuple):
3096 name: str
3097 id: int
3098
3099 This is equivalent to::
3100
3101 Employee = collections.namedtuple('Employee', ['name', 'id'])
3102
3103 The resulting class has an extra __annotations__ attribute, giving a
3104 dict that maps field names to types. (The field names are also in
3105 the _fields attribute, which is part of the namedtuple API.)
3106 An alternative equivalent functional syntax is also accepted::
3107
3108 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
3109 """
3110 types = {n: _type_check(t, f"field {n} annotation must be a type")
3111 for n, t in fields}
3112 nt = _make_nmtuple(typename, types, _make_eager_annotate(types), module=_caller())
3113 nt.__orig_bases__ = (NamedTuple,)
3114 return nt
3115
3116_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
3117

Callers 6

test_basicsMethod · 0.90
test_empty_namedtupleMethod · 0.90
test_copy_and_pickleMethod · 0.90
test_orig_basesMethod · 0.90

Calls 4

_type_checkFunction · 0.85
_make_nmtupleFunction · 0.85
_make_eager_annotateFunction · 0.85
_callerFunction · 0.85

Tested by 6

test_basicsMethod · 0.72
test_empty_namedtupleMethod · 0.72
test_copy_and_pickleMethod · 0.72
test_orig_basesMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…