Skip to content

Bug: C419 autofix removing list within any/all causes undesirable short circuiting #4320

@jamesbraza

Description

@jamesbraza

Please see the below code:

class Worker:
    """Asynchronous worker."""

    def wait(self) -> bool:
        """Wait for worker thread to join."""
        # Code wraps threading.Event.wait()


def wait_all(*workers: Worker) -> bool:
    return all([w.wait() for w in workers])  # List stops short circuit

With ruff==0.0.265, and running ruff --select=C419 --fix a.py, it removes the list cast:

def wait_all(*workers: Worker) -> bool:
    return all(w.wait() for w in workers)  # List stops short circuit

However, this actually added a subtle bug, because now the all statement will be short-circuited. This short circuiting (in my scenario here) will cause all workers beyond the first w.wait() return of False to not be awaited.

I think C4 autofixing should not be applied within all/any, as these can be short circuited, which can cause undesirable side effects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated 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