You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -81,21 +81,52 @@ x = X(1, 2, 3) # E: Too many arguments for "X"
81
81
[case testCreateNamedTupleWithKeywordArguments]
82
82
from collections import namedtuple
83
83
84
-
X = namedtuple('X', ['x', 'y'])
84
+
X = namedtuple('X', 'x y')
85
85
x = X(x=1, y='x')
86
86
x = X(1, y='x')
87
87
x = X(x=1, z=1) # E: Unexpected keyword argument "z" for "X"
88
88
x = X(y=1) # E: Missing positional argument "x" in call to "X"
89
89
90
-
91
90
[case testNamedTupleCreateAndUseAsTuple]
92
91
from collections import namedtuple
93
92
94
-
X = namedtuple('X', ['x', 'y'])
93
+
X = namedtuple('X', 'x y')
95
94
x = X(1, 'x')
96
95
a, b = x
97
96
a, b, c = x # E: Need more than 2 values to unpack (3 expected)
98
97
98
+
[case testNamedTupleAdditionalArgs]
99
+
from collections import namedtuple
100
+
101
+
A = namedtuple('A', 'a b')
102
+
B = namedtuple('B', 'a b', rename=1)
103
+
C = namedtuple('C', 'a b', rename='not a bool')
104
+
D = namedtuple('D', 'a b', unrecognized_arg=False)
105
+
E = namedtuple('E', 'a b', 0)
106
+
107
+
[builtins fixtures/bool.pyi]
108
+
109
+
[out]
110
+
main:5: error: Argument "rename" to "namedtuple" has incompatible type "str"; expected "int"
111
+
main:6: error: Unexpected keyword argument "unrecognized_arg" for "namedtuple"
112
+
<ROOT>/test-data/unit/lib-stub/collections.pyi:3: note: "namedtuple" defined here
113
+
main:7: error: Too many positional arguments for "namedtuple"
114
+
115
+
[case testNamedTupleDefaults]
116
+
# flags: --python-version 3.7
117
+
from collections import namedtuple
118
+
119
+
X = namedtuple('X', ['x', 'y'], defaults=(1,))
120
+
121
+
X() # E: Too few arguments for "X"
122
+
X(0) # ok
123
+
X(0, 1) # ok
124
+
X(0, 1, 2) # E: Too many arguments for "X"
125
+
126
+
Y = namedtuple('Y', ['x', 'y'], defaults=(1, 2, 3)) # E: Too many defaults given in call to namedtuple()
127
+
Z = namedtuple('Z', ['x', 'y'], defaults='not a tuple') # E: Argument "defaults" to "namedtuple" has incompatible type "str"; expected "Optional[Iterable[Any]]" # E: List or tuple literal expected as the defaults argument to namedtuple()
0 commit comments