Skip to content

Including F841 in the default --fix behaviour is a gigantic footgun #6835

@ssokolow

Description

@ssokolow

I have a habit of writing my code from the top down and saving part-way through to trigger things like MyPy at a time when they won't be distracting. After installing Ruff and integrating it into my Vim+ALE-based setup that triggers everything (including autofix/autoformat) on save (thank you, rustfmt, for bringing my habits into the 21st century), I started getting confusing errors.

After a couple of times, I realized I wasn't at risk of dementia, but that Ruff was deleting assignments that I hadn't used yet... sometimes at the worst possible time for derailing my memory of how I planned things to work.

Beyond that, while it's very much a "do not rely on this!" thing in the Python language spec, because, in practice, CPython only uses garbage collection to break cycles and uses reference counting for non-cyclical data structures, dropping memory does have Rust-style determinism in CPython, it does alter the drop order in the same way it would in Rust, and Hyrum's law does apply...

...which means the fixer can alter the semantics of the program in ways other than "pulled the rug out from under the train of thought". Take a look at how ruff alters the observed behaviour of this code:

class Foo:
    def __init__(self, msg):
        self.msg = msg
    def __del__(self):
        print(self.msg)

def main():
    print("Yo Ho Ho")
    ham = Foo("bottle of rum")
    print("and a")

main()
ssokolow@monolith ~ % python3 demo.py
Yo Ho Ho
and a
bottle of rum
ssokolow@monolith ~ % ruff --fix demo.py
Found 1 error (1 fixed, 0 remaining).
ssokolow@monolith ~ % python3 demo.py   
Yo Ho Ho
bottle of rum
and a

I think that, if --fix support is offered for F841, it shouldn't be part of the set of fixes that's enabled by default when a new user (or their IDE/editor integration plugin) just blindly runs ruff --fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliRelated to the command-line interfacefixesRelated 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