pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

[Feature request] Autocomplete Abstract, Protocol and method override

Open judej opened this issue 1 year ago • 4 comments

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.

judej avatar Jan 17 '24 22:01 judej

we just converted this back to an issue to add it to our roadmap

luabud avatar Jan 17 '24 22:01 luabud

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.

heejaechang avatar Jan 17 '24 22:01 heejaechang

+1 to doing this soon

rchiodo avatar Jan 26 '24 19:01 rchiodo

+1 this request is so powerful and really mark the difference between vscode and pycharm.

olidroide avatar Feb 06 '24 15:02 olidroide

+1

instanceofmel avatar Mar 05 '24 19:03 instanceofmel

+1

InnovateInfinity87 avatar Mar 26 '24 10:03 InnovateInfinity87

This issue has been fixed in prerelease version 2024.4.101, which we've just released. You can find the changelog here: CHANGELOG.md

debonte avatar Apr 11 '24 01:04 debonte

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?

christopherdean11 avatar Apr 16 '24 19:04 christopherdean11

Hi @christopherdean11, thanks for exploring the feature! To try it out:

  1. Place the cursor on the derived class name.
  2. Wait for the light bulb icon to show up.
  3. Click on the Implement all inherited abstract classes code action

Below is a gif that shows how to trigger it:

codeaction

StellaHuang95 avatar Apr 16 '24 19:04 StellaHuang95

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.

MRiabov avatar Jun 21 '24 13:06 MRiabov