Home > Blockchain >  Method parameter auto completion for a class instance
Method parameter auto completion for a class instance

Time:05-13

I am trying to get auto completion of method parameter names for class instances but can't quite figure it out. Take this example class:

class Abc:
    def meth(param):
        pass

If I type Abc.meth(p and press Tab I see the expected completion of param=:

enter image description here

However, if I try to do the same with an instance of the class Abc().meth(p, I don't see param in the completion results:

enter image description here

I see the same behavior in both JupyterLab and VS Code, and the fact that this works for instances of other packages such as scikit learn, I think there is something I am missing.

enter image description here

How can I get method parameter completion for an instance of my Abc() class similar to the LinearRegression class above?

CodePudding user response:

Based on experimentation on JupyterLab Version 3.2.9, this seems to be because the autocomplete tries to account for implicit parameters like self and cls. Including a self parameter should mostly fix your issues.

class Abc:
    def meth(arg1, arg2, arg3):
        pass

These are the completion options I was presented for the class above:

Decorator Abc.meth Abc().meth
None arg1, arg2, arg3 arg2, arg3
@staticmethod arg1, arg2, arg3 arg2, arg3
@classmethod arg2, arg3 arg3

The standard behavior is fine, but the results for Abc().meth are wrong with both decorators. As mentioned in the docs for staticmethod and classmethod:

method can be called either on the class (such as C.f()) or on an instance (such as C().f())

So both columns should have been the same when decorators are used, but it always omits one parameter from Abc().meth, presumably for self. And even though cls is correctly handled for Abc.meth when using @classmethod, it ends up omitting two parameters in Abc().meth instead.


Testing with Visual Studio Code 1.67.1 gave the correct autocomplete options for all cases. So the missing suggestion you experienced there is expected behavior since param takes the place of self and there are no other parameters.

  • Related