psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
39.2k stars 2.48k forks source link

Comments between fmt:on/off are reformatted #1245

Open marcelm opened 4 years ago

marcelm commented 4 years ago

This code:

# fmt: off
 #nospace
 #  twospaces
# fmt: on

is changed to:

# fmt: off
# nospace
#  twospaces
# fmt: on

I expected the lines betwen # fmt: off and # fmt: on to remain unchanged.

Playground (master)

steve-biggs-fox commented 4 years ago

This bug is affecting me too as my python code contains supercomputer queue manager options that must be formatted like "#PBS (option)" with no space between "#" and "PBS". As a workaround, a print statement within the "# fmt: off/on" block makes it work but ideally blocks of comments only within a "# fmt: off/on" block with no executable code in the block should still respect the "# fmt: off/on" directive.

JelleZijlstra commented 3 years ago

An interesting case from a duplicate:

class A:
    ........

    minutes_available_per_week = models.PositiveIntegerField(
        # fmt: off
        default=settings.TIME_ESTIMATES["defaults"]["minutes_available_per_week"]  # type: ignore
        # fmt: on
    )
petergthatsme commented 2 years ago

I was just wondering if there were any updates related to this bug report. I also find this bug problematic. My usecase is the following: I have some code that needs to be temporarily commented out (say for a few days), while other code in the file is added/modified. Using black then reformat the commented code, which completely looses its indentation, making it useless even after removing the comments. The workaround is to add, say, pass somewhere next to the commented code, but it would be great if that wasn't required. thanks.

aljabadi commented 2 years ago

Thanks for creating the best opinionated formatter I've used.

I'm having this issue with setup.cfg file:

# fmt: off
[metadata]
name = appname
description = a project
long_description = file: README.md
long_description_content_type = text/markdown
charset=UTF-8
author = Al J Abadi
author_email =foo@company.com
classifiers =
    Development Status :: 2 - Pre-Alpha 
    Environment :: Console
    Environment :: MacOS X
    Intended Audience :: Developers
    Natural Language :: English
    Operating System :: POSIX :: Linux
    Operating System :: MacOS :: MacOS X
    Programming Language :: Python :: 3.7
    Programming Language :: Python :: 3.8
    Programming Language :: Python :: 3.9
    Topic :: Scientific/Engineering :: Data Science

[options]
zip_safe = False
packages = find:
platforms = any
include_package_data = True
install_requires =
    bz2file
python_requires = >=3.7
setup_requires =
    setuptools_scm

[bdist_wheel]
universal = 1
# fmt: on

The indentation is required for a successful build but it is removed by black. I was surprised this issue still persists after such a long time.

felix-hilden commented 2 years ago

@aljabadi I don't think we format CFG files 🤔

SomeoneSerge commented 2 years ago

Another use-case that's affected:

#!/usr/bin/env python3
# fmt: off
#SBATCH --job-name="Example"
#SBATCH --array=0-250
# ...
# fmt: on

Black alters #SBATCH into # SBATCH. The former is interpreted by SLURM, the latter ignored

EDIT: already reported in https://github.com/psf/black/issues/1024

randolf-scholz commented 1 year ago

Same issue here: Playground link

Input

rawdata_schema = {
    # fmt: off
    "No"      : "uint16[pyarrow]",
    "year"    : "uint16[pyarrow]",
    "month"   : "uint8[pyarrow]",
    "day"     : "uint8[pyarrow]",
    "hour"    : "uint8[pyarrow]",
    "PM2.5"   : "float32[pyarrow]",
    "PM10"    : "float32[pyarrow]",
    "SO2"     : "float32[pyarrow]",
    "NO2"     : "float32[pyarrow]",
    "CO"      : "float32[pyarrow]",
    "O3"      : "float32[pyarrow]",
    "TEMP"    : "float32[pyarrow]",
    "PRES"    : "float32[pyarrow]",
    "DEWP"    : "float32[pyarrow]",
    "RAIN"    : "float32[pyarrow]",
    "wd"      : "string",  # FIXME bug in pandas prevents using pyarrrow here.
    "WSPM"    : "float32[pyarrow]",
    "station" : "string",  # FIXME bug in pandas prevents using pyarrrow here.
    # fmt: on
}

Output

rawdata_schema = {
    # fmt: off
    "No"      : "uint16[pyarrow]",
    "year"    : "uint16[pyarrow]",
    "month"   : "uint8[pyarrow]",
    "day"     : "uint8[pyarrow]",
    "hour"    : "uint8[pyarrow]",
    "PM2.5"   : "float32[pyarrow]",
    "PM10"    : "float32[pyarrow]",
    "SO2"     : "float32[pyarrow]",
    "NO2"     : "float32[pyarrow]",
    "CO"      : "float32[pyarrow]",
    "O3"      : "float32[pyarrow]",
    "TEMP"    : "float32[pyarrow]",
    "PRES"    : "float32[pyarrow]",
    "DEWP"    : "float32[pyarrow]",
    "RAIN"    : "float32[pyarrow]",
    "wd"      : "string",  # FIXME bug in pandas prevents using pyarrrow here.
    "WSPM"    : "float32[pyarrow]",
    "station" : "string",
    # FIXME bug in pandas prevents using pyarrrow here.
    # fmt: on
}