Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclasses of abstract class that do not inherit abc.ABC are considered abstract #7950

Open
sshane opened this issue Dec 15, 2022 · 4 comments · May be fixed by #7955
Open

Subclasses of abstract class that do not inherit abc.ABC are considered abstract #7950

sshane opened this issue Dec 15, 2022 · 4 comments · May be fixed by #7955
Labels
False Negative 🦋 No message is emitted but something is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@sshane
Copy link

sshane commented Dec 15, 2022

Bug description

Related to #3098. Subclasses of an abstract parent class which do not inherit directly from abc.ABC are considered abstract, and only throw an error during run time when trying to instantiate it.

For example, take this code below. PyCharm correctly determines through a IDE warning that Sub is not abstract, and says Class Sub must implement all abstract methods, but pylint will ignore this class, as its ancestors inherit from abc.ABC. Do we not get the best of both worlds (allowing abstract subclasses of abstract parent classes, ie. AbsSub, while catching non-abstract subclasses of abstract parent classes, ie. Sub) if we check the bases, instead of the ancestors where that PR to fix the issue above does it?

import abc
import unittest

class Abstract(unittest.TestCase, abc.ABC):  # abstract, should have no warning
  @abc.abstractmethod
  def test_something(self):
    pass

  @abc.abstractmethod
  def test_another_thing(self):
    pass


class AbsSub(Abstract, abc.ABC):  # abstract, should have no warning
  def test_another_thing(self):
    return 1


class Sub(AbsSub):  # not abstract since it doesn't inherit from abc.ABC directly, should warn
  pass

Configuration

No response

Command used

pylint test.py

Pylint output

`NOTHING`

Expected behavior

test.py:19:0: W0223: Method 'test_something' is abstract in class 'Abstract' but is not overridden (abstract-method) (referring to Sub)

Pylint version

2.5.0 (but same behavior on 2.15.8)

OS / Environment

Ubuntu 20.04

Additional dependencies

No response

@sshane sshane added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Dec 15, 2022
@DanielNoord
Copy link
Collaborator

Seems like the check is indeed incorrect!

@DanielNoord DanielNoord added False Negative 🦋 No message is emitted but something is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 15, 2022
@sshane
Copy link
Author

sshane commented Dec 15, 2022

If not one wants to/has time to open a PR, I can try to tackle it.

@DanielNoord
Copy link
Collaborator

That would definitely be appreciated!

@jacobtylerwalls jacobtylerwalls added this to the 3.3.0 milestone Jun 4, 2024
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 3.3.0, 3.4.0 Sep 20, 2024
@jacobtylerwalls jacobtylerwalls modified the milestones: 3.4.0, 4.0.0 Sep 25, 2024
@DetachHead
Copy link
Contributor

as a workaround, you can define your own version of ABC instead:

from abc import abstractmethod
import unittest

class ABC(metaclass=ABCMeta):
    pass

class Abstract(unittest.TestCase, ABC):
  @abstractmethod
  def test_something(self):
    pass

  @abstractmethod
  def test_another_thing(self):
    pass


class AbsSub(Abstract, abc.ABC):
  def test_another_thing(self):
    return 1


class Sub(AbsSub):  # error: Method 'test_something' is abstract in class 'Abstract' but is not overridden in child class 'Sub'
  pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Negative 🦋 No message is emitted but something is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants