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