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.58k stars 291 forks source link

Block autopep off/on does not work for imports #770

Open KubaO opened 1 month ago

KubaO commented 1 month ago

autopep8 off/on doesn't work

Before formatting:

# autopep8: off
import sys
sys.dont_write_bytecode = True
# autopep8: on
import locale

After:

# autopep8: off
import locale
import sys
sys.dont_write_bytecode = True
# autopep8: on

The workaround is to add a semicolon and join the import line with the non-import line(s):

# autopep8: off
import sys; sys.dont_write_bytecode = True
# autopep8: on
import locale

Originally posted by @PlatonB in https://github.com/hhatto/autopep8/issues/460#issuecomment-1593174293

Chris3606 commented 1 month ago

Similar issues appear to happen in docstrings. Something like this:

class A:
    # autopep8: off
    """Docstring

    .. code-block::python
      print(1)

      print (2)

    """
    # autopep8: on

autopep8 still insists on removing whitespace on the blank lines within the code example. This actually generates warnings/errors in documentation generators link Sphinx, and I don't have a workaround at the moment.