Skip to content

PERF401 new preview fixes invalidly hoists extend to list compre #14362

@Skylion007

Description

@Skylion007

I tried running the new autofixes on the PyTorch codebase and was mostly impressed, but found one really annoying (and handable edge case).

Here is an example bad fix

     @dist_init
     def test_wait_all_with_exception(self):
-        futs = []
+        futs = [rpc.rpc_async(dst, raise_func) for _ in range(10)]
         dst = worker_name((self.rank + 1) % self.world_size)
-        for _ in range(10):
-            futs.append(rpc.rpc_async(dst, raise_func))

         with self.assertRaisesRegex(ValueError, "Expected error"):
             torch.futures.wait_all(futs)

Here is an example diff generated by ruff. Note that list comprehensions uses dst even though dst is first defined on the line underneath the list comprehension. Ruff can already detect this because it immeaditely created a bunch of ruff F821 errors as soon as the fixes were applied. It would be good not to hoist the forloop from an extend to a list comprehensions if there are any variables needed for the list comprehension defined or mutated in anyway. I was kind of surprised given that it did properly not hoist the function if there were any comments in between the list definition and the loop.

ruff 0.7.4
ruff check --select=PERF401 --fix --unsafe-fixes --preview

FYI @w0nder1ng

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions