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

Non-idempotency with commented multiline imports #2232

Open
dpoznik opened this issue Feb 14, 2024 · 0 comments
Open

Non-idempotency with commented multiline imports #2232

dpoznik opened this issue Feb 14, 2024 · 0 comments

Comments

@dpoznik
Copy link
Contributor

dpoznik commented Feb 14, 2024

The following example illustrates a non-idempotency in isort==5.13.2:

from some.very.long.subpackage.path.defining.classes.to.be.imported import (
    SomeLengthyClassName1,  # noqa F401
    SomeLengthyClassName2,  # noqa F401
)

Running isort over this file changes it:

$ isort foo.py 
Fixing /path/to/foo.py
$ cat foo.py 
from some.very.long.subpackage.path.defining.classes.to.be.imported import (  # noqa F401
    SomeLengthyClassName1,
)
from some.very.long.subpackage.path.defining.classes.to.be.imported import (  # noqa F401
    SomeLengthyClassName2,
)

Then, running isort over the result changes it a second time:

$ isort foo.py 
Fixing /path/to/foo.py
$ cat foo.py 
from some.very.long.subpackage.path.defining.classes.to.be.imported import (  # noqa F401
    SomeLengthyClassName1,
    SomeLengthyClassName2,
)

I (and my editor) prefer the original version, as it enables applying the # noqa directives to individual imported objects, but the final version would be preferable to the intermediate version.

The following example illustrates some more unexpected behavior:

from some.very.long.subpackage.path.defining.classes.to.be.imported import (
    SomeLengthyClassName1,  # noqa F401
    SomeLengthyClassName2,
    SomeLengthyClassName3,  # noqa F401
)

Applying isort once results an in an unsorted file (SomeLengthyClassName3 before SomeLengthyClassName2):

$ isort foo.py 
Fixing /path/to/foo.py
$ cat foo.py 
from some.very.long.subpackage.path.defining.classes.to.be.imported import (  # noqa F401
    SomeLengthyClassName1,
)
from some.very.long.subpackage.path.defining.classes.to.be.imported import (  # noqa F401
    SomeLengthyClassName3,
)
from some.very.long.subpackage.path.defining.classes.to.be.imported import (
    SomeLengthyClassName2,
)

Then, applying isort a second time attaches a # noqa directive to an object that didn't have one in the original (i.e., SomeLengthyClassName2):

$ isort foo.py 
Fixing /path/to/foo.py
$ cat foo.py 
from some.very.long.subpackage.path.defining.classes.to.be.imported import (  # noqa F401
    SomeLengthyClassName1,
    SomeLengthyClassName2,
    SomeLengthyClassName3,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant