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

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

Closed
jamesbraza opened this issue May 9, 2023 · 3 comments
Labels
fixes Related to suggested fixes for violations

Comments

@jamesbraza
Copy link
Contributor

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.

@charliermarsh charliermarsh added the fixes Related to suggested fixes for violations label May 9, 2023
@charliermarsh
Copy link
Member

Ah yeah, this is intentional. In the future we'll probably qualify this as a suggested, and not an automated fix, since it changes the semantics of the program, but I wouldn't want to remove it entirely, since this is the entire rule and it's generally the correct fix.

@charliermarsh charliermarsh closed this as not planned Won't fix, can't repro, duplicate, stale May 9, 2023
@zanieb
Copy link
Member

zanieb commented May 9, 2023

We can make that change as part of #4181, specifically #4184 which should be ready to work on once #4303 is merged.

@jamesbraza
Copy link
Contributor Author

but I wouldn't want to remove it entirely, since this is the entire rule and it's generally the correct fix.

Just to share my two cents here, I agree removing extra list casts is generally the correct fix, especially with argument unpacking into *args.

I would argue though, when short circuiting can happen, that C4 actually changes the core behavior. Imo autofixes should not be able to change fundamental behaviors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

No branches or pull requests

3 participants