mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-34536: raise error for invalid _missing_ results (GH-9147)
* raise exception if _missing_ returns None or invalid type
This commit is contained in:
parent
a5d1eb8d8b
commit
019f0a0cb8
3 changed files with 54 additions and 1 deletions
|
@ -3,6 +3,7 @@ import inspect
|
|||
import pydoc
|
||||
import sys
|
||||
import unittest
|
||||
import sys
|
||||
import threading
|
||||
from collections import OrderedDict
|
||||
from enum import Enum, IntEnum, EnumMeta, Flag, IntFlag, unique, auto
|
||||
|
@ -1697,6 +1698,38 @@ class TestEnum(unittest.TestCase):
|
|||
third = auto()
|
||||
self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes))
|
||||
|
||||
def test_missing(self):
|
||||
class Color(Enum):
|
||||
red = 1
|
||||
green = 2
|
||||
blue = 3
|
||||
@classmethod
|
||||
def _missing_(cls, item):
|
||||
if item == 'three':
|
||||
return cls.blue
|
||||
elif item == 'bad return':
|
||||
# trigger internal error
|
||||
return 5
|
||||
elif item == 'error out':
|
||||
raise ZeroDivisionError
|
||||
else:
|
||||
# trigger not found
|
||||
return None
|
||||
self.assertIs(Color('three'), Color.blue)
|
||||
self.assertRaises(ValueError, Color, 7)
|
||||
try:
|
||||
Color('bad return')
|
||||
except TypeError as exc:
|
||||
self.assertTrue(isinstance(exc.__context__, ValueError))
|
||||
else:
|
||||
raise Exception('Exception not raised.')
|
||||
try:
|
||||
Color('error out')
|
||||
except ZeroDivisionError as exc:
|
||||
self.assertTrue(isinstance(exc.__context__, ValueError))
|
||||
else:
|
||||
raise Exception('Exception not raised.')
|
||||
|
||||
|
||||
class TestOrder(unittest.TestCase):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue