英文:
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'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'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's not supposed to be an empty line between the docstring and the function body.
I've tried disabling the rule for each system, respectively, but because it's an empty line, both tools appear to be ignoring it. Furthermore, I can't just disable the tools themselves or modify their rules because they'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'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) -> 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
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) -> 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
and now everything works. That being said, these sorts of problems are an excellent argument against excessive linting, IMHO.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论