8000 Mypy doesn't infer enumerate argument properly · Issue #8141 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Mypy doesn't infer enumerate argument properly #8141

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

Closed
calebho opened this issue Dec 13, 2019 · 3 comments
Closed

Mypy doesn't infer enumerate argument properly #8141

calebho opened this issue Dec 13, 2019 · 3 comments

Comments

@calebho
Copy link
calebho commented Dec 13, 2019

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?
    Reporting a bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import *
from itertools import product
import random

# error
a = range(2) if random.random() < .5 else product(range(2), range(2))
enumerate(a)

# ok if you give an explicit annotation
b: Union[Iterable[int], Iterable[Tuple[int, int]]] = range(2) if random.random() < .5 else product(range(2), range(2))
enumerate(b)
  • What is the actual behavior/output?
main.py:7: error: Argument 1 to "enumerate" has incompatible type "object"; expected "Iterable[<nothing>]"
  • What is the behavior/output you expect?
    No error

For version information + flags, see this playground example https://mypy-play.net/?mypy=latest&python=3.6&gist=16fc66a21fa582f9431188040f1c6bda

@calebho
Copy link
Author
calebho commented Dec 13, 2019

I have a feeling this doesn't really depend on enumerate, but rather how the type of an if ... else expression is inferred, but I'm not 100% sure

@TH3CHARLie
Copy link
Collaborator

try reveal_type(a) in your code and yes it is actually because of the type of the conditional expr

@calebho
Copy link
Author
calebho commented Dec 13, 2019

Cool looks like it's being tracked in #8074 already

@calebho calebho closed this as completed Dec 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0