question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Check for abstract base class definitions with no abstractmethods

See original GitHub issue
from abc import ABC, ABCMeta, abstractmethod

class Base(ABC):  # I'd like a lint error here if there are no @abstract* decorated methods

    # @abstractmethod
    def method(self):
        ...

class MyABC(metaclass=ABCMeta):  # and here too for the same reason
    pass

Suggested message: {classname} is an abstract base class, but it has no abstract methods. Remember to use @abstractmethod, @abstractclassmethod, and/or @abstractproperty decorators.

We might also want to emit a warning for a probably-missing-decorator on any method of an ABC where the whole method body is ..., even if there are some abstract-decorated methods on the class.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cooperleescommented, Aug 22, 2022

I’m not a fan of that tbh as we don’t have good enough change log enforcement etc. with this project so the 1 commit + release via GitHub is there enough for me imo.

1reaction
jakkdlcommented, Aug 17, 2022

I’m starting on a PR now!

We might also want to emit a warning for a probably-missing-decorator on any method of an ABC where the whole method body is …, even if there are some abstract-decorated methods on the class.

How about extending this to the body being anything but pass, ... or string constants - to also catch

# @abstractmethod
def method(self):
  """my abstract method"""
  pass

Similar to pyright’s check for @overload with an implementation https://github.com/microsoft/pyright/blob/6fa755c025cb5b18838d8bfdd85761184afa45f1/packages/pyright-internal/src/analyzer/parseTreeUtils.ts#L1999

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can we define an abstract class without abstract method in ...
And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that...
Read more >
Defining an abstract class without any abstract methods
Yes we can have an abstract class without Abstract Methods as both are independent concepts. Declaring a class abstract means that ...
Read more >
Abstract Methods and Classes (The Java™ Tutorials ...
An abstract class is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated,...
Read more >
Writing Abstract Classes and Methods
Sometimes, a class that you define represents an abstract concept and as such, should not be instantiated. Take, for example, food in the...
Read more >
Abstract Class in Java - GeeksforGeeks
If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found