Skip to content

overload with tuples of different arity #5302

Closed
@wabu

Description

@wabu

When using overload, I realized that it wrongly does not complain about type mismatches as soon as one defines overloaded versions with Tuple of different arites. Here comes a minimal example:

from typing import Tuple, TypeVar, overload, List, Any

T = TypeVar('T')
_1 = TypeVar('_1')
_2 = TypeVar('_2')
_3 = TypeVar('_3')

@overload
def foo(bar: List[T], baz: List[T]) -> List[T]:
    pass

@overload
def foo(bar: Tuple[_1, _2], baz: Tuple[_1, _2]) -> Tuple[_1, _2]:
    pass

@overload
def foo(bar: Tuple[_1, _2, _3], baz: Tuple[_1, _2, _3]) -> Tuple[_1, _3]:
    pass

def foo(bar: Any, baz: Any) -> Any:
    return (bar[0], baz[-1])

# good call to foo
d: Tuple[int, str] = foo((1, 'a'), (2, 'a'))

# should fails, but currently only fails if Tuple[_1, _2, _3] overload is removed
e: Tuple[int, str] = foo((1, 'a'), (2, 3)) 
  • mypy 0.610
  • Python 3.6.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions