lyz-code / autoimport

Autoimport automatically fixes wrong import statements.
https://lyz-code.github.io/autoimport
GNU General Public License v3.0
97 stars 24 forks source link

Extra blank lines after imports are removed #219

Closed ghost closed 2 years ago

ghost commented 2 years ago

Description

autoimport removes extra blank lines after imports, even when it’s required for PEP 8 compliance (e.g. when imports are directly followed by a class or a def). And also I like doing lines_after_imports = 2 in isort for consistency anyway.

While you may say I can just run another auto-formatter afterwards to fix the issue, this actually makes it impossible to use autoimport as a pre-commit hook, as on the next run autoimport will remove the blank line again, and pre-commit requires all hooks to succeed.

Steps to reproduce

  1. Create a file with the following contents:

    import random
    
    def foo():
        print(random.random())
  2. Run autoimport foo.py

Current behavior

The file gets rewritten to this:

    import random

    def foo():
        print(random.random())

Desired behavior

The file should remain untouched.

Environment

           autoimport version: 1.2.2
               python version: 3.10.4 (main, Apr  4 2022, 14:54:08) [GCC 10.2.1 20210110]
                     platform: Linux-5.10.0-16-amd64-x86_64-with-glibc2.31
lyz-code commented 2 years ago

Hi @nyuszika7h thanks for taking the time to open the issue. I see your point and agree it should be fixed.

Sadly I don't have time to implement this on my own, but if anyone is interested in contributing I think the easier way to do it is by implementing some logic when the different parts of the code are joined so that if the self.code starts with def or class it should add a new \n

lyz-code commented 2 years ago

Available since 1.3.1