-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathnamedtuples_define_functional.py
More file actions
69 lines (52 loc) · 2.12 KB
/
namedtuples_define_functional.py
File metadata and controls
69 lines (52 loc) · 2.12 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
62
63
64
65
66
67
68
69
"""
Tests NamedTuple definitions using the functional syntax.
"""
from collections import namedtuple
from typing import NamedTuple
# Specification: https://typing.readthedocs.io/en/latest/spec/namedtuples.html#defining-named-tuples
# > A type checker may support the function syntax in its various forms::
Point1 = namedtuple("Point1", ["x", "y"])
p1_1 = Point1(x=1, y=1)
p1_2 = Point1(2.3, "")
p1_3 = Point1(2.3) # E
Point2 = namedtuple("Point2", ("x", "y"))
p2_1 = Point2(x=1, y=1)
p2_2 = Point2(2.3, "")
p2_3 = Point2() # E
Point3 = namedtuple("Point3", "x y")
p3_1 = Point3(x=1, y=1)
p3_2 = Point3(2.3, "")
p3_3 = Point3(1, 2, 3) # E
Point4 = namedtuple("Point4", "x, y")
p4_1 = Point4(x=1, y=1)
p4_2 = Point4(2.3, "")
p4_3 = Point4(1, z=3) # E
Point5 = NamedTuple("Point5", [("x", int), ("y", int)])
p5_1 = Point5(x=1, y=1)
p5_2 = Point5(2, 1)
p5_3 = Point5(2, "1") # E
p5_4 = Point5(1, 2, 3) # E
Point6 = NamedTuple("Point6", (("x", int), ("y", int)))
p6_1 = Point6(x=1, y=1)
p6_2 = Point6(2, 1)
p6_3 = Point6(2, "1") # E
p6_4 = Point6(x=1.1, y=2) # E
# > At runtime, the ``namedtuple`` function disallows field names that begin with
# > an underscore or are illegal Python identifiers, and either raises an exception
# > or replaces these fields with a parameter name of the form ``_N``. The behavior
# > depends on the value of the ``rename`` argument. Type checkers may replicate
# > this behavior statically.
NT1 = namedtuple("NT1", ["a", "a"]) # E?: duplicate field name
NT2 = namedtuple("NT2", ["abc", "def"]) # E?: illegal field name
NT3 = namedtuple("NT3", ["abc", "def"], rename=False) # E?: illegal field name
NT4 = namedtuple("NT4", ["abc", "_d"], rename=False) # E?: illegal field name
NT5 = namedtuple("NT5", ["abc", "def"], rename=True) # OK
NT5(abc="", _1="") # OK
NT6 = namedtuple("NT6", ["abc", "_d"], rename=True) # OK
NT6(abc="", _1="") # OK
# > The ``namedtuple`` function also supports a ``defaults`` keyword argument that
# > specifies default values for the fields. Type checkers may support this.
NT7 = namedtuple("NT7", "a b c", defaults=(1, 2))
NT7(1) # OK
NT7(1, 2, 3) # OK
NT7() # E: too few arguments