Python pylint错误格式字符串的说明
在以下文件中:Python pylint错误格式字符串的说明,python,pylint,Python,Pylint,在以下文件中: """hello I am the module spam.py""" from __future__ import unicode_literals 'hello {world}'.format(world='potato') 对于格式错误的字符串,我们有以下pylint冲突: wim@SDFA100461C:/tmp$ pylint --reports=n spam.py No config file found, using default configuration *
"""hello I am the module spam.py"""
from __future__ import unicode_literals
'hello {world}'.format(world='potato')
对于格式错误的字符串
,我们有以下pylint冲突:
wim@SDFA100461C:/tmp$ pylint --reports=n spam.py
No config file found, using default configuration
************* Module spam
W: 3, 0: Invalid format string (bad-format-string)
我不理解这个建议,pylint开发人员说检查是关于风格的,但我在PEP中没有看到任何违反此处的内容
有什么问题?派林希望我们对此做些什么
版本号如下
wim@SDFA100461C:/tmp$ pylint --version
No config file found, using default configuration
pylint 1.3.0,
astroid 1.2.0, common 0.62.1
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2]
这是
pylint
中的一个bug;它假定所有字符串格式都是字节字符串
linter解析格式,然后解析占位符名称。因为您使用的是Unicode文本,这也会产生一个Unicode
名称,但是解析器假设它只会遇到ByTestRing;如果不是,则假定它找到了一个整数:
if not isinstance(keyname, str):
# In Python 2 it will return long which will lead
# to different output between 2 and 3
keyname = int(keyname)
这会为格式字符串引发ValueError
,因为world
被解析为unicode
值:
>>> import string
>>> formatter = string.Formatter()
>>> parseiterator = formatter.parse(u'hello {world}')
>>> result = next(parseiterator)
>>> result
(u'hello ', u'world', u'', None)
>>> keyname, fielditerator = result[1]._formatter_field_name_split()
>>> keyname
u'world'
然后捕获ValueError
异常,并将其转换为IncompleteFormatString
异常,这将导致W1302
错误代码
看
此处的测试应更改为测试与format_string
相同的类型:
if not isinstance(keyname, type(format_string)):
# In Python 2 it will return long which will lead
# to different output between 2 and 3
keyname = int(keyname)
这在Python2和Python3中都是正确的