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

Multiple assignments to _ with conflicting types in loops and comprehension results in error #4456

Closed
posita opened this issue Jan 10, 2018 · 3 comments

Comments

@posita
Copy link
Contributor

posita commented Jan 10, 2018

The following results in MyPy complaining Incompatible types in assignment (expression has type "unicode", variable has type "int"):

ints_n_txt = [(1, u'1'), (2, u'2'), (3, u'3')]  # type: List[Tuple[int, Text]]
for _, _ in ints_n_txt:  # _ has two different types
    pass

Unsurprisingly, this affects collection comprehension as well:

spam = [True for _, _ in ints_n_txt]  # type: List[bool]

Explicitly declaring the type of _ as Any "fixes" the error for the for loop:

_ = None  # type: Any
for _, _ in …

But that doesn't work for collection comprehension. (MyPy will still complain, and some linters will too. For example, flake8 will complain that F812 - list comprehension redefines '_' from line ….)

These seem to work, but both look a little awkward:

for (
            _,  # type: ignore
            _,  # type: ignore
        ) in ints_n_txt:
    pass

spam = [
    True for (
        _,  # type: ignore
        _,  # type: ignore
    ) in ints_n_txt
]  # type: List[bool]

This may be as intended. I'm wondering if treating _ in all scopes as type Any by default is a bad idea? (Apologies if this is more appropriately filed as an issue with typeshed.)

@ilevkivskyi
Copy link
Member

Yes, this idea appeared before, so this is a duplicate of #465

@posita
Copy link
Contributor Author

posita commented Jan 10, 2018

Oh jeez! Apologies for missing that! 😞

@ilevkivskyi
Copy link
Member

No problem!

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