microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.67k stars 770 forks source link

Visible marking of ABC implementation possibility #6045

Open MRiabov opened 1 week ago

MRiabov commented 1 week ago

Hello, Related to, automatic inherited method implementation of ABC and `ABCMeta, I would suggest a visible marking a possibility of such action, e.g. a warning or a spelling underline under the class. It is only natural that abstract class needs an implementation. E.g:

`class SketchEnv(Abstract

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, e.g.

 class AbstractClass(ABC):
        @abstractmethod        
        def desiredChildMethod():
            pass

    class MetaAbstract(ABCMeta, AbstractClass):

        @abstractmethod     _# @abstractmethod inserted automatically_
        def desiredChildMethod():
              _#this passes the method forward_
            pass

I know this isn't always the case (with real implementations), but my guess is that it is more often that not.

P.S.S. Better context (21.6.2024): https://github.com/microsoft/pylance-release/issues/5362

heejaechang commented 4 days ago

tagging @StellaHuang95

rchiodo commented 3 days ago

Adding the GIF that shows how to trigger this:

image

I believe this new request is to mark the class somehow so that the code action is more apparent?

MRiabov commented 3 days ago

Adding the GIF that shows how to trigger this:

image

I believe this new request is to mark the class somehow so that the code action is more apparent?

Precisely. It's simply non-obvious that something can be done. Btw, the attachment you have is image, not GIF.

MRiabov commented 1 day ago

bump?

rchiodo commented 1 day ago

It's unlikely we'd get to this anytime soon. It would need a lot of people asking for it.

rchiodo commented 1 day ago

And it's not obvious how we should expose it. I'm not sure a squiggle makes sense. Those indicate something wrong with your code.

This is more likely a VS code UI suggestion. How to indicate Code Actions are available on different pieces of code without clicking on them.

MRiabov commented 1 day ago

Well, squiggle can be white, as in grammar typo underline. And indeed, something is wrong - as we are not implementing the necessary for the class functions - although we should.

erictraut commented 1 day ago

If you are not implementing all of the abstract methods, pyright (the type checker upon which pylance is built) will emit an error if you attempt to instantiate the incomplete class. It will tell you which methods are unimplemented. Furthermore, if you mark the concrete class as @final (using the typing.final decorator), pyright will tell you about the missing implementations without an attempt to instantiate the class. To get these diagnostics, you need to enable the reportAbstractUsage diagnostic check.

from abc import ABC, abstractmethod
from typing import final

class AbstractClass(ABC):
    @abstractmethod
    def method1(self) -> int:
        raise NotImplementedError()

class ConcreteClass1(AbstractClass):
    pass

c = ConcreteClass1() # Error: cannot instantiate abstract class

@final
class ConcreteClass2(AbstractClass): # Error: final class does not implement abstract symbols
    pass

image