Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer constraints against tuple fallbacks #4087

Merged
merged 4 commits into from Nov 15, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions mypy/constraints.py
Expand Up @@ -373,12 +373,8 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
cb = infer_constraints(template.args[0], item, SUPERTYPE_OF)
res.extend(cb)
return res
elif (isinstance(actual, TupleType) and template.type.is_protocol and
self.direction == SUPERTYPE_OF):
if mypy.subtypes.is_subtype(actual.fallback, erase_typevars(template)):
res.extend(infer_constraints(template, actual.fallback, self.direction))
return res
return []
elif isinstance(actual, TupleType) and self.direction == SUPERTYPE_OF:
return infer_constraints(template, actual.fallback, self.direction)
else:
return []

Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-tuples.test
Expand Up @@ -941,6 +941,21 @@ reveal_type(t[x:]) # E: Revealed type is 'Union[builtins.int, builtins.str]'
t[y:] # E: Slice index must be an integer or None
[builtins fixtures/tuple.pyi]

[case testInferTupleTypeFallbackAgainstInstance]
from typing import TypeVar, Generic, Tuple
T = TypeVar('T')

class Base(Generic[T]): pass
def f(x: Base[T]) -> T: pass

class DT(Tuple[str, str], Base[int]):
pass

reveal_type(f(DT())) # E: Revealed type is 'builtins.int*'

[builtins fixtures/tuple.pyi]
[out]

[case testTypeTupleClassmethod]
from typing import Tuple, Type

Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/pythoneval.test
Expand Up @@ -1418,6 +1418,17 @@ async def main() -> None:
_testAsyncioGatherPreciseType.py:9: error: Revealed type is 'builtins.str'
_testAsyncioGatherPreciseType.py:10: error: Revealed type is 'builtins.str'

[case testMultipleInheritanceWorksWithTupleTypeGeneric]
from typing import SupportsAbs, NamedTuple

class Point(NamedTuple('Point', [('x', int), ('y', int)]), SupportsAbs[int]):
def __abs__(p) -> int:
return abs(p.x) + abs(p.y)

def test(a: Point) -> bool:
return abs(a) == 2
[out]

[case testNoCrashOnGenericUnionUnpacking]
from typing import Union, Dict

Expand Down