Skip to content

FURB140 (reimplemented-starmap) does not handle multiple uses of the loop variable #11810

@tdulcet

Description

@tdulcet

Input code:

def output_table(rows):
    amax = max(len(row) for row in rows)
    for row in rows:
        row.extend("" for _ in range(amax - len(row)))
    lens = [max(strcol(v) for v in col) for col in zip(*rows)]
    print(
        "\n".join(
            "  ".join(
                "{{{0}:<{1}}}".format(i, alen - (strcol(v) - len(v)))
                for i, (alen, v) in enumerate(zip(lens, row))      # << use 1 of `row`
            ).format(*row)                                         # << use 2 of `row`
            for row in rows
        )
    )

Command: ruff check --isolated --select FURB140 --preview --fix example.py
Resulting code after autofix:

from itertools import starmap
# ...
def output_table(rows):
   amax = max(len(row) for row in rows)
   for row in rows:
       row.extend("" for _ in range(amax - len(row)))
   lens = [max(strcol(v) for v in col) for col in zip(*rows)]
   print(
       "\n".join(
           starmap(
               "  ".join(
                   "{{{0}:<{1}}}".format(i, alen - (strcol(v) - len(v)))
                   for i, (alen, v) in enumerate(zip(lens, row))     # << `row` is no longer defined here
               ).format,
               rows,
           )
       )
   )

Note how the original generator comprehension had two uses use of the row loop variable, but the autofix only replaces one of them with the starmap, which breaks the code.

Ruff version: ruff 0.4.8

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions