[Feature request] Autocomplete Abstract, Protocol and method override #2703
Replies: 7 comments
-
Thanks for the suggestion @GilShoshan94. Pylance already implements a part of what you're suggesting. If you create a subclass of an ABC, you can start to type the name of an abstract method, and Pylance will offer to fill in the full signature for that method. It doesn't currently copy any decorators (like @Property or @classmethod), and it doesn't copy the docstring. It also doesn't currently work for protocols. Those are all good extensions to the current behavior. |
Beta Was this translation helpful? Give feedback.
-
@erictraut That's a good start, I still think the autocomplete I am talking about would be very useful since you anyway has to implement every abstract methods, and it seems error-prone when you have more than 2,3 abstract method to do since as more as you "fill" your class with stuff, you can forget about them and won't see it clearly in the suggestion list. It is not critical of course since Python will throw an error if an abstract method is missing. I just remember how comfortable that was when I was programming in C++ in Visual Studio. |
Beta Was this translation helpful? Give feedback.
-
Pylance offers a couple of related features that you may find useful. These are off by default, but you can enable them is you want. First, if you enable the type checker ("python.analysis.typeCheckingMode": "basic"), Pylance will flag any attempt to instantiate a class that has abstract methods that haven't been overridden. Second, if you enable the diagnostic rule "reportIncompatibleMethodOverride", Pylance will report any case where a subclass overrides a method with a signature that differs from its parent class. This applies to abstract and non-abstract methods. |
Beta Was this translation helpful? Give feedback.
-
We are working on a project where we have several abstract classes for different purposes and they have a bunch of abstract methods as well. We then create classes that inherit from this abstract classes we need to go 1 by 1 implementing the different abstract methods which is tedious and it is rather common that we try instantiating one in the same file just to check the error raised to see which method we have forgotten. Having this type of autocomplete would be great because you are forced to implement those methods and giving that the method signature is already autocompleted it would be great to be able to select from autocompletion pop up an option that autocompletes every method one needs to write. Is this something you plan on adding? It would make it sooo much simpler. |
Beta Was this translation helpful? Give feedback.
-
As something related, I like the It would be really nice to have something similar in VS Code 🙂 |
Beta Was this translation helpful? Give feedback.
-
Moving this issue to discussion as an enhancement request for comments and upvotes. |
Beta Was this translation helpful? Give feedback.
-
This has been added to our backlog. Use #5362 to track the progress |
Beta Was this translation helpful? Give feedback.
-
Hi,
Thank you for this amazing product !
Python support Abstract class (it is stable since 3.4) and Protocol class (New in version 3.8).
Abstract class
It would be a great feature if, when implementing a concrete class that inherit from an abstract one, all the methods decorated by @abstractmethod will be immediately copy with the same signature (and maybe a
raise NotImplementedError
in the body ?), with the same decorator and with the same docstringExample:
And than whenever we start a new class and inherit from
BaseAbc
, after the:
and we pressenter
, we would have this auto-completion:Protocol
I did not use it yet but it sounds useful if I can import a module with Protocols inside and than vscode would propose an "implement protocol ..." in the "right click menu" and/or in the command palette when the cursor is inside a class.
It would autocomplete the same way it does with abstract class described here.
Anyone is welcome to improve further this suggestion.
Beta Was this translation helpful? Give feedback.
All reactions