| 1 |
-- Semantic analysis of named tuples |
| 2 |
|
| 3 |
[case testSimpleNamedtuple] |
| 4 |
from collections import namedtuple |
| 5 |
N = namedtuple('N', ['a']) |
| 6 |
def f() -> N: pass |
| 7 |
[builtins fixtures/tuple.pyi] |
| 8 |
[out] |
| 9 |
MypyFile:1( |
| 10 |
ImportFrom:1(collections, [namedtuple]) |
| 11 |
AssignmentStmt:2( |
| 12 |
NameExpr(N* [__main__.N]) |
| 13 |
NamedTupleExpr:2(N, tuple[Any])) |
| 14 |
FuncDef:3( |
| 15 |
f |
| 16 |
def () -> tuple[Any, fallback=__main__.N] |
| 17 |
Block:3( |
| 18 |
PassStmt:3()))) |
| 19 |
|
| 20 |
[case testTwoItemNamedtuple] |
| 21 |
from collections import namedtuple |
| 22 |
N = namedtuple('N', ['a', 'xyz']) |
| 23 |
def f() -> N: pass |
| 24 |
[builtins fixtures/tuple.pyi] |
| 25 |
[out] |
| 26 |
MypyFile:1( |
| 27 |
ImportFrom:1(collections, [namedtuple]) |
| 28 |
AssignmentStmt:2( |
| 29 |
NameExpr(N* [__main__.N]) |
| 30 |
NamedTupleExpr:2(N, tuple[Any, Any])) |
| 31 |
FuncDef:3( |
| 32 |
f |
| 33 |
def () -> tuple[Any, Any, fallback=__main__.N] |
| 34 |
Block:3( |
| 35 |
PassStmt:3()))) |
| 36 |
|
| 37 |
[case testTwoItemNamedtupleWithTupleFieldNames] |
| 38 |
from collections import namedtuple |
| 39 |
N = namedtuple('N', ('a', 'xyz')) |
| 40 |
def f() -> N: pass |
| 41 |
[builtins fixtures/tuple.pyi] |
| 42 |
[out] |
| 43 |
MypyFile:1( |
| 44 |
ImportFrom:1(collections, [namedtuple]) |
| 45 |
AssignmentStmt:2( |
| 46 |
NameExpr(N* [__main__.N]) |
| 47 |
NamedTupleExpr:2(N, tuple[Any, Any])) |
| 48 |
FuncDef:3( |
| 49 |
f |
| 50 |
def () -> tuple[Any, Any, fallback=__main__.N] |
| 51 |
Block:3( |
| 52 |
PassStmt:3()))) |
| 53 |
|
| 54 |
[case testTwoItemNamedtupleWithShorthandSyntax] |
| 55 |
from collections import namedtuple |
| 56 |
N = namedtuple('N', ' a xyz ') |
| 57 |
def f() -> N: pass |
| 58 |
[builtins fixtures/tuple.pyi] |
| 59 |
[out] |
| 60 |
MypyFile:1( |
| 61 |
ImportFrom:1(collections, [namedtuple]) |
| 62 |
AssignmentStmt:2( |
| 63 |
NameExpr(N* [__main__.N]) |
| 64 |
NamedTupleExpr:2(N, tuple[Any, Any])) |
| 65 |
FuncDef:3( |
| 66 |
f |
| 67 |
def () -> tuple[Any, Any, fallback=__main__.N] |
| 68 |
Block:3( |
| 69 |
PassStmt:3()))) |
| 70 |
|
| 71 |
[case testNamedTupleWithItemTypes] |
| 72 |
from typing import NamedTuple |
| 73 |
N = NamedTuple('N', [('a', int), |
| 74 |
('b', str)]) |
| 75 |
[builtins fixtures/tuple.pyi] |
| 76 |
[out] |
| 77 |
MypyFile:1( |
| 78 |
ImportFrom:1(typing, [NamedTuple]) |
| 79 |
AssignmentStmt:2( |
| 80 |
NameExpr(N* [__main__.N]) |
| 81 |
NamedTupleExpr:2(N, tuple[builtins.int, builtins.str]))) |
| 82 |
|
| 83 |
[case testNamedTupleWithTupleFieldNamesWithItemTypes] |
| 84 |
from typing import NamedTuple |
| 85 |
N = NamedTuple('N', (('a', int), |
| 86 |
('b', str))) |
| 87 |
[builtins fixtures/tuple.pyi] |
| 88 |
[out] |
| 89 |
MypyFile:1( |
| 90 |
ImportFrom:1(typing, [NamedTuple]) |
| 91 |
AssignmentStmt:2( |
| 92 |
NameExpr(N* [__main__.N]) |
| 93 |
NamedTupleExpr:2(N, tuple[builtins.int, builtins.str]))) |
| 94 |
|
| 95 |
[case testNamedTupleBaseClass] |
| 96 |
from collections import namedtuple |
| 97 |
N = namedtuple('N', ['x']) |
| 98 |
class A(N): pass |
| 99 |
[builtins fixtures/tuple.pyi] |
| 100 |
[out] |
| 101 |
MypyFile:1( |
| 102 |
ImportFrom:1(collections, [namedtuple]) |
| 103 |
AssignmentStmt:2( |
| 104 |
NameExpr(N* [__main__.N]) |
| 105 |
NamedTupleExpr:2(N, tuple[Any])) |
| 106 |
ClassDef:3( |
| 107 |
A |
| 108 |
TupleType( |
| 109 |
tuple[Any, fallback=__main__.N]) |
| 110 |
BaseType( |
| 111 |
__main__.N) |
| 112 |
PassStmt:3())) |
| 113 |
|
| 114 |
[case testNamedTupleBaseClass2] |
| 115 |
from collections import namedtuple |
| 116 |
class A(namedtuple('N', ['x'])): pass |
| 117 |
[builtins fixtures/tuple.pyi] |
| 118 |
[out] |
| 119 |
MypyFile:1( |
| 120 |
ImportFrom:1(collections, [namedtuple]) |
| 121 |
ClassDef:2( |
| 122 |
A |
| 123 |
TupleType( |
| 124 |
tuple[Any, fallback=__main__.A@base1]) |
| 125 |
BaseType( |
| 126 |
__main__.A@base1) |
| 127 |
PassStmt:2())) |
| 128 |
|
| 129 |
[case testNamedTupleBaseClassWithItemTypes] |
| 130 |
from typing import NamedTuple |
| 131 |
class A(NamedTuple('N', [('x', int)])): pass |
| 132 |
[builtins fixtures/tuple.pyi] |
| 133 |
[out] |
| 134 |
MypyFile:1( |
| 135 |
ImportFrom:1(typing, [NamedTuple]) |
| 136 |
ClassDef:2( |
| 137 |
A |
| 138 |
TupleType( |
| 139 |
tuple[builtins.int, fallback=__main__.A@base1]) |
| 140 |
BaseType( |
| 141 |
__main__.A@base1) |
| 142 |
PassStmt:2())) |
| 143 |
|
| 144 |
-- Errors |
| 145 |
|
| 146 |
[case testNamedTupleWithTooFewArguments] |
| 147 |
from collections import namedtuple |
| 148 |
N = namedtuple('N') # E: Too few arguments for "namedtuple()" |
| 149 |
[builtins fixtures/tuple.pyi] |
| 150 |
|
| 151 |
[case testNamedTupleWithInvalidName] |
| 152 |
from collections import namedtuple |
| 153 |
N = namedtuple(1, ['x']) # E: "namedtuple()" expects a string literal as the first argument |
| 154 |
[builtins fixtures/tuple.pyi] |
| 155 |
|
| 156 |
[case testNamedTupleWithInvalidItems] |
| 157 |
from collections import namedtuple |
| 158 |
N = namedtuple('N', 1) # E: List or tuple literal expected as the second argument to "namedtuple()" |
| 159 |
[builtins fixtures/tuple.pyi] |
| 160 |
|
| 161 |
[case testNamedTupleWithInvalidItems2] |
| 162 |
from collections import namedtuple |
| 163 |
N = namedtuple('N', ['x', 1]) # E: String literal expected as "namedtuple()" item |
| 164 |
[builtins fixtures/tuple.pyi] |
| 165 |
|
| 166 |
[case testNamedTupleWithUnderscoreItemName] |
| 167 |
from collections import namedtuple |
| 168 |
N = namedtuple('N', ['_fallback']) # E: "namedtuple()" field name "_fallback" starts with an underscore |
| 169 |
[builtins fixtures/tuple.pyi] |
| 170 |
|
| 171 |
-- NOTE: The following code works at runtime but is not yet supported by mypy. |
| 172 |
-- Keyword arguments may potentially be supported in the future. |
| 173 |
[case testNamedTupleWithNonpositionalArgs] |
| 174 |
from collections import namedtuple |
| 175 |
N = namedtuple(typename='N', field_names=['x']) # E: Unexpected arguments to "namedtuple()" |
| 176 |
[builtins fixtures/tuple.pyi] |
| 177 |
|
| 178 |
[case testTypingNamedTupleWithTooFewArguments] |
| 179 |
from typing import NamedTuple |
| 180 |
N = NamedTuple('N') # E: Too few arguments for "NamedTuple()" |
| 181 |
[builtins fixtures/tuple.pyi] |
| 182 |
|
| 183 |
[case testTypingNamedTupleWithManyArguments] |
| 184 |
from typing import NamedTuple |
| 185 |
N = NamedTuple('N', [], []) # E: Too many arguments for "NamedTuple()" |
| 186 |
[builtins fixtures/tuple.pyi] |
| 187 |
|
| 188 |
[case testTypingNamedTupleWithInvalidName] |
| 189 |
from typing import NamedTuple |
| 190 |
N = NamedTuple(1, ['x']) # E: "NamedTuple()" expects a string literal as the first argument |
| 191 |
[builtins fixtures/tuple.pyi] |
| 192 |
|
| 193 |
[case testTypingNamedTupleWithInvalidItems] |
| 194 |
from typing import NamedTuple |
| 195 |
N = NamedTuple('N', 1) # E: List or tuple literal expected as the second argument to "NamedTuple()" |
| 196 |
[builtins fixtures/tuple.pyi] |
| 197 |
|
| 198 |
[case testTypingNamedTupleWithUnderscoreItemName] |
| 199 |
from typing import NamedTuple |
| 200 |
N = NamedTuple('N', [('_fallback', int)]) # E: "NamedTuple()" field name "_fallback" starts with an underscore |
| 201 |
[builtins fixtures/tuple.pyi] |
| 202 |
|
| 203 |
[case testTypingNamedTupleWithUnexpectedNames] |
| 204 |
from typing import NamedTuple |
| 205 |
N = NamedTuple(name='N', fields=[]) # E: Unexpected arguments to "NamedTuple()" |
| 206 |
[builtins fixtures/tuple.pyi] |
| 207 |
|
| 208 |
-- NOTE: The following code works at runtime but is not yet supported by mypy. |
| 209 |
-- Keyword arguments may potentially be supported in the future. |
| 210 |
[case testNamedTupleWithNonpositionalArgs2] |
| 211 |
from collections import namedtuple |
| 212 |
N = namedtuple(typename='N', field_names=['x']) # E: Unexpected arguments to "namedtuple()" |
| 213 |
[builtins fixtures/tuple.pyi] |
| 214 |
|
| 215 |
[case testInvalidNamedTupleBaseClass] |
| 216 |
from typing import NamedTuple |
| 217 |
class A(NamedTuple('N', [1])): pass # E: Tuple expected as "NamedTuple()" field |
| 218 |
class B(A): pass |
| 219 |
[builtins fixtures/tuple.pyi] |
| 220 |
|
| 221 |
[case testInvalidNamedTupleBaseClass2] |
| 222 |
|
| 223 |
class A(NamedTuple('N', [1])): pass |
| 224 |
class B(A): pass |
| 225 |
[out] |
| 226 |
main:2: error: Unsupported dynamic base class "NamedTuple" |
| 227 |
main:2: error: Name "NamedTuple" is not defined |
| 228 |
|
| 229 |
[case testNamedTupleWithDecorator] |
| 230 |
from typing import final, NamedTuple |
| 231 |
|
| 232 |
@final |
| 233 |
class A(NamedTuple("N", [("x", int)])): |
| 234 |
pass |
| 235 |
[builtins fixtures/tuple.pyi] |
| 236 |
[out] |
| 237 |
MypyFile:1( |
| 238 |
ImportFrom:1(typing, [final, NamedTuple]) |
| 239 |
ClassDef:4( |
| 240 |
A |
| 241 |
TupleType( |
| 242 |
tuple[builtins.int, fallback=__main__.A@base1]) |
| 243 |
Decorators( |
| 244 |
NameExpr(final [typing.final])) |
| 245 |
BaseType( |
| 246 |
__main__.A@base1) |
| 247 |
PassStmt:5())) |