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

Method test_intenum_missing

tests/unit/test_common.py:449–476  ·  view source on GitHub ↗
(self, proto)

Source from the content-addressed store, hash-verified

447 dec.decode(proto.encode({"fruit": 3}))
448
449 def test_intenum_missing(self, proto):
450 class Ex(enum.IntEnum):
451 A = 1
452 B = 2
453
454 @classmethod
455 def _missing_(cls, val):
456 if val == 3:
457 return cls.A
458 elif val == -4:
459 return cls.B
460 elif val == 5:
461 raise ValueError("oh no!")
462 else:
463 return None
464
465 dec = proto.Decoder(Ex)
466
467 def roundtrip(msg):
468 return dec.decode(proto.encode(msg))
469
470 assert roundtrip(1) is Ex.A
471 assert roundtrip(3) is Ex.A
472 assert roundtrip(-4) is Ex.B
473 with pytest.raises(ValidationError, match="Invalid enum value 5"):
474 roundtrip(5)
475 with pytest.raises(ValidationError, match="Invalid enum value 6"):
476 roundtrip(6)
477
478 def test_intflag(self, proto):
479 class Ex(enum.IntFlag):

Callers

nothing calls this directly

Calls 1

roundtripFunction · 0.85

Tested by

no test coverage detected