Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix meet of a broken named tuple and a good named tuple
  • Loading branch information
JukkaL committed Mar 20, 2017
commit e3ff2c7864b63cfe6f415b7396ca535d8813cb75
4 changes: 4 additions & 0 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ def visit_tuple_type(self, t: TupleType) -> Type:
elif (isinstance(self.s, Instance) and
self.s.type.fullname() == 'builtins.tuple' and self.s.args):
return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
elif (isinstance(self.s, Instance) and t.fallback.type == self.s.type):
# Uh oh, a broken named tuple type (https://github.com/python/mypy/issues/3016).
# Do something reasonable until that bug is fixed.
return t
else:
return self.default(self.s)

Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def test_tuples(self) -> None:

self.assert_meet(self.tuple(self.fx.a, self.fx.a),
self.fx.std_tuple,
NoneTyp())
self.tuple(self.fx.a, self.fx.a))
self.assert_meet(self.tuple(self.fx.a),
self.tuple(self.fx.a, self.fx.a),
NoneTyp())
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,25 @@ from typing import NamedTuple
def f() -> None:
A = NamedTuple('A', [('x', int)])
A # E: Name 'A' is not defined

[case testNamedTupleWithImportCycle]
import a
[file a.py]
from collections import namedtuple
from b import f

N = namedtuple('N', 'a')

class X(N): pass
[file b.py]
import a

def f(x: a.X) -> None:
# The type of x is broken (https://github.com/python/mypy/issues/3016) but we need to
# do something reasonable here to avoid a regression.
reveal_type(x)
x = a.X(1)
reveal_type(x)
[out]
tmp/b.py:6: error: Revealed type is 'a.X'
tmp/b.py:8: error: Revealed type is 'Tuple[Any, fallback=a.X]'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding another test that just uses N instead of X everywhere.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work because of #3054.