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.57k stars 290 forks source link

Handling E402 at .py file with notebook style block comment #635

Open yasirroni opened 2 years ago

yasirroni commented 2 years ago

Is there option to not fully ignore E402, but partially ignore it the file use notebook style block of code (supported to run as block on VSCode). One of the most common style is # %%.

isort solve it using treat_comments_as_code = ["# %%"] as explained in https://github.com/PyCQA/isort/issues/1338 and here.

Some user might want that autopep did not fix E402 outside block comment if the parameter used, but fix it as long as inside one block.


Python Code

# %%
import numpy as np # should not be fixed because import already on top of the block and file

...

# %%
...

# %%
import os  # should not be fixed because import already on top of the block
import sys

# %%
...  # should be fixed, because import not on top

import os
import sys

Command Line and Configuration

pyproject.toml

[tool.autopep8]
in-place = true
recursive = true
verbose = true
max_line_length = 147
ignore = ["E402"]
aggressive = 3

Command Line

$ pre-commit run

Your Environment