这看起来像一个错误,jedi.evaluate.representation.Instance.__getattr__()
错误地阻止了.names_dict
. 我向绝地存储库添加了一个拉取请求来解决这个问题。同时,您可以将“names_dict”添加到副本中的白名单中Instance.__getattr__()
jedi/evaluate/representation.py
,或者使用下面的代码为当前会话自动修补此方法。
import jedi
def patch_jedi():
__old__getattr__ = jedi.evaluate.representation.Instance.__getattr__
def __patched__getattr__(self, name):
if name == 'names_dict':
# do a simplified version of __old__getattr__, bypassing the name check
return getattr(self.base, name)
else:
# use standard behavior
return __old__getattr__(self, name)
# test whether jedi has been updated to avoid the Instance.defined_names() bug
try:
jedi.names("lst = list()")[0].defined_names()
except AttributeError as e:
if e.args[0].startswith("Instance ") and e.args[0].endswith("Don't touch this (names_dict)!"):
# patch jedi to avoid this error
print "patching jedi"
jedi.evaluate.representation.Instance.__getattr__ = __patched__getattr__
else:
# something else strange is going on
raise
patch_jedi()
print jedi.names("lst = list()")[0].defined_names()
# or: print jedi.Script("lst = list()").goto_definitions()[0].defined_names()
我应该注意我不熟悉jedi
,所以我不知道是否defined_names()
应该适用于创建实例的定义。上面的代码不会修复类似的引用jedi.names("lst = []")[0].defined_names()
,并且没有明显的补丁可以做到这一点。所以可能有一些我不知道的更深层次的事情正在发生。希望开发人员能够帮助解决这个拉取请求。