Test compatibility of structs created by StructMeta subclasses with encoders.
()
| 333 | |
| 334 | |
| 335 | def 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 | |
| 373 | def test_structmeta_abcmeta_mixed_behaves_like_abc(): |
nothing calls this directly
no test coverage detected
searching dependent graphs…