msgspec.defstruct is not compatible with msgspec.StructMeta
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.1k
- Forks
- 193
- Avg merge
- 3d 20h
- Merged PRs (30d)
- 15
Description
Description
Structs that were defined with custom metaclass (subclass of msgspec.StructMeta) can't be subclassed with msgspec.defstruct. (msgspec 0.20.0)
Following code:
import msgspec
class _FooStructMeta(msgspec.StructMeta):
def __new__(mcls, name, bases, namespace, **struct_config):
return super().__new__(mcls, name, bases, namespace, **struct_config)
class FooStruct(msgspec.Struct, metaclass=_FooStructMeta):
pass
BarStruct = msgspec.defstruct(name="BarStruct", fields=[("id", int)], bases=(FooStruct,))
Raises this error:
Traceback (most recent call last):
File "/home/jirka642/.config/JetBrains/PyCharm2025.3/scratches/scratch_1.py", line 13, in <module>
BarStruct = msgspec.defstruct(name="BarStruct", fields=[("id", int)], bases=(FooStruct,))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jirka642/.config/JetBrains/PyCharm2025.3/scratches/scratch_1.py", line 6, in __new__
return super().__new__(mcls, name, bases, namespace, **struct_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Struct types cannot define __slots__
I have been able to hotfix it by running namespace.pop("__slots__", None) in the metaclass, but I'm not sure if that is safe.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Python reproduction and the msgspec.defstruct entry point, then inspect how its bases and metaclass are handled. Done means the reproduction creates BarStruct successfully when FooStruct uses a custom StructMeta, with coverage for the reported case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100