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

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

Closed
tdulcet opened this issue Jun 9, 2024 · 1 comment · Fixed by #11830
Closed
Assignees
Labels
bug Something isn't working

Comments

@tdulcet
Copy link

tdulcet commented Jun 9, 2024

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

@charliermarsh charliermarsh added the bug Something isn't working label Jun 9, 2024
@charliermarsh
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants