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

Multi-inheritance B024 #277

Closed
barney-ellis opened this issue Aug 23, 2022 · 4 comments · Fixed by #281
Closed

Multi-inheritance B024 #277

barney-ellis opened this issue Aug 23, 2022 · 4 comments · Fixed by #281

Comments

@barney-ellis
Copy link

Hi,

The latest release introduced a new error code (B024) which checks that an abc defines abstract methods/properties. I have an abc, A, which has a __call__ abstract method.

class A(metaclass=abc.ABCMeta):

  def __eq__(self, other: Any) -> bool:
      ...

  def __repr__(self) -> str:
      ...

  @abc.abstractmethod
  def __call__(self, ...) -> ...:
      raise NotImplementedError`

I also have a different abc, RandomA which shares A's methods (such as eq) and abstract __call__, but also has a random number generator as an instance variable

class RandomA(A, metaclass=abc.ABCMeta): 

    def __init__(self, rng: Optional[np.random.Generator]) -> None:
        self.rng = rng if rng is not None else np.random.default_rng()

This way I can create child classes which have deterministic or random behaviour.

RandomA now violates B024 as it doesn't define abstract methods, even though it should inherit A's __call__ method right?

@Zac-HD
Copy link
Member

Zac-HD commented Aug 25, 2022

Yep, that's a false alarm.

I think in this specific case you don't need to declare the metaclass though, since that should be inherited from A? I could be wrong though.

@barney-ellis
Copy link
Author

barney-ellis commented Aug 25, 2022

This passes the flake8 tests, however my IDE (PyCharm) flags a warning that I'm not implementing the __call__ method from A.

@Zac-HD
Copy link
Member

Zac-HD commented Aug 25, 2022

Hmm, I think Pycharm is also giving a false alarm there!

Nonetheless, we've got something to fix too. I propose that B024 be disabled for child classes which inherit from anything but abc.ABC - it'll miss some warnings, but still cover the common cases.

@jakkdl
Copy link
Contributor

jakkdl commented Aug 31, 2022

Yeah that sounds good, and also fixes #278

jakkdl added a commit to jakkdl/flake8-bugbear that referenced this issue Sep 29, 2022
jakkdl added a commit to jakkdl/flake8-bugbear that referenced this issue Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants