summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2020-09-16 17:37:51 -0700
committerGitHub <noreply@github.com>2020-09-16 17:37:51 -0700
commita9ba8ba9a71f3cb8d274c354ff67b6206abeb8ac (patch)
tree75d7cd7a6579d2f0b9ea4d510d142ad21709385d
parent4465df626684fdc8eeb25ca248218cde2eeeb9ec (diff)
downloadcpython-git-a9ba8ba9a71f3cb8d274c354ff67b6206abeb8ac.tar.gz
[3.9] bpo-39728: Enum: fix duplicate `ValueError` (GH-22277) (GH-22282)
fix default `_missing_` to return `None` instead of raising a `ValueError` Co-authored-by: Andrey Darascheka <andrei.daraschenka@leverx.com> (cherry picked from commit c95ad7a91fbd7636f33a098d3b39964ab083bf49) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
-rw-r--r--Lib/enum.py2
-rw-r--r--Lib/test/test_enum.py19
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2020-02-24-10-58-34.bpo-39728.kOOaHn.rst1
4 files changed, 21 insertions, 2 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index da5a31f0b8..ebadd9f662 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -628,7 +628,7 @@ class Enum(metaclass=EnumMeta):
@classmethod
def _missing_(cls, value):
- raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
+ return None
def __repr__(self):
return "<%s.%s: %r>" % (
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 6f547165ac..0e6b1b16ca 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -1832,6 +1832,18 @@ class TestEnum(unittest.TestCase):
third = auto()
self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes))
+ def test_default_missing(self):
+ class Color(Enum):
+ RED = 1
+ GREEN = 2
+ BLUE = 3
+ try:
+ Color(7)
+ except ValueError as exc:
+ self.assertTrue(exc.__context__ is None)
+ else:
+ raise Exception('Exception not raised.')
+
def test_missing(self):
class Color(Enum):
red = 1
@@ -1850,7 +1862,12 @@ class TestEnum(unittest.TestCase):
# trigger not found
return None
self.assertIs(Color('three'), Color.blue)
- self.assertRaises(ValueError, Color, 7)
+ try:
+ Color(7)
+ except ValueError as exc:
+ self.assertTrue(exc.__context__ is None)
+ else:
+ raise Exception('Exception not raised.')
try:
Color('bad return')
except TypeError as exc:
diff --git a/Misc/ACKS b/Misc/ACKS
index 2a180eb67c..5e0953ae91 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -432,6 +432,7 @@ Marcos Donolo
Dima Dorfman
Yves Dorfsman
Michael Dorman
+Andrey Doroschenko
Steve Dower
Allen Downey
Cesar Douady
diff --git a/Misc/NEWS.d/next/Library/2020-02-24-10-58-34.bpo-39728.kOOaHn.rst b/Misc/NEWS.d/next/Library/2020-02-24-10-58-34.bpo-39728.kOOaHn.rst
new file mode 100644
index 0000000000..beb2016a85
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-02-24-10-58-34.bpo-39728.kOOaHn.rst
@@ -0,0 +1 @@
+fix default `_missing_` so a duplicate `ValueError` is not set as the `__context__` of the original `ValueError`