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

A false positive for C416 #204

Closed
arnimarj opened this issue Nov 20, 2019 · 4 comments · Fixed by #205
Closed

A false positive for C416 #204

arnimarj opened this issue Nov 20, 2019 · 4 comments · Fixed by #205

Comments

@arnimarj
Copy link

arnimarj commented Nov 20, 2019

Hi,

I saw some fixes were committed for C416 false positives before, and I believe I've found a new one.

I was working on some code, which converts a list of 2-element sequences to a list of 2-tuples:

from typing import List, Tuple

a_list_of_2lists: List[List[int]] = [[i, i + 1] for i in range(10)]

a_list_of_2tuples: List[Tuple[int, int]] = [(a, b) for a, b in a_list_of_2lists]

Incidentally, the code passes if I create a 3-tuple instead:

a_list_of_3tuples: List[Tuple[int, int, str]] = [(a, b, 'foobar') for a, b in a_list_of_2lists]

Then I had this idea:

a_list_of_2tuples: List[Tuple[int, int]] = [tuple((a, b)) for a, b in a_list_of_2lists]

but that gave me a C409

Cheers

@arnimarj
Copy link
Author

My current fix is to create a 2-tuple factory function:

def _as_tuple(a: _T, b: _TT) -> Tuple[_T, _TT]:
	return (a, b)

@adamchainz
Copy link
Owner

C416 currently assumes an iterable using unpacking is an iterable of tuples, which your example proves is not always the case. I'll remove the unpacking path of it.

@adamchainz
Copy link
Owner

Fix released in version 3.1.4: https://pypi.org/project/flake8-comprehensions/3.1.4/

@arnimarj
Copy link
Author

That's some seriously impressive turnaround time....many thanks!

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

Successfully merging a pull request may close this issue.

2 participants