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

isort.core.process incorrectly returns False if only changes to lines before imports were made, with no changes to raw import section #2242

Open
TheVayt opened this issue Mar 2, 2024 · 0 comments

Comments

@TheVayt
Copy link

TheVayt commented Mar 2, 2024

I found a bug where isort.core.process returns False when only changes to lines before imports were made, with no changes to raw import section.

isort.core.process:

...Returns True if there were changes that needed to be made (errors present) from what
was provided in the input_stream, otherwise False.

The bug results in isort not fixing files that require fixing only lines before imports.

For example isort file.py --lines-before-imports=10 does nothing for this file:

from a import b, x

foo = 'bar'

But this one will have 10 lines added before imports, just because imports are unsorted and isort.core.process returned True:

from a import x, b

foo = 'bar'

Example tests, 2 of which fail:

import isort

ln = '\n'

input_0 = "from a import b, x\n\nfoo = 'bar'\n"
input_1 = ln + input_0
input_0_unsorted = "from a import x, b\n\nfoo = 'bar'\n"
input_1_unsorted = ln + input_0_unsorted

# isort.code returns sorted code
# isort.check_code checks if code is correct
# Both functions call isort.core.process internally but isort.check_code gives us it's return value

assert isort.code(input_0) == input_0
assert isort.check_code(input_0)
assert isort.code(input_0, lines_before_imports=0) == input_0
assert isort.check_code(input_0, lines_before_imports=0)
assert isort.code(input_0, lines_before_imports=1) == input_1
assert not isort.check_code(input_0, lines_before_imports=1)  # AssertionError

assert isort.code(input_1) == input_1
assert isort.check_code(input_1)
assert isort.code(input_1, lines_before_imports=0) == input_0
assert not isort.check_code(input_1, lines_before_imports=0)  # AssertionError
assert isort.code(input_1, lines_before_imports=1) == input_1
assert isort.check_code(input_1, lines_before_imports=1)

assert isort.code(input_0_unsorted) == input_0
assert not isort.check_code(input_0_unsorted)
assert isort.code(input_0_unsorted, lines_before_imports=0) == input_0
assert not isort.check_code(input_0_unsorted, lines_before_imports=0)
assert isort.code(input_0_unsorted, lines_before_imports=1) == input_1
assert not isort.check_code(input_0_unsorted, lines_before_imports=1)

assert isort.code(input_1_unsorted) == input_1
assert not isort.check_code(input_1_unsorted)
assert isort.code(input_1_unsorted, lines_before_imports=0) == input_0
assert not isort.check_code(input_1_unsorted, lines_before_imports=0)
assert isort.code(input_1_unsorted, lines_before_imports=1) == input_1
assert not isort.check_code(input_1_unsorted, lines_before_imports=1)
@TheVayt TheVayt changed the title isort.core.process returns False if only changes to lines before imports were made, with no changes to raw import section isort.core.process incorrectly returns False if only changes to lines before imports were made, with no changes to raw import section Mar 4, 2024
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