简体   繁体   English

带有注释的长参数的Python PEP8格式

[英]Python PEP8 formatting for long arguments with annotations

I have several function with log, annotated arguments such as the following: 我有几个带日志的函数,带注释的参数如下:

def myfunction(values: Optional[List[str]],
               keywords: Dict[str, Tuple[str, int]],
               default_value: Optional[str]=None
               ) -> Dict[str, str]:
    ...

PEP8 doesn't say anything specifically about this, but the formatting above is my best estimate of what would be considered conventional. PEP8对此没有特别说明,但是上面的格式是我对常规格式的最佳估计。 However, the linters suggest otherwise. 然而,短毛绒建议相反。 Flake8 is happy with this, and complains if I change it, pylint wants it formatted with a space at the default argument, and the brackets lining up: Flake8对此很满意,并且抱怨如果我更改它,pylint希望在默认参数中将其格式化为空格,并且将括号对齐:

def myfunction(values: Optional[List[str]],
               keywords: Dict[str, Tuple[str, int]],
               default_value: Optional[str] = None
              ) -> Dict[str, str]:
    ...

So flake8 and pylint contradict each other on the bracket indentation, and pylint insists on spacing around the = whichI'm pretty sure contradicts PEP8. 所以flake8和pylint在括号缩进上彼此矛盾,并且pylint坚持=周围的间距,我敢肯定与PEP8矛盾。

Is there any clear PEP8 guideline on this that I've missed? 我错过了与此相关的任何明确的PEP8指南吗? Or is it simply that pylint and flake8 have differing opinions on an undocumented case? 还是pylint和flake8对一个无证案件有不同的看法? Regarding the = spacing, is pylint just plain wrong here, or is there some exception to PEP8 that I've missed. 关于=间距,是pylint只是这里的错误,还是我错过了PEP8的一些例外。

I think you can usually format it like this to make pep8 and pylint happy (not that it's better though) 我认为您通常可以像这样格式化它,以使pep8和pylint开心(尽管这不是更好)

def myfunction(
    values: Optional[List[str]],
    keywords: Dict[str, Tuple[str, int]],
    default_value: Optional[str] = None
    ) -> Dict[str, str]:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM