X Tutup
The Wayback Machine - https://web.archive.org/web/20260305145447/https://github.com/python/cpython/issues/102077
Skip to content

[abc] Support ABCMeta with bases other than type #102077

@matekelemen

Description

@matekelemen

Feature or enhancement

Provide an option to choose a custom base class for abc.ABCMeta.

Pitch

In its current implementation, ABCMeta is hard-coded to inherit from type directly, forwarding calls to its __new__ member right to type.__new__. So what happens if I have another metaclass MyMeta that also adds functionality to __new__, and want to inherit from both? It seems to me that there's no way to get the behaviour of ABCMeta and MyMeta without calling both metaclasses' __new__ and thus invoking type.__new__ twice.

To my understanding, ABCMeta's extra features over type could be isolated into a standalone package that then could be used to enrich an existing metaclass:

def ABCMetaFactory(meta_type):
    class AbstractifiedMeta(meta_type): ...
    ...
    return AbstractifiedMeta

ABCMeta = ABCMetaFactory(type)

As an exact use case, pybind11 defines its own metaclass (pybind11_type), but I haven't found a non-hackish way to make it work with ABCMeta for quite a while now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup