Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Changelog
Unreleased
----------

* Fix a C414 false positive for nested ``sorted()`` calls where the outer call
specifies a non-``None`` ``key``, including potentially through a ``**``
expansion.

Fixes `Issue #661 <https://github.com/adamchainz/flake8-comprehensions/issues/661>`__.

* Support Python 3.15.

* Switch package build backend from setuptools to `uv_build <https://docs.astral.sh/uv/concepts/build-backend/>`__.
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ C414: Unnecessary ``<list/reversed/set/sorted/tuple>`` call within ``<list/set/s
--------------------------------------------------------------------------------------------------

It's unnecessary to double-cast or double-process iterables by wrapping the listed functions within ``list``/``set``/``sorted``/``tuple``.
One exception is a nested ``sorted()`` call where the outer call has a non-``None`` ``key``, including potentially through a ``**`` expansion, since the stable sorts can establish primary and secondary orderings.
For example:

* Rewrite ``list(list(iterable))`` as ``list(iterable)``
Expand Down
15 changes: 15 additions & 0 deletions src/flake8_comprehensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@ def run(self) -> Generator[tuple[int, int, str, type[Any]]]:
node.func.id in {"set", "sorted"}
and node.args[0].func.id
in {"list", "reversed", "sorted", "tuple"}
and not (
node.func.id == "sorted"
and node.args[0].func.id == "sorted"
and (
has_double_star_args(node)
or any(
keyword.arg == "key"
and not (
isinstance(keyword.value, ast.Constant)
and keyword.value.value is None
)
for keyword in node.keywords
)
)
)
)
or (
node.func.id in {"list", "tuple"}
Expand Down
7 changes: 7 additions & 0 deletions tests/test_flake8_comprehensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,9 @@ def test_C413_fail(code, failures, flake8_path):
"list(set(a))",
"tuple(set(a))",
"sorted(set(a))",
"sorted(sorted(a), key=len)",
"sorted(sorted(a), **kwargs)",
'sorted(sorted(a), **{"key": len})',
],
)
def test_C414_pass(code, flake8_path):
Expand Down Expand Up @@ -674,6 +677,10 @@ def test_C414_pass(code, flake8_path):
"sorted(sorted(a), reverse=True)",
["./example.py:1:1: C414 Unnecessary sorted call within sorted()."],
),
(
"sorted(sorted(a), key=None)",
["./example.py:1:1: C414 Unnecessary sorted call within sorted()."],
),
(
"sorted(sorted(a, reverse=True))",
["./example.py:1:1: C414 Unnecessary sorted call within sorted()."],
Expand Down