Pyright: Pyright does not autocomplete parent class' fields

Created on 20 Aug 2020  路  5Comments  路  Source: microsoft/pyright

Hi, pyright does not autocomplete parent class' fields.

Here is a screenshot with both parent and child class, the point where language server should suggest parent class' field, and expected suggestion.
Screenshot_20200821_000357

Just pyright 1.1.64 is enabled as extension, nothing else.

addressed in next version enhancement request

Most helpful comment

This is now fixed in Pyright 1.1.65, which I just published. It will also be included in the next version of Pylance.

All 5 comments

You're correct that parent class fields are not suggested for auto-completion in the context of subclasses. What is the use case for this? Are you overriding a class field and initializing it with a different value than in the base class? I guess that's legal in Python, but it's not a very sound pattern in OOP.

Pyright does autocomplete base class methods (including the full signature) when in the context of a subclass. It's common to override the behavior of a method defined in the base class, so this suggestion makes sense.
Screen Shot 2020-08-20 at 4 15 02 PM

Pyright also offers class variables as suggestions when referencing a parent via a "self" reference.
Screen Shot 2020-08-20 at 4 11 06 PM

Thanks for quick reply. Overriding class fields is very common in Django. An example:

from django.views.generic import CreateView

class NewPostView(CreateView):
    model = Post
    form_class = PostForm
    success_url = reverse_lazy('post_list')
    template_name = 'new_post.html'

I agree with @KaratasFurkan . I think overriding class fields used mostly like overriding the constructor method ( __init__) to set the fields. It makes code very clean if you need the customize just a few fields.

It is very common on Django views and classes which inherit from views. Since this feature is not available, when you create views you have to look source code or documentation for sure.

https://docs.djangoproject.com/en/3.1/ref/contrib/admin/
https://www.django-rest-framework.org/tutorial/3-class-based-views/
https://docs.djangoproject.com/en/3.1/topics/class-based-views/generic-display/

OK, thanks for suggestion and the additional details.

I've added support for this, and it will be in the next version of Pyright and Pylance.

This is now fixed in Pyright 1.1.65, which I just published. It will also be included in the next version of Pylance.

Was this page helpful?
0 / 5 - 0 ratings