Skip to content

ABCMeta and its subclasses are treated differently #13561

Closed

Description

@sobolevn

It is a common practice to create subclasses of ABCMeta for your own abstract metaclasses when you also have other metaclass(es). It helps solving metaclass conflict error.

For example:

from djang.db.models import ModelBase
from abc import ABCMeta

class AbstractModel(ABCMeta, ModelBase): ...

Or any other existing metaclass.

But, mypy treats ABCMeta and AbstractModel differently:

from abc import ABCMeta, abstractmethod
from typing import Protocol

class A:  # OK, has @abstractmethod
    @abstractmethod
    def f(self) -> None:
        pass

# Expected:
class B(A):  # E: Class a.B has abstract attributes "f"  # N: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
    pass

class C(A, metaclass=ABCMeta):  # OK, has ABCMeta as a metaclass
    pass

class CustomABC(ABCMeta):
    pass

# Unexpected:
class D(A, metaclass=CustomABC):  # E: Class a.D has abstract attributes "f"  # N: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
    pass

I think that needs to be fixed.

Activity

  1. self-assigned this
    on Aug 31, 2022
  2. added a commit that references this issue on Sep 3, 2022
    fd2d684
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions