【发布时间】:2018-03-17 14:43:01
【问题描述】:
为什么 pylint 和 IDE 的智能感知功能无法识别 time.struct_time 的实例?以下代码包含对类的存在/不存在属性、命名元组和类命名元组time.struct_time 的一些简单测试。在 pylint、IntelliJ 和 VSCode 中,一切都按预期工作——除了time.struct_time 之外,在每种情况下都会报告对缺失属性的访问——它在任何这些工具中都不会产生警告或错误。为什么他们不知道它是什么以及它的属性是什么?
import time
from collections import namedtuple
t = time.localtime()
e = t.tm_mday
e = t.bad # this is not reported by linters or IDEs.
class Clz:
cvar = 'whee'
def __init__(self):
self.ivar = 'whaa'
o = Clz()
e = Clz.cvar
e = o.ivar
e = Clz.bad
e = o.bad
Ntup = namedtuple('Ntup', 'thing')
n = Ntup(thing=3)
e = n.thing
e = n.bad
问题的上下文是pipenv中的以下最近的错误 -
# Halloween easter-egg.
if ((now.tm_mon == 10) and (now.tm_day == 30))
显然,pass 路径从未经过测试,但似乎典型的静态分析工具在这里也无济于事。这对于标准库中的类型来说很奇怪。
(可以在https://github.com/kennethreitz/pipenv/commit/033b969d094ba2d80f8ae217c8c604bc40160b03 看到完整的修复)
【问题讨论】:
-
可能是因为
time.struct_time在 C 中,所以他们无法检查其来源是否有有效属性。 -
当然,这可能与它有关,但我认为这还不够。这些工具不会被每种 C 实现的类型所混淆。他们对列表等非常满意。
-
他们需要对
time.struct_time不存在的列表进行特定处理。 -
再一次,这听起来很合理,但完全是推测性的。为什么不会到位?这些东西不会自动生成吗?那还缺什么?等等
-
如果您尝试转到提到
def localtime(seconds=None): # real signature unknown; restored from __doc__的函数定义。因此,如果不知道真正的签名,那么 lint 将如何工作?
标签: python-3.x visual-studio-code pycharm pylint