isort icon indicating copy to clipboard operation
isort copied to clipboard

Import in function with comments are messed up

Open bigbirdcode opened this issue 2 years ago • 0 comments

I found a bug with isort. I have a function with import inside with comments and blank lines. Seems that all these are needed for the problem to happen. The example is a bit stupid and I know there are better ways to do that, but still the problem makes the code wrong.

isort version: 5.12.0

isort config:

[tool.isort]
lines_before_imports = 2
lines_after_imports = 2

Example code:

import sys


def fast():
    print("in fast")


def slow():
    # This is a slow function
    # import is slow that's why it is here
    import logging

    # here we use that
    logging.warning("in slow")


def main():
    if sys.platform == "win32":
        slow()
    else:
        fast()


if __name__ == "__main__":
    main()

After isort the slow() function becomes like this:

def slow():
    # This is a slow function
    # import is slow that's why it is here
    # here we use that
    logging.warning("in slow")
    import logging

This line order change is clearly wrong.

bigbirdcode avatar Feb 14 '23 13:02 bigbirdcode