Skip to content

Use Union for tuple fallback? #7616

Description

@mthuurne
  • Please insert below the code you are checking with mypy,
from typing import Iterable, Iterator, Union
from typing_extensions import Protocol

class MixedIter(Protocol):
    def __iter__(self) -> Iterator[Union[int, str]]: ...

def f(xs: Iterable[Union[int, str]]) -> str:
    return ''.join(str(x) for x in xs)

def g(xs: MixedIter) -> str:
    return ''.join(str(x) for x in xs)

mixedTuple = (56, 'A')
reveal_type(mixedTuple)
reveal_type(mixedTuple.__iter__)
print(f(mixedTuple))
print(g(mixedTuple))
  • What is the actual behavior/output?
mixedtuple.py:14: note: Revealed type is 'Tuple[builtins.int, builtins.str]'
mixedtuple.py:15: note: Revealed type is 'def () -> typing.Iterator[builtins.object*]'
mixedtuple.py:17: error: Argument 1 to "g" has incompatible type "Tuple[int, str]"; expected "MixedIter"  [arg-type]
  • What is the behavior/output you expect?

I'd expect both f(mixedTuple) and g(mixedTuple) to be accepted without errors.

  • What are the versions of mypy and Python you are using?

mypy 0.730 on Python 3.7.2.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions