hhatto / autopep8

A tool that automatically formats Python code to conform to the PEP 8 style guide.
https://pypi.org/project/autopep8/
MIT License
4.56k stars 290 forks source link

Skip formatting block's on top import don't work for distributed imports #628

Closed ridilculous closed 5 months ago

ridilculous commented 2 years ago

Python Code

Having

# fmt: off
from os import chdir
# fmt: on

test = False

import configargparse

or

# fmt: off
from os import chdir
# fmt: on

def test():
    pass

import configargparse

or

# fmt: off
import os
# fmt: on

test = False

import configargparse

or similar, formatting moves imports which are spread over the code into the region where formatting is disabled.

# fmt: off
import os; os.cpu_count()
# fmt: on

import configargparse

test = False
def test():
    pass

works find though and doesn't move import configargparse into the block which is disabled for formatting.

Expectation

I am looking for a way to pin one specific import to the top of the source without having to rename it and was hoping that, altough the imports are grouped at the beginning of the file, the region with the disabled formatting would be preserved and all other imports grouped below it.

In my specific case, the import will look like

# fmt: off
import os; some_function_invocation_here()
# fmt: on

Environment

hhatto commented 5 months ago

Thanks for suggestion.

works find though and doesn't move import configargparse into the block which is disabled for formatting.

First, the E401 error does not exist, so this behavior is correct.

$ cat valid_one.py
# fmt: off
import os; os.cpu_count()
# fmt: on

import configargparse

test = False
def test():
    pass

$ pycodestyle valid_one.py
valid_one.py:2:10: E702 multiple statements on one line (semicolon)
valid_one.py:8:1: E302 expected 2 blank lines, found 0

$ autopep8 --version
autopep8 1.6.0 (pycodestyle: 2.8.0)

I am looking for a way to pin one specific import to the top of the source without having to rename it and was hoping that, altough the imports are grouped at the beginning of the file, the region with the disabled formatting would be preserved and all other imports grouped below it.

At this time, we have no idea to support this behavior.