Skip to content

Commit c7906f8

Browse files
committed
add conformance tests for namedtuple spec changes
1 parent 5a9bc52 commit c7906f8

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

conformance/tests/namedtuples_define_class.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,33 @@ class Point(NamedTuple):
4949
p10 = Point(1, 2, "", other="") # E
5050

5151

52+
# > Fields must be annotated attributes - methods and un-annotated attributes are not
53+
# > considered fields.
54+
55+
56+
class Point2(NamedTuple):
57+
x: int
58+
y: int
59+
units = "meters" # Not a field
60+
61+
def is_origin(self) -> int: # Not a field
62+
return self.x == 0 and self.y == 0
63+
64+
65+
p11 = Point2(1, 2)
66+
assert_type(p11, Point2)
67+
x, y = p11
68+
69+
p12 = Point2(1, 2, "") # E
70+
71+
72+
# > Field names may not start with an underscore.
73+
74+
class Point3(NamedTuple):
75+
x: int
76+
_y: int # E: illegal field name
77+
78+
5279
# > The runtime implementation of ``NamedTuple`` enforces that fields with default
5380
# > values must come after fields without default values. Type checkers should
5481
# > likewise enforce this restriction::

conformance/tests/namedtuples_define_functional.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,21 @@
4343
p6_4 = Point6(x=1.1, y=2) # E
4444

4545

46-
# > At runtime, the ``namedtuple`` function disallows field names that are
47-
# > illegal Python identifiers and either raises an exception or replaces these
48-
# > fields with a parameter name of the form ``_N``. The behavior depends on
49-
# > the value of the ``rename`` argument. Type checkers may replicate this
50-
# > behavior statically.
46+
# > At runtime, the ``namedtuple`` function disallows field names that begin with
47+
# > an underscore or are illegal Python identifiers, and either raises an exception
48+
# > or replaces these fields with a parameter name of the form ``_N``. The behavior
49+
# > depends on the value of the ``rename`` argument. Type checkers may replicate
50+
# > this behavior statically.
5151

5252
NT1 = namedtuple("NT1", ["a", "a"]) # E?: duplicate field name
5353
NT2 = namedtuple("NT2", ["abc", "def"]) # E?: illegal field name
5454
NT3 = namedtuple("NT3", ["abc", "def"], rename=False) # E?: illegal field name
55+
NT4 = namedtuple("NT4", ["abc", "_d"], rename=False) # E?: illegal field name
5556

56-
NT4 = namedtuple("NT4", ["abc", "def"], rename=True) # OK
57-
NT4(abc="", _1="") # OK
57+
NT5 = namedtuple("NT5", ["abc", "def"], rename=True) # OK
58+
NT5(abc="", _1="") # OK
59+
NT6 = namedtuple("NT6", ["abc", "_d"], rename=True) # OK
60+
NT6(abc="", _1="") # OK
5861

5962

6063
# > The ``namedtuple`` function also supports a ``defaults`` keyword argument that

0 commit comments

Comments
 (0)