当您的格式化工具和代码检查工具发生冲突时该怎么办?

huangapple go评论150阅读模式
英文:

What to do when your formatter and your linter are fighting

问题

  1. 我一直在Python中编写装饰器
  2. ```python
  3. def dictionary_updater(key: str) -> Callable[[FieldStringer], PayloadSetter]:
  4. """Convert string-converter to dictionary modifier."""
  5. # Create the actual decorator method and return it
  6. def inner(func: FieldStringer) -> PayloadSetter:
  7. # Create the method that should actually be called when the decorated function
  8. # is invoked
  9. def with_dict(self, payload: Payload) -> None:
  10. payload[key] = func(self)
  11. return with_dict
  12. return inner

我遇到的问题是,black 会尝试在文档字符串之后插入空行,我认为这是因为代码的第一行是函数定义。然而,pydocstyle 会抱怨,因为文档字符串和函数体之间不应该有空行。

我尝试分别禁用每个系统的规则,但因为它是一个空行,两个工具似乎都忽略了它。此外,我不能只是禁用工具本身或修改它们的规则,因为它们是一个我无法控制的CI/CD管道的一部分。我想我可以禁用整个文件的一个工具或另一个工具,但我不想这样做,因为这违背了最初引入这些工具的初衷。

有人知道如何解决这个问题吗?

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;ve been writing a decorator in Python:

def dictionary_updater(key: str) -> Callable[[FieldStringer], PayloadSetter]:
"""Convert string-converter to dictionary modifier.
"""
# Create the actual decorator method and return it
def inner(func: FieldStringer) -> PayloadSetter:
# Create the method that should actually be called when the decorated function
# is invoked
def with_dict(self, payload: Payload) -> None:
payload[key] = func(self)

  1. return with_dict
  2. return inner
  1. The issue I&#39;m having is that `black` will try to put an empty line after the docstring, I assume because the first line of code is a function definition. However, `pydocstyle` will complain about this because there&#39;s not supposed to be an empty line between the docstring and the function body.
  2. I&#39;ve tried disabling the rule for each system, respectively, but because it&#39;s an empty line, both tools appear to be ignoring it. Furthermore, I can&#39;t just disable the tools themselves or modify their rules because they&#39;re part of a CI/CD pipeline I have no control over. I suppose I could disable one tool or the other for the entire file, but I&#39;d rather not do that either, as that defeats the purpose of having the tools in the first place.
  3. Does anyone know how to fix this issue?
  4. </details>
  5. # 答案1
  6. **得分**: 1
  7. 问题原来是我注释了错误的行。最初,我是这样做的:
  8. ```python
  9. def dictionary_updater(key: str) -> Callable[[FieldStringer], PayloadSetter]:
  10. """Convert string-converter to dictionary modifier."""
  11. # fmt: skip
  12. # Create the actual decorator method and return it
  13. def inner(func: FieldStringer) -> PayloadSetter:
  14. # Create the method that should actually be called when the decorated function
  15. # is invoked
  16. def with_dict(self, payload: Payload) -> None:
  17. payload[key] = func(self)
  18. return with_dict
  19. return inner

这个不起作用。经过一点思考,我意识到黑色将添加前一行的行,所以我必须在那里放置我的注释。我修改了我的代码,看起来像这样:

  1. def dictionary_updater(key: str) -> Callable[[FieldStringer], PayloadSetter]:
  2. """Convert string-converter to dictionary modifier.""" # fmt: skip
  3. # Create the actual decorator method and return it
  4. def inner(func: FieldStringer) -> PayloadSetter:
  5. # Create the method that should actually be called when the decorated function
  6. # is invoked
  7. def with_dict(self, payload: Payload) -> None:
  8. payload[key] = func(self)
  9. return with_dict
  10. return inner

现在一切都正常工作。尽管如此,我认为这些问题是反对过度检查的很好的理由,个人观点。

英文:

It turns out the issue was that I had commented the wrong line. Originally, I was doing this:

  1. def dictionary_updater(key: str) -&gt; Callable[[FieldStringer], PayloadSetter]:
  2. &quot;&quot;&quot;Convert string-converter to dictionary modifier.
  3. &quot;&quot;&quot;
  4. # fmt: skip
  5. # Create the actual decorator method and return it
  6. def inner(func: FieldStringer) -&gt; PayloadSetter:
  7. # Create the method that should actually be called when the decorated function
  8. # is invoked
  9. def with_dict(self, payload: Payload) -&gt; None:
  10. payload[key] = func(self)
  11. return with_dict
  12. return inner

which wasn't working. After a little thought, I realized that black would be adding the line from the previous line, so I had to put my comment there. I amended my code to look like this:

  1. def dictionary_updater(key: str) -&gt; Callable[[FieldStringer], PayloadSetter]:
  2. &quot;&quot;&quot;Convert string-converter to dictionary modifier.
  3. &quot;&quot;&quot; # fmt: skip
  4. # Create the actual decorator method and return it
  5. def inner(func: FieldStringer) -&gt; PayloadSetter:
  6. # Create the method that should actually be called when the decorated function
  7. # is invoked
  8. def with_dict(self, payload: Payload) -&gt; None:
  9. payload[key] = func(self)
  10. return with_dict
  11. return inner

and now everything works. That being said, these sorts of problems are an excellent argument against excessive linting, IMHO.

huangapple
  • 本文由 发表于 2023年7月11日 14:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定