Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,11 @@ def lvalue_type_from_base(self, expr_node: Var,

if base_type:
if not has_no_typevars(base_type):
# TODO: Handle TupleType, don't cast
instance = cast(Instance, self.scope.active_self_type())
self_type = self.scope.active_self_type()
if isinstance(self_type, TupleType):
instance = self_type.fallback
else:
instance = self_type
itype = map_instance_to_supertype(instance, base)
base_type = expand_type_by_instance(base_type, itype)

Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ b = B._make(['']) # type: B

[builtins fixtures/list.pyi]

[case testNamedTupleIncompatibleRedefinition]
from typing import NamedTuple
class Crash(NamedTuple):
count: int # E: Incompatible types in assignment (expression has type "int", base class "tuple" defined the type as Callable[[Tuple[Any, ...], Any], int])
[builtins fixtures/tuple.pyi]

[case testNamedTupleInClassNamespace]
# https://github.com/python/mypy/pull/2553#issuecomment-266474341
from typing import NamedTuple
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/fixtures/tuple.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Builtins stub used in tuple-related test cases.

from typing import Iterable, Iterator, TypeVar, Generic, Sequence
from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any

Tco = TypeVar('Tco', covariant=True)

Expand All @@ -13,6 +13,7 @@ class type:
class tuple(Sequence[Tco], Generic[Tco]):
def __iter__(self) -> Iterator[Tco]: pass
def __getitem__(self, x: int) -> Tco: pass
def count(self, obj: Any) -> int: pass
class function: pass

# We need int and slice for indexing tuples.
Expand Down