简体   繁体   中英

How can flycheck give warning if the object's function does not exist?

I am using lsp for Python . I was wondering for an object's function, if there is no definition for it, can lsp give an error/warning or underline using flycheck or jedi ? I know this is challenging I just wonder that's possible or not.

example python code:

class World():
    def hello():
        print("hello")


obj = World()
obj.hello()()
obj.foo()   # <=== hoping to see: No definitions found for: foo and underline foo()
~~~~~~~~~

Since foo() is not an object under World ; I want lsp to give me a warning message letting me know that that function does not exist under the object definition.


Example configuration could be seen here: https://github.com/rksm/emacs-rust-config

Comment out the lines 9..35 and 48 and add the following (use-package python:ensure nil) save and install packages. Then open a python file, and Mx lsp to start lsp,

This is a function to programatically check if a given object has a method with an arbitrary name:

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

It returns a boolean instead of erroring out and interrupting the program's execution. From there, you can give a flycheck warning or do pretty much whatever you want if the information. It is only checking for instance methods but can be easily adapted to also check for class methods or functions not associated with objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM