如何解决如果对象的功能不存在,flycheck 如何发出警告?
我正在使用 lsp for Python。我想知道一个对象的函数,如果它没有定义,lsp
可以使用 flycheck
或 jedi 给出错误/警告或下划线吗?我知道这很有挑战性,我只是想知道这是否可能。
示例python代码:
class World():
def hello():
print("hello")
obj = World()
obj.hello()()
obj.foo() # <=== hoping to see: No deFinitions found for: foo and underline foo()
~~~~~~~~~
由于 foo()
不是 World
下的对象;我希望 lsp
给我一条警告消息,让我知道该函数在对象定义下不存在。
示例配置可以在这里看到:https://github.com/rksm/emacs-rust-config
注释掉 9..35
和 48
行并添加以下 (use-package python :ensure nil)
保存和安装包。然后打开一个python文件,M-x lsp启动lsp,
解决方法
这是一个以编程方式检查给定对象是否具有任意名称的方法的函数:
def method_exists(obj_instance,method_name_as_string):
try:
eval("obj_instance." + method_name_as_string + "()")
except AttributeError:
print("object does not have the method " + method_name_as_string + "!")
return False
else:
print("object does not has the method " + method_name_as_string + "!")
return True
method_exists(obj,"foo") #returns False
method_exists(obj,"hello") #returns True
它返回一个布尔值,而不是出错并中断程序的执行。从那里,您可以发出飞行检查警告或根据信息做任何您想做的事情。它只检查实例方法,但可以很容易地调整为检查与对象无关的类方法或函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。