Skip to content

enumerate(Union[Sequence, Sequence]) #8586

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

Open
jbwdevries opened this issue Mar 25, 2020 · 2 comments
Open

enumerate(Union[Sequence, Sequence]) #8586

jbwdevries opened this issue Mar 25, 2020 · 2 comments

Comments

@jbwdevries
Copy link

  • Are you reporting a bug, or opening a feature request?
    Bug
  • Please insert below the code you are checking with mypy.
from typing import Sequence, Union

def baz(idx: int, arg: Union[int, str]) -> None:
    pass

def foo(the_list: Union[Sequence[int], Sequence[str]]) -> None:
    for idx, val in enumerate(the_list):
        reveal_type(idx)
        reveal_type(val)
        baz(idx, val)
  • What is the actual behavior/output?
foo.py:8: note: Revealed type is 'builtins.int'
foo.py:9: note: Revealed type is 'builtins.object*'
foo.py:10: error: Argument 2 to "baz" has incompatible type "object"; expected "Union[int, str]"
  • What is the behavior/output you expect?
foo.py:8: note: Revealed type is 'builtins.int'
foo.py:9: note: Revealed type is 'Union[int, str]'
  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?
    0.761, 0.770 and master all have the same result.
  • What are the mypy flags you are using? (For example --strict-optional)
    --strict
@jbwdevries jbwdevries changed the title enumerate(Union) enumerate(Union[Sequence, Sequence]) Mar 25, 2020
@ilevkivskyi
Copy link
Member

This is actually an expected behavior, but maybe it may sense to special case constraint solver for type variables to use a union instead of join if multiple constraints come from union items.

@qthequartermasterman
Copy link

I believe I'm seeing a related, but not identical issue when typeddicts are involved. The union type is correctly inferred when iterating over the list directly, but when iterating over enumerate, it appears to be inferred as the intersection of the types.

from typing_extensions import TypedDict, NotRequired

class A(TypedDict):
    attr_a: str
    attr_common: NotRequired[float]

class B(TypedDict):
    attr_b: int
    attr_common: NotRequired[float]

union_of_lists: list[A] | list[B] = []

reveal_type(union_of_lists)  # Revealed type is "list[A] | list[B]", expected: list[A] | list[B]
for item1 in union_of_lists:
    reveal_type(union_of_lists)  # Revealed type is "list[A] | list[B]", expected: list[A] | list[B]
    reveal_type(item1)  # Revealed type is "A | B", expected A|B

for i, item2 in enumerate(union_of_lists):
    reveal_type(union_of_lists) # Revealed type is "list[A] | list[B]", expected: list[A] | list[B]
    reveal_type(item2)  # Revealed type is "TypedDict({'attr_common'?: builtins.float})", expected A | B

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

No branches or pull requests

4 participants