pylance-release
pylance-release copied to clipboard
[Feature request] Autocomplete Abstract, Protocol and method override
Discussed in https://github.com/microsoft/pylance-release/discussions/2703
Originally posted by GilShoshan94 July 19, 2020 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 docstring
Example:
from abc import ABC, abstractmethod
class BaseAbc(ABC):
def my_concrete_method(self, a, b, c):
"""My docstring my_concrete_method"""
pass
@abstractmethod
def my_abstract_method(self, a, b, c):
"""My docstring my_abstract_method"""
pass
@classmethod
@abstractmethod
def my_abstract_classmethod(cls, a):
"""My docstring my_abstract_classmethod"""
pass
@staticmethod
@abstractmethod
def my_abstract_staticmethod(b, c):
"""My docstring my_abstract_staticmethod"""
pass
@property
@abstractmethod
def my_abstract_property(self):
"""My docstring my_abstract_property"""
pass
And than whenever we start a new class and inherit from BaseAbc
, after the :
and we press enter
, we would have this auto-completion:
from my_abc_module import BaseAbc
class ConcreteClass(BaseAbc):
def my_abstract_method(self, a, b, c):
"""My docstring my_abstract_method"""
raise NotImplementedError
@classmethod
def my_abstract_classmethod(cls, a):
"""My docstring my_abstract_classmethod"""
raise NotImplementedError
@staticmethod
def my_abstract_staticmethod(b, c):
"""My docstring my_abstract_staticmethod"""
raise NotImplementedError
@property
def my_abstract_property(self):
"""My docstring my_abstract_property"""
raise NotImplementedError
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.
we just converted this back to an issue to add it to our roadmap
see this - https://code.visualstudio.com/docs/csharp/refactoring#_implement-all-members-implicitly
there are a few of refactoring of this kind. called implement interface
, implement abstract class
and etc.
+1 to doing this soon
+1 this request is so powerful and really mark the difference between vscode and pycharm.
+1
+1
This issue has been fixed in prerelease version 2024.4.101, which we've just released. You can find the changelog here: CHANGELOG.md
Thank you for implementing this feature, I am excited to try it out. I can't seem to figure out how to get it to work though. I've read through this and the related post and the changelog but there is no mention of how to get it to trigger and write the "not implemented" methods. I've tried using the example at the top of this thread with the pylance preview version 2024.4.101 installed in VS Code 1.88.1. I've reloaded the window, full quit VS Code and relaunched, looked through the pylance settings but nothing stuck out as a knob that defaults to 'off' to me. What am I missing?
Hi @christopherdean11, thanks for exploring the feature! To try it out:
- Place the cursor on the derived class name.
- Wait for the light bulb icon to show up.
- Click on the
Implement all inherited abstract classes
code action
Below is a gif that shows how to trigger it:
Hello, Maybe worth a separate issue, but I would suggest some at least subtle marking that this is possible, e.g. a warning or a spelling underline. Right now there is no marking that this is possible, and new users (me) don't know that this is a thing.
Thank you.
P.S. Also, it would be good if an ABCMeta implementation of ABC would have their methods declated as @abstractmethod
automatically.