[case testNewNamedTupleNoUnderscoreFields] from typing import NamedTuple class X(NamedTuple): x: int _y: int # E: NamedTuple field name cannot start with an underscore: _y _z: int # E: NamedTuple field name cannot start with an underscore: _z [builtins fixtures/tuple.pyi] [case testNewNamedTupleAccessingAttributes] from typing import NamedTuple class X(NamedTuple): x: int y: str x: X x.x x.y x.z # E: "X" has no attribute "z" [builtins fixtures/tuple.pyi] [case testNewNamedTupleAttributesAreReadOnly] from typing import NamedTuple class X(NamedTuple): x: int x: X x.x = 5 # E: Property "x" defined in "X" is read-only x.y = 5 # E: "X" has no attribute "y" class A(X): pass a: A a.x = 5 # E: Property "x" defined in "X" is read-only [builtins fixtures/tuple.pyi] [case testNewNamedTupleCreateWithPositionalArguments] from typing import NamedTuple class X(NamedTuple): x: int y: str x = X(1, '2') x.x x.z # E: "X" has no attribute "z" x = X(1) # E: Missing positional argument "y" in call to "X" x = X(1, '2', 3) # E: Too many arguments for "X" [builtins fixtures/tuple.pyi] [case testNewNamedTupleShouldBeSingleBase] from typing import NamedTuple class A: ... class X(NamedTuple, A): # E: NamedTuple should be a single base pass [builtins fixtures/tuple.pyi] [case testCreateNewNamedTupleWithKeywordArguments] from typing import NamedTuple class X(NamedTuple): x: int y: str x = X(x=1, y='x') x = X(1, y='x') x = X(x=1, z=1) # E: Unexpected keyword argument "z" for "X" x = X(y='x') # E: Missing positional argument "x" in call to "X" [builtins fixtures/tuple.pyi] [case testNewNamedTupleCreateAndUseAsTuple] from typing import NamedTuple class X(NamedTuple): x: int y: str x = X(1, 'x') a, b = x a, b, c = x # E: Need more than 2 values to unpack (3 expected) [builtins fixtures/tuple.pyi] [case testNewNamedTupleWithItemTypes] from typing import NamedTuple class N(NamedTuple): a: int b: str n = N(1, 'x') s: str = n.a # E: Incompatible types in assignment (expression has type "int", \ variable has type "str") i: int = n.b # E: Incompatible types in assignment (expression has type "str", \ variable has type "int") x, y = n if int(): x = y # E: Incompatible types in assignment (expression has type "str", variable has type "int") [builtins fixtures/tuple.pyi] [case testNewNamedTupleConstructorArgumentTypes] from typing import NamedTuple class N(NamedTuple): a: int b: str n = N('x', 'x') # E: Argument 1 to "N" has incompatible type "str"; expected "int" n = N(1, b=2) # E: Argument "b" to "N" has incompatible type "int"; expected "str" N(1, 'x') N(b='x', a=1) [builtins fixtures/tuple.pyi] [case testNewNamedTupleAsBaseClass] from typing import NamedTuple class N(NamedTuple): a: int b: str class X(N): pass x = X(1, 2) # E: Argument 2 to "X" has incompatible type "int"; expected "str" s = '' i = 0 if int(): s = x.a # E: Incompatible types in assignment (expression has type "int", variable has type "str") if int(): i, s = x if int(): s, s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") [builtins fixtures/tuple.pyi] [case testNewNamedTupleSelfTypeWithNamedTupleAsBase] from typing import NamedTuple class A(NamedTuple): a: int b: str class B(A): def f(self, x: int) -> None: self.f(self.a) self.f(self.b) # E: Argument 1 to "f" of "B" has incompatible type "str"; expected "int" i = 0 s = '' if int(): i, s = self i, i = self # E: Incompatible types in assignment (expression has type "str", \ variable has type "int") [builtins fixtures/tuple.pyi] [out] [case testNewNamedTupleTypeReferenceToClassDerivedFrom] from typing import NamedTuple class A(NamedTuple): a: int b: str class B(A): def f(self, x: 'B') -> None: i = 0 s = '' if int(): self = x i, s = x i, s = x.a, x.b i, s = x.a, x.a # E: Incompatible types in assignment (expression has type "int", \ variable has type "str") i, i = self # E: Incompatible types in assignment (expression has type "str", \ variable has type "int") [builtins fixtures/tuple.pyi] [case testNewNamedTupleSubtyping] from typing import NamedTuple, Tuple class A(NamedTuple): a: int b: str class B(A): pass a = A(1, '') b = B(1, '') t: Tuple[int, str] if int(): b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B") if int(): a = t # E: Incompatible types in assignment (expression has type "tuple[int, str]", variable has type "A") if int(): b = t # E: Incompatible types in assignment (expression has type "tuple[int, str]", variable has type "B") if int(): t = a if int(): t = (1, '') if int(): t = b if int(): a = b [builtins fixtures/tuple.pyi] [case testNewNamedTupleSimpleTypeInference] from typing import NamedTuple, Tuple class A(NamedTuple): a: int l = [A(1), A(2)] a = A(1) a = l[0] (i,) = l[0] i, i = l[0] # E: Need more than 1 value to unpack (2 expected) l = [A(1)] a = (1,) # E: Incompatible types in assignment (expression has type "tuple[int]", \ variable has type "A") [builtins fixtures/list.pyi] [case testNewNamedTupleMissingClassAttribute] from typing import NamedTuple class MyNamedTuple(NamedTuple): a: int b: str MyNamedTuple.x # E: "type[MyNamedTuple]" has no attribute "x" [builtins fixtures/tuple.pyi] [case testNewNamedTupleEmptyItems] from typing import NamedTuple class A(NamedTuple): ... [builtins fixtures/tuple.pyi] [case testNewNamedTupleForwardRef] from typing import NamedTuple class A(NamedTuple): b: 'B' class B: ... a = A(B()) a = A(1) # E: Argument 1 to "A" has incompatible type "int"; expected "B" [builtins fixtures/tuple.pyi] [case testNewNamedTupleProperty36] from typing import NamedTuple class A(NamedTuple): a: int class B(A): @property def b(self) -> int: return self.a class C(B): pass B(1).b C(2).b [builtins fixtures/property.pyi] [case testNewNamedTupleAsDict] from typing import NamedTuple, Any class X(NamedTuple): x: Any y: Any x: X reveal_type(x._asdict()) # N: Revealed type is "builtins.dict[builtins.str, Any]" [builtins fixtures/dict.pyi] [case testNewNamedTupleReplaceTyped] from typing import NamedTuple class X(NamedTuple): x: int y: str x: X reveal_type(x._replace()) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.X]" x._replace(x=5) x._replace(y=5) # E: Argument "y" to "_replace" of "X" has incompatible type "int"; expected "str" [builtins fixtures/tuple.pyi] [case testNewNamedTupleFields] from typing import NamedTuple class X(NamedTuple): x: int y: str reveal_type(X._fields) # N: Revealed type is "tuple[builtins.str, builtins.str]" reveal_type(X._field_types) # N: Revealed type is "builtins.dict[builtins.str, Any]" reveal_type(X._field_defaults) # N: Revealed type is "builtins.dict[builtins.str, Any]" # In typeshed's stub for builtins.pyi, __annotations__ is `dict[str, Any]`, # but it's inferred as `Mapping[str, object]` here due to the fixture we're using reveal_type(X.__annotations__) # N: Revealed type is "typing.Mapping[builtins.str, builtins.object]" [builtins fixtures/dict-full.pyi] [case testNewNamedTupleUnit] from typing import NamedTuple class X(NamedTuple): pass x: X = X() x._replace() x._fields[0] # E: Tuple index out of range [builtins fixtures/tuple.pyi] [case testNewNamedTupleJoinNamedTuple] from typing import NamedTuple class X(NamedTuple): x: int y: str class Y(NamedTuple): x: int y: str reveal_type([X(3, 'b'), Y(1, 'a')]) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.str]]" [builtins fixtures/list.pyi] [case testNewNamedTupleJoinTuple] from typing import NamedTuple class X(NamedTuple): x: int y: str reveal_type([(3, 'b'), X(1, 'a')]) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.str]]" reveal_type([X(1, 'a'), (3, 'b')]) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.str]]" [builtins fixtures/list.pyi] [case testNewNamedTupleWithTooManyArguments] from typing import NamedTuple class X(NamedTuple): x: int y = z = 2 # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" def f(self): pass [builtins fixtures/tuple.pyi] [case testNewNamedTupleWithInvalidItems2] import typing class X(typing.NamedTuple): x: int y = 1 # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" x.x: int # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" z: str = 'z' aa: int # E: Non-default NamedTuple fields cannot follow default fields [builtins fixtures/list.pyi] [case testNewNamedTupleWithoutTypesSpecified] from typing import NamedTuple class X(NamedTuple): x: int y = 2 # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" [builtins fixtures/tuple.pyi] [case testTypeUsingTypeCNamedTuple] from typing import NamedTuple, Type class N(NamedTuple): x: int y: str def f(a: Type[N]): a() # E: Missing positional arguments "x", "y" in call to "N" [builtins fixtures/list.pyi] [case testNewNamedTupleWithDefaults] from typing import List, NamedTuple, Optional class X(NamedTuple): x: int y: int = 2 reveal_type(X(1)) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.X]" reveal_type(X(1, 2)) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.X]" X(1, 'a') # E: Argument 2 to "X" has incompatible type "str"; expected "int" X(1, z=3) # E: Unexpected keyword argument "z" for "X" class HasNone(NamedTuple): x: int y: Optional[int] = None reveal_type(HasNone(1)) # N: Revealed type is "tuple[builtins.int, builtins.int | None, fallback=__main__.HasNone]" class Parameterized(NamedTuple): x: int y: List[int] = [1] + [2] z: List[int] = [] reveal_type(Parameterized(1)) # N: Revealed type is "tuple[builtins.int, builtins.list[builtins.int], builtins.list[builtins.int], fallback=__main__.Parameterized]" Parameterized(1, ['not an int']) # E: List item 0 has incompatible type "str"; expected "int" class Default: pass class UserDefined(NamedTuple): x: Default = Default() reveal_type(UserDefined()) # N: Revealed type is "tuple[__main__.Default, fallback=__main__.UserDefined]" reveal_type(UserDefined(Default())) # N: Revealed type is "tuple[__main__.Default, fallback=__main__.UserDefined]" UserDefined(1) # E: Argument 1 to "UserDefined" has incompatible type "int"; expected "Default" [builtins fixtures/list.pyi] [case testNewNamedTupleWithDefaultsStrictOptional] from typing import List, NamedTuple, Optional class HasNone(NamedTuple): x: int y: Optional[int] = None reveal_type(HasNone(1)) # N: Revealed type is "tuple[builtins.int, builtins.int | None, fallback=__main__.HasNone]" HasNone(None) # E: Argument 1 to "HasNone" has incompatible type "None"; expected "int" HasNone(1, y=None) HasNone(1, y=2) class CannotBeNone(NamedTuple): x: int y: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int") [builtins fixtures/list.pyi] [case testNewNamedTupleWrongType] from typing import NamedTuple class X(NamedTuple): x: int y: int = 'not an int' # E: Incompatible types in assignment (expression has type "str", variable has type "int") [builtins fixtures/tuple.pyi] [case testNewNamedTupleErrorInDefault] from typing import NamedTuple class X(NamedTuple): x: int = 1 + '1' # E: Unsupported left operand type for + ("int") [builtins fixtures/tuple.pyi] [case testNewNamedTupleInheritance] from typing import NamedTuple class X(NamedTuple): x: str y: int = 3 class Y(X): def method(self) -> str: self.y return self.x reveal_type(Y('a')) # N: Revealed type is "tuple[builtins.str, builtins.int, fallback=__main__.Y]" Y(y=1, x='1').method() class CallsBaseInit(X): def __init__(self, x: str) -> None: super().__init__(x) # E: Too many arguments for "__init__" of "object" [builtins fixtures/tuple.pyi] [case testNewNamedTupleWithMethods] from typing import NamedTuple class XMeth(NamedTuple): x: int def double(self) -> int: return self.x async def asyncdouble(self) -> int: return self.x class XRepr(NamedTuple): x: int y: int = 1 def __str__(self) -> str: return 'string' def __sub__(self, other: XRepr) -> int: return 0 reveal_type(XMeth(1).double()) # N: Revealed type is "builtins.int" _ = reveal_type(XMeth(1).asyncdouble()) # N: Revealed type is "typing.Coroutine[Any, Any, builtins.int]" reveal_type(XMeth(42).x) # N: Revealed type is "builtins.int" reveal_type(XRepr(42).__str__()) # N: Revealed type is "builtins.str" reveal_type(XRepr(1, 2).__sub__(XRepr(3))) # N: Revealed type is "builtins.int" [typing fixtures/typing-async.pyi] [builtins fixtures/tuple.pyi] [case testNewNamedTupleOverloading] from typing import NamedTuple, overload class Overloader(NamedTuple): x: int @overload def method(self, y: str) -> str: pass @overload def method(self, y: int) -> int: pass def method(self, y): return y reveal_type(Overloader(1).method('string')) # N: Revealed type is "builtins.str" reveal_type(Overloader(1).method(1)) # N: Revealed type is "builtins.int" Overloader(1).method(('tuple',)) # E: No overload variant of "method" of "Overloader" matches argument type "tuple[str]" \ # N: Possible overload variants: \ # N: def method(self, y: str) -> str \ # N: def method(self, y: int) -> int [builtins fixtures/tuple.pyi] [case testNewNamedTupleMethodInheritance] from typing import NamedTuple, TypeVar T = TypeVar('T') class Base(NamedTuple): x: int def copy(self: T) -> T: reveal_type(self) # N: Revealed type is "T`-1" return self def good_override(self) -> int: reveal_type(self) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Base]" reveal_type(self[0]) # N: Revealed type is "builtins.int" self[0] = 3 # E: Unsupported target for indexed assignment ("Base") reveal_type(self.x) # N: Revealed type is "builtins.int" self.x = 3 # E: Property "x" defined in "Base" is read-only self[1] # E: Tuple index out of range reveal_type(self[T]) # N: Revealed type is "builtins.int" \ # E: No overload variant of "__getitem__" of "tuple" matches argument type "TypeVar" \ # N: Possible overload variants: \ # N: def __getitem__(self, int, /) -> int \ # N: def __getitem__(self, slice[int | None], /) -> tuple[int, ...] return self.x def bad_override(self) -> int: return self.x class Child(Base): def new_method(self) -> int: reveal_type(self) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Child]" reveal_type(self[0]) # N: Revealed type is "builtins.int" self[0] = 3 # E: Unsupported target for indexed assignment ("Child") reveal_type(self.x) # N: Revealed type is "builtins.int" self.x = 3 # E: Property "x" defined in "Base" is read-only self[1] # E: Tuple index out of range return self.x def good_override(self) -> int: return 0 def bad_override(self) -> str: # E: Return type "str" of "bad_override" incompatible with return type "int" in supertype "Base" return 'incompatible' def takes_base(base: Base) -> int: return base.x reveal_type(Base(1).copy()) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Base]" reveal_type(Child(1).copy()) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Child]" reveal_type(Base(1).good_override()) # N: Revealed type is "builtins.int" reveal_type(Child(1).good_override()) # N: Revealed type is "builtins.int" reveal_type(Base(1).bad_override()) # N: Revealed type is "builtins.int" reveal_type(takes_base(Base(1))) # N: Revealed type is "builtins.int" reveal_type(takes_base(Child(1))) # N: Revealed type is "builtins.int" [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] [case testNewNamedTupleIllegalNames] from typing import Callable, NamedTuple class XMethBad(NamedTuple): x: int def _fields(self): # E: Cannot overwrite NamedTuple attribute "_fields" return 'no chance for this' class MagicalFields(NamedTuple): x: int def __slots__(self) -> None: pass # E: Cannot overwrite NamedTuple attribute "__slots__" def __new__(cls) -> MagicalFields: pass # E: Cannot overwrite NamedTuple attribute "__new__" def _source(self) -> int: pass # E: Cannot overwrite NamedTuple attribute "_source" __annotations__ = {'x': float} # E: NamedTuple field name cannot start with an underscore: __annotations__ \ # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" \ # E: Cannot overwrite NamedTuple attribute "__annotations__" class AnnotationsAsAMethod(NamedTuple): x: int # This fails at runtime because typing.py assumes that __annotations__ is a dictionary. def __annotations__(self) -> float: # E: Cannot overwrite NamedTuple attribute "__annotations__" return 1.0 class ReuseNames(NamedTuple): x: int def x(self) -> str: # E: Name "x" already defined on line 22 return '' def y(self) -> int: return 0 y: str # E: Name "y" already defined on line 26 class ReuseCallableNamed(NamedTuple): z: Callable[[ReuseNames], int] def z(self) -> int: # E: Name "z" already defined on line 31 return 0 [builtins fixtures/dict.pyi] [case testNewNamedTupleDocString] from typing import NamedTuple class Documented(NamedTuple): """This is a docstring.""" x: int reveal_type(Documented.__doc__) # N: Revealed type is "builtins.str" reveal_type(Documented(1).x) # N: Revealed type is "builtins.int" class BadDoc(NamedTuple): x: int def __doc__(self) -> str: return '' reveal_type(BadDoc(1).__doc__()) # N: Revealed type is "builtins.str" [builtins fixtures/tuple.pyi] [case testNewNamedTupleClassMethod] from typing import NamedTuple class HasClassMethod(NamedTuple): x: str @classmethod def new(cls, f: str) -> 'HasClassMethod': reveal_type(cls) # N: Revealed type is "type[tuple[builtins.str, fallback=__main__.HasClassMethod]]" reveal_type(HasClassMethod) # N: Revealed type is "def (x: builtins.str) -> tuple[builtins.str, fallback=__main__.HasClassMethod]" return cls(x=f) [builtins fixtures/classmethod.pyi] [case testNewNamedTupleStaticMethod] from typing import NamedTuple class HasStaticMethod(NamedTuple): x: str @staticmethod def new(f: str) -> 'HasStaticMethod': return HasStaticMethod(x=f) [builtins fixtures/classmethod.pyi] [case testNewNamedTupleProperty] from typing import NamedTuple class HasStaticMethod(NamedTuple): x: str @property def size(self) -> int: reveal_type(self) # N: Revealed type is "tuple[builtins.str, fallback=__main__.HasStaticMethod]" return 4 [builtins fixtures/property.pyi] [case testTypingExtensionsNamedTuple] from typing_extensions import NamedTuple class Point(NamedTuple): x: int y: int bad_point = Point('foo') # E: Missing positional argument "y" in call to "Point" \ # E: Argument 1 to "Point" has incompatible type "str"; expected "int" point = Point(1, 2) x, y = point x = point.x reveal_type(x) # N: Revealed type is "builtins.int" reveal_type(y) # N: Revealed type is "builtins.int" point.y = 6 # E: Property "y" defined in "Point" is read-only [builtins fixtures/tuple.pyi]