Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete inherited methods when overriding in child class #458

Closed
kerryz opened this issue Aug 1, 2014 · 15 comments
Closed

Autocomplete inherited methods when overriding in child class #458

kerryz opened this issue Aug 1, 2014 · 15 comments

Comments

@kerryz
Copy link

kerryz commented Aug 1, 2014

From an issue from Anaconda:

When working with frameworks, a very common pattern is to inherit from a provided class and then overriding the class methods/fields to customize behavior.

When writing this new customized child class, a very helpful tool is the autocomplete-functionality of both the method that I'm trying to override and also the autocompletion of the required arguments. This functionality is provided in IDEs like PyCharm and PyDev.

Example

from django.db import models

class TestModel(models.Model):
    def sav

A dropdown should appear here with a suggestion to autocomplete to save, and writing a left parenthesis should then generate all the parameters:

class TestModel(models.Model):
    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        models.Model.save(self, force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields)

Maybe an autocompletion to super(TestModel, self).save(...) would be better, or not generating the super call at all would also be acceptable (though preferably it should be generated), but this is the behavior in the Eclipse plugin PyDev (a dropdown is generated in which you can choose the option: save (Override method in models.Model) when pressing the autocomplete keybinding CTRL + Space)

I.e. my suggestion is to make Jedi aware of a cursor being inside a (child) class that is inheriting from another (parent) class, and when trying to override methods and fields (defined in the parent class) from within the child class, Jedi should autocomplete the fields and methods and also their arguments.

@davidhalter
Copy link
Owner

Hmm, this could potentially be another method of #253.

I'm not sure if this should be part of the autocompletion (by default). But there could be some kind of switch to enable this.

@Nahtrezer
Copy link

Sorry for bumping old thread, but feature described by topicstarter would be very useful.
Is there any possibility to turn it on?

@blueyed
Copy link
Contributor

blueyed commented Sep 4, 2015

@Nahtrezer
It is not implemented yet, so cannot be turned on.

@davidhalter
Copy link
Owner

Done!

@blueyed
Copy link
Contributor

blueyed commented Aug 1, 2016

Please note that you need to use Jedi's dev branch for this to work in jedi-vim.

@fx-kirin
Copy link

fx-kirin commented Apr 5, 2018

How to use this feature? I'm using jedi v0.11.1 through jedi-vim, but not autocompleted. I checked commit 647a4db is contained in v0.11.1.

class ParentClass(object):
    def __init__(self, a, b, c):
        pass
        

class TestClass(ParentClass):
    def __init__{hit ctrl-n here}

I'm currently asking the same question in stackoverflow.

https://stackoverflow.com/questions/49640881/python-super-class-method-auto-completion-or-snippets-in-vim

Envirionment

  • Python 2.7.12
  • Ubuntu 16.04

@blueyed
Copy link
Contributor

blueyed commented Apr 5, 2018

I do not think this is possible as of now. 647a4db appears to be only about parent methods, but not args itself.

@singhravi1
Copy link

Is there any possible solution for this right now?

@davidhalter
Copy link
Owner

About what?

@singhravi1
Copy link

singhravi1 commented Jun 12, 2019

Auto-completing the child method of an overriding class while using jedi.
And also, when i click on the the overriding method linked via super() like, super().dummy_method(..), it does not take me to its definition.

I'm using VS code BTW

@davidhalter
Copy link
Owner

Auto-completing the child method of an overriding class while using jedi.

Not exactly sure what you mean with that.

And also, when i click on the the overriding method linked via super() like, super().dummy_method(..), it does not take me to its definition.

That should work. At least it does for me. (v0.13.3+).

@singhravi1
Copy link

> Auto-completing the child method of an overriding class while using jedi.

Not exactly sure what you mean with that.

Like suggesting params for the method

And also, when i click on the the overriding method linked via super() like,

super().dummy_method(..), it does not take me to its definition.

That should work. At least it does for me. (v0.13.3+).

Not working for me maybe because i'm writing the method from parent of the current parent class

@davidhalter
Copy link
Owner

Like suggesting params for the method

Please open an issue if you want that. This was about something else.

Not working for me maybe because i'm writing the method from parent of the current parent class

That doesn't work and probably won't for quite some time. It's also a bad idea IMO in many cases and in the few cases where it's a good idea, it's probably easy enough to remember. If you want a solution for this, use abc.ABC with abc.abstractmethod (which then works with Jedi).

@fangjunzhou
Copy link

fangjunzhou commented Dec 18, 2024

I think the issue of method override completion with parameters is still not resolved. I've searched online but it seems nobody is talking about it.

Take test file test/completion/context.py in 647a4db for example:

As I mentioned in 647a4db#r150509859, an ideal completion with parameter in line 13 should be

#? ['func(self, foo)']
def f

Edited

I've checked the implementation of _get_class_context_completions. It seems that the entire auto completion in Jedi only returns name. Therefore, I think the aforementioned parameter completion proposal doesn't fit in the project.

However, the problem I'm trying to solve goes as follows:

When calling a class method, only completing the name like this is OK:

#? ['func']
X().f

This is because I can see the parameter declaration after typing the left parenthesis

# Method declaration hint will popup with `func(foo)` here
X().func(

However, this feature doesn't exist with method override. Therefore, it'll be hard to follow the parent class method declaration for complex methods.

Not sure how to solve the problem, but I guess it's not Jedi's job.

@davidhalter
Copy link
Owner

Jedi just doesn't do that yet, but I wouldn't oppose pull requests into this direction, if it's well done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants