我正在为Python使用lsp。我想知道,对于对象的函数,如果没有定义,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()
~~~~~~~~~
由于CCD_ 3不是CCD_;我希望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
它返回一个布尔值,而不是出错并中断程序的执行。从那里,你可以发出飞行检查警告,或者如果有信息,你可以做任何你想做的事情。它只检查实例方法,但可以很容易地适用于检查与对象无关的类方法或函数。