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

msgspec.Struct cannot be used together with abc #806

Open
777michael777 opened this issue Jan 23, 2025 · 1 comment
Open

msgspec.Struct cannot be used together with abc #806

777michael777 opened this issue Jan 23, 2025 · 1 comment

Comments

@777michael777
Copy link

Description

I am trying to create a Struct that is also an abstract class and run into the metaclass conflict.
Here's an example:

import msgspec
import abc


class Base(msgspec.Struct, metaclass=abc.ABCMeta):
    member: int

    @abc.abstractmethod
    def method(self) -> None:
        pass


class Subclass(Base):
    pass


a = Subclass(7)

When executing above code, I get

Traceback (most recent call last):
  File "x.py", line 5, in <module>
    class Base(msgspec.Struct, metaclass=abc.ABCMeta):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Also tried with not using the metaclass, but inherit Base from abc.ABC directly, with the same result.

@rafalkrupinski
Copy link

rafalkrupinski commented Jan 23, 2025

That makes sense, since msgspec.Struct brings its own metaclass. No abstract classes for us.

Funny, I tried to do the same thing just days ago, but I didn't know how to pass both metaclass parameters and two base classes. Didn't occur to me ABC brings a metaclass too.

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

No branches or pull requests

2 participants