Proposal: flake8-comprehensions - fix issues raised and add to dep list

This is a follow up to Removed unnecessary list() calls on sorted(). by cclauss · Pull Request #17653 · django/django · GitHub which addresses a ruff rule derived from Adam’s own flake8-comprehensions.

I think flake8-comprehensions is great and have started using on most projects now. Seeing as the above PR was accepted, should we address the remaining issues that f8c addresses and add it to the dependency list?

Here are the remaining issues:

django main % (3) flake8 --show-source .
./contrib/gis/management/commands/ogrinspect.py:130:18: C416 Unnecessary list comprehension - rewrite using list().
        output = [s for s in _ogrinspect(ds, model_name, **ogr_options)]
                 ^
./core/management/templates.py:121:32: C417 Unnecessary use of map - use a generator expression instead.
            extra_files.extend(map(lambda x: x.strip(), file.split(",")))
                               ^
./forms/formsets.py:387:23: C419 Unnecessary list comprehension passed to all() prevents short-circuiting - rewrite as a generator.
        forms_valid = all(
                      ^
./forms/formsets.py:579:12: C419 Unnecessary list comprehension passed to all() prevents short-circuiting - rewrite as a generator.
    return all([formset.is_valid() for formset in formsets])
           ^
./template/backends/django.py:116:12: C416 Unnecessary dict comprehension - rewrite using dict().
    return {
           ^
./test/runner.py:1056:36: C401 Unnecessary generator - rewrite as a set comprehension.
        suite.serialized_aliases = set(
                                   ^
./utils/datastructures.py:322:21: C416 Unnecessary dict comprehension - rewrite using dict().
        return repr({key: value for key, value in self._store.values()})

Just saw Mariusz’ response to other flake8-comp fixes here: Fix ruff issues related to comprehensions by cclauss · Pull Request #17649 · django/django · GitHub which was an earlier PR to the one above.

Thanks for this patch :+1:, however we try to avoid trivial code reformatting unless it’s a big problem as it creates extra noise in the history. Also, we don’t use ruff or flake8-comprehensions so noqa comments are unnecessary.

I don’t think it’s worth adding. There are only a few cases and even changing them is incorrect in two cases.

1 Like

Ah yes the hidden comment explains it :+1: