[Python-checkins] [3.8] bpo-39728: Enum: fix duplicate `ValueError` (GH-22277) (GH-22283)
Ethan Furman
webhook-mailer at python.org
Wed Sep 16 20:38:19 EDT 2020
https://github.com/python/cpython/commit/5efb1a77e75648012f8b52960c8637fc296a5c6d
commit: 5efb1a77e75648012f8b52960c8637fc296a5c6d
branch: 3.8
author: Ethan Furman <ethan at stoneleaf.us>
committer: GitHub <noreply at github.com>
date: 2020-09-16T17:38:14-07:00
summary:
[3.8] bpo-39728: Enum: fix duplicate `ValueError` (GH-22277) (GH-22283)
fix default `_missing_` to return `None` instead of raising a `ValueError`
Co-authored-by: Andrey Darascheka <andrei.daraschenka at leverx.com>.
(cherry picked from commit c95ad7a91fbd7636f33a098d3b39964ab083bf49)
Co-authored-by: Ethan Furman <ethan at stoneleaf.us>
files:
A Misc/NEWS.d/next/Library/2020-02-24-10-58-34.bpo-39728.kOOaHn.rst
M Lib/enum.py
M Lib/test/test_enum.py
M Misc/ACKS
diff --git a/Lib/enum.py b/Lib/enum.py
index c2adfd1c6c0bb..de9ed4c1ec371 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -634,7 +634,7 @@ def _generate_next_value_(name, start, count, last_values):
@classmethod
def _missing_(cls, value):
- raise ValueError("%r is not a valid %s" % (value, cls.__name__))
+ return None
def __repr__(self):
return "<%s.%s: %r>" % (
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index daa44ae6824ce..745962a1e66fe 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -1834,6 +1834,18 @@ class Dupes(Enum):
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
@@ -1852,7 +1864,12 @@ def _missing_(cls, item):
# 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 e6d0c3ba12708..eee6b9accaa50 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -421,6 +421,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 0000000000000..beb2016a85ba6
--- /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`
More information about the Python-checkins
mailing list