-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathnamedtuples_usage.py
More file actions
61 lines (41 loc) · 1.26 KB
/
namedtuples_usage.py
File metadata and controls
61 lines (41 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Tests NamedTuple usage.
"""
from typing import NamedTuple, assert_type
# Specification: https://typing.readthedocs.io/en/latest/spec/namedtuples.html#named-tuple-usage
class Point(NamedTuple):
x: int
y: int
units: str = "meters"
# > The fields within a named tuple instance can be accessed by name using an
# > attribute access (``.``) operator. Type checkers should support this.
p = Point(1, 2)
assert_type(p.x, int)
assert_type(p.units, str)
# > Like normal tuples, elements of a named tuple can also be accessed by index,
# > and type checkers should support this.
assert_type(p[0], int)
assert_type(p[1], int)
assert_type(p[2], str)
assert_type(p[-1], str)
assert_type(p[-2], int)
assert_type(p[-3], int)
print(p[3]) # E
print(p[-4]) # E
# > Type checkers should enforce that named tuple fields cannot be overwritten
# > or deleted.
p.x = 3 # E
p[0] = 3 # E
del p.x # E
del p[0] # E
# > Like regular tuples, named tuples can be unpacked. Type checkers should understand
# > this.
x1, y1, units1 = p
assert_type(x1, int)
assert_type(units1, str)
x2, y2 = p # E: too few values to unpack
x3, y3, unit3, other = p # E: too many values to unpack
class PointWithName(Point):
name: str = "" # OK
pn = PointWithName(1, 1)
x4, y4, units4 = pn