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

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

What to do when your formatter and your linter are fighting

问题

我一直在Python中编写装饰器

```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)

        return with_dict

    return inner

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

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

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


<details>
<summary>英文:</summary>

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)

    return with_dict

return inner

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.

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.

Does anyone know how to fix this issue?

</details>


# 答案1
**得分**: 1

问题原来是我注释了错误的行。最初,我是这样做的:

```python
def dictionary_updater(key: str) -> Callable[[FieldStringer], PayloadSetter]:
    """Convert string-converter to dictionary modifier."""
    # fmt: skip
    # 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)

        return with_dict

    return inner

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

def dictionary_updater(key: str) -> Callable[[FieldStringer], PayloadSetter]:
    """Convert string-converter to dictionary modifier."""  # fmt: skip
    # 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)

        return with_dict

    return inner

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

英文:

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

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

        return with_dict

    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:

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

        return with_dict

    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:

确定