| -- 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__.A@base1]) | |
| BaseType( | |
| __main__.A@base1) | |
| 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__.A@base1]) | |
| BaseType( | |
| __main__.A@base1) | |
| 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__.A@base1]) | |
| Decorators( | |
| NameExpr(final [typing.final])) | |
| BaseType( | |
| __main__.A@base1) | |
| PassStmt:5())) |