MCPcopy Index your code
hub / github.com/msgspec/msgspec / test_struct_meta_subclass_with_encoder

Function test_struct_meta_subclass_with_encoder

tests/unit/test_struct_meta.py:335–370  ·  view source on GitHub ↗

Test compatibility of structs created by StructMeta subclasses with encoders.

()

Source from the content-addressed store, hash-verified

333
334
335def test_struct_meta_subclass_with_encoder():
336 """Test compatibility of structs created by StructMeta subclasses with encoders."""
337
338 # Define a custom metaclass
339 class EncoderMeta(StructMeta):
340 """Custom metaclass for testing encoders"""
341
342 # Use the custom metaclass to create a struct class
343 class EncoderStruct(metaclass=EncoderMeta):
344 id: int
345 name: str
346 tags: list[str] = []
347
348 # Create an instance
349 obj = EncoderStruct(id=123, name="test")
350
351 # Test JSON encoding and decoding
352 json_bytes = msgspec.json.encode(obj)
353 decoded = msgspec.json.decode(json_bytes, type=EncoderStruct)
354
355 assert decoded.id == 123
356 assert decoded.name == "test"
357 assert decoded.tags == []
358
359 # Test encoding and decoding with nested structs
360 class Container(metaclass=EncoderMeta):
361 item: EncoderStruct
362 count: int
363
364 container = Container(item=obj, count=1)
365 json_bytes = msgspec.json.encode(container)
366 decoded = msgspec.json.decode(json_bytes, type=Container)
367
368 assert decoded.count == 1
369 assert decoded.item.id == 123
370 assert decoded.item.name == "test"
371
372
373def test_structmeta_abcmeta_mixed_behaves_like_abc():

Callers

nothing calls this directly

Calls 3

EncoderStructClass · 0.85
ContainerClass · 0.85
decodeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…