Skip to content

Suboptimal autofix when combining PLC0415,I001,RUF100 with a long line #20042

Description

@tpgillam

Summary

Using ruff 0.12.10.

Start with the following code in moo.py:

def moo():
    from very.long.nonsense.indeed.need.line.to.be.full.to.cause.breakage import foo

    return foo()

Running ruff with (at least) these three rules enabled, currently PLC0415 triggers:

$ uvx ruff==0.12.10 check --select PLC0415,I001,RUF100 moo.py
moo.py:2:5: PLC0415 `import` should be at the top-level of a file

Because there are good reasons that the import is local, we disable (in practice, this disable is done in an editor via the ruff language server's suggested code action):

def moo():
    from very.long.nonsense.indeed.need.line.to.be.full.to.cause.breakage import foo  # noqa: PLC0415

    return foo()

but now the line being too long causes I001 to trigger:

$ uvx ruff==0.12.10 check --select PLC0415,I001,RUF100 moo.py
moo.py:2:5: I001 [*] Import block is un-sorted or un-formatted

but if we trigger the autofix with RUF100 enabled, then what the user sees is that the noqa they added disappears, and we get back to:

def moo():
    from very.long.nonsense.indeed.need.line.to.be.full.to.cause.breakage import foo

    return foo()

Fixing without RUF100, we can see that we get this intermediate state:

def moo():
    from very.long.nonsense.indeed.need.line.to.be.full.to.cause.breakage import (
        foo,  # noqa: PLC0415
    )

    return foo()

... but the # noqa: PLC0415 is now the line below where it needs to be, so RUF100's autofix will remove it.

Version

0.12.10

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    isortRelated to import sortingsuppressionRelated to supression of violations e.g. noqa

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions