You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Black格式化工具未识别#fmt: skip指令问题求助

问题:Black忽略# fmt: skip注释仍格式化代码行

使用Black v22.10.0版本,在pyproject.toml中配置line-length=120,执行命令black --config "pyproject.toml" --target-version py39 --check --diff .时,带有# fmt: skip注释的代码行仍被标记需要格式化。

环境配置

pyproject.toml内容

[tool.black]

# How many characters per line to allow.
line-length = 120

# When processing Jupyter Notebooks, add the given magic to the list of known 
# python-magics (timeit, prun, capture, pypy, python3, python, time). 
# Useful for formatting cells with custom python magics.
# python-cell-magics = 

# Require a specific version of Black to be running 
# (useful for unifying results across many environments e.g. with a pyproject.toml file). 
# It can be either a major version number or an exact version.
# required-version = 

# A regular expression that matches files and directories that should be 
# included on recursive searches. An empty value means all files are included 
# regardless of the name. Use forward slashes for directories on all platforms (Windows, too). 
# Exclusions are calculated first, inclusions later.
# include = "(\.pyi?|\.ipynb)$"

# A regular expression that matches files and directories that should be 
# excluded on recursive searches. An empty value means no paths are excluded. 
# Use forward slashes for directories on all platforms (Windows, too). 
# Exclusions are calculated first, inclusions later.
# exclude = "/(\.direnv|\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|\.svn|\.ipynb_checkpoints|_build|buck-out|build|dist|__pypackages__)/"

# Like 'exclude', but adds additional files and directories on top of the excluded ones.
# (Useful if you simply want to add to the default).
# extend-exclude = 

# Like 'exclude', but files and directories matching this regex will be excluded 
# even when they are passed explicitly as arguments.
# force-exclude = 

# The name of the file when passing it through stdin. 
# Useful to make sure Black will respect 'force-exclude' option on some editors that rely on using stdin.
# stdin-filename = 

# Number of parallel workers. 
# Can be a number or a range.
# workers = 

被标记的代码行

ave_quantity = self.exec_math(math_iterable["mean"], "mean", [])  # execute the "mean" fxn on the dataset  # cspell: disable-line # fmt: skip

Black输出的diff结果

--- properties/datasets/models.py    2022-11-30 00:01:16.590743 +0000
+++ properties/datasets/models.py    2022-11-30 00:01:18.692767 +0000
@@ -746,11 +746,13 @@
         calculate the mean value of all the dataset points

         return: numerical value of this function when all variables are zero
         rtype: float
         """
-        ave_quantity = self.exec_math(math_iterable["mean"], "mean", [])  # execute the "mean" fxn on the dataset # fmt:skip
+        ave_quantity = self.exec_math(
+            math_iterable["mean"], "mean", []
+        )  # execute the "mean" fxn on the dataset # fmt:skip
         return getattr(ave_quantity, "magnitude", 0.0)

     def serialize(self, flat=False):
         return {
             "type": "dataset",
would reformat properties/datasets/models.py

Oh no! 💥 💔 💥
1 file would be reformatted, 102 files would be left unchanged.

问题原因与解决方法

原因

Black对# fmt: skip注释的识别有严格要求:

  1. 注释必须严格拼写为# fmt: skip(冒号必须带有空格,不能写成# fmt:skip);
  2. # fmt: skip必须是该行的最后一个注释,不能有其他注释出现在它之后。

你的代码同时触发了这两个问题:一是diff里的注释写成了# fmt:skip(无空格),二是原代码中# fmt: skip后面还有其他注释,导致Black无法正确识别跳过指令。

解决方法

修改代码注释的格式和位置,确保:

  • # fmt: skip是该行的最后一个注释;
  • 严格按照# fmt: skip的格式书写(冒号后加空格)。

示例修改:

# cspell: disable-line
ave_quantity = self.exec_math(math_iterable["mean"], "mean", [])  # execute the "mean" fxn on the dataset  # fmt: skip

或者:

ave_quantity = self.exec_math(math_iterable["mean"], "mean", [])  # execute the "mean" fxn on the dataset  # fmt: skip
# cspell: disable-line

内容的提问来源于stack exchange,提问作者Ricardo de Matos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 10:25:36