Search code examples
python-3.xdjangovisual-studio-codeautocompletepylance

Vs Code + Python : What means VS (Pylance) autocomplete suggestion for get_success_url()


When I define `get_success_url' in my views, if I accept Pylance Autocomplete suggestion, I got this :

def get_success_url(self) -> str:
        return super().get_success_url()

Didn't find anywhere how to use this. By my side, I'm use to do :

def get_success_url(self, **kwargs):
        return reverse_lazy('name_space:url_name', kwargs={'pk': foo})

How to (can I) use pylance's suggestion to achieve the same result as my method


Solution

  • The autocomplete version isn't actually doing anything.

    Sometimes when you are overriding a function, you want to use the same function of the parent class plus something extra, eg, if you override save() you might set another property and then do a normal save, which you do by calling super().save().

    def save(self, *args, **kwargs):
        self.status = Model.SAVED
        return super().save()
    

    However, you don't always have to call super() if your function is doing the job on its own. In this case with the autocomplete, if all you do is return super().get_success_url() there's no point in overriding the function as you haven't done anything different to an parent function. Your existing get_success_url() performs a purpose and replaces any super() version, so you don't need to call super() in it.

    Autocomplete is just a suggestion, it's not saying you need to be doing something a certain way.