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

Remove unpacking check from C416 #205

Merged
merged 1 commit into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
History
=======

* Remove the tuple/unpacking check from C416 to prevent false positives where
the type of the iterable is changed from some iterable to a tuple.

3.1.3 (2019-11-19)
------------------

Expand Down
4 changes: 0 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,4 @@ It's unnecessary to use a list comprehension if the elements are unchanged. The
iterable should be wrapped in ``list()`` or ``set()`` instead. For example:

* Rewrite ``[x for x in iterable]`` as ``list(iterable)``
* Rewrite ``[(x, y) for x, y in iterable]`` as ``list(iterable)``
* Rewrite ``[(x, y) for (x, y) in iterable]`` as ``list(iterable)``
* Rewrite ``{x for x in iterable}`` as ``set(iterable)``
* Rewrite ``{(x, y) for x, y in iterable}`` as ``set(iterable)``
* Rewrite ``{(x, y) for (x, y) in iterable}`` as ``set(iterable)``
24 changes: 3 additions & 21 deletions src/flake8_comprehensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,27 +288,9 @@ def run(self):
and not node.generators[0].ifs
and not is_async_generator(node.generators[0])
and (
(
isinstance(node.elt, ast.Name)
and isinstance(node.generators[0].target, ast.Name)
and node.elt.id == node.generators[0].target.id
)
or (
isinstance(node.elt, ast.Tuple)
and isinstance(node.generators[0].target, ast.Tuple)
and (
len(node.elt.elts)
== len(node.generators[0].target.elts)
)
and all(
isinstance(a, ast.Name)
and isinstance(b, ast.Name)
and a.id == b.id
for a, b in zip(
node.elt.elts, node.generators[0].target.elts
)
)
)
isinstance(node.elt, ast.Name)
and isinstance(node.generators[0].target, ast.Name)
and node.elt.id == node.generators[0].target.id
)
):
lookup = {ast.ListComp: "list", ast.SetComp: "set"}
Expand Down
26 changes: 16 additions & 10 deletions tests/test_flake8_comprehensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,29 +964,35 @@ def test_C416_pass_4_tuples(flake8dir):
assert result.out_lines == []


def test_C416_fail_1(flake8dir):
def test_C416_fail_5_unpacking(flake8dir):
# We can't assume unpacking came from tuples, so these examples should pass
flake8dir.make_example_py(
"""
[x for x in range(5)]
[(x, y) for x, y in zip('abc', '123')]
[(x, y) for (x, y) in zip('abc', '123')]
{x for x in range(5)}
{(x, y) for x, y in zip('abc', '123')}
{(x, y) for (x, y) in zip('abc', '123')}
"""
)
result = flake8dir.run_flake8()
assert result.out_lines == []


def test_C416_fail_1_list(flake8dir):
flake8dir.make_example_py("[x for x in range(5)]")
result = flake8dir.run_flake8()
# Column offset for list comprehensions was incorrect in Python < 3.8.
# See https://bugs.python.org/issue31241 for details.
col_offset = 1 if sys.version_info >= (3, 8) else 2
assert result.out_lines == [
"./example.py:1:%d: C416 Unnecessary list comprehension - rewrite using list()."
% col_offset,
"./example.py:2:%d: C416 Unnecessary list comprehension - rewrite using list()."
% col_offset,
"./example.py:3:%d: C416 Unnecessary list comprehension - rewrite using list()."
% col_offset,
"./example.py:4:1: C416 Unnecessary set comprehension - rewrite using set().",
"./example.py:5:1: C416 Unnecessary set comprehension - rewrite using set().",
"./example.py:6:1: C416 Unnecessary set comprehension - rewrite using set().",
]


def test_C416_fail_2_set(flake8dir):
flake8dir.make_example_py("{x for x in range(5)}")
result = flake8dir.run_flake8()
assert result.out_lines == [
"./example.py:1:1: C416 Unnecessary set comprehension - rewrite using set().",
]