Skip to content

Commit 323842c

Browse files
doerwalterethanfurman
authored andcommitted
bpo-34443: Use __qualname__ instead of __name__ in enum exception messages. (GH-14809)
* Use __qualname__ instead of __name__ in enum exception messages.
1 parent af2f5b1 commit 323842c

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

Lib/enum.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ def __new__(cls, value):
577577
if isinstance(result, cls):
578578
return result
579579
else:
580-
ve_exc = ValueError("%r is not a valid %s" % (value, cls.__name__))
580+
ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
581581
if result is None and exc is None:
582582
raise ve_exc
583583
elif exc is None:
@@ -599,7 +599,7 @@ def _generate_next_value_(name, start, count, last_values):
599599

600600
@classmethod
601601
def _missing_(cls, value):
602-
raise ValueError("%r is not a valid %s" % (value, cls.__name__))
602+
raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
603603

604604
def __repr__(self):
605605
return "<%s.%s: %r>" % (
@@ -706,7 +706,7 @@ def _create_pseudo_member_(cls, value):
706706
# verify all bits are accounted for
707707
_, extra_flags = _decompose(cls, value)
708708
if extra_flags:
709-
raise ValueError("%r is not a valid %s" % (value, cls.__name__))
709+
raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
710710
# construct a singleton enum pseudo-member
711711
pseudo_member = object.__new__(cls)
712712
pseudo_member._name_ = None
@@ -780,7 +780,7 @@ class IntFlag(int, Flag):
780780
@classmethod
781781
def _missing_(cls, value):
782782
if not isinstance(value, int):
783-
raise ValueError("%r is not a valid %s" % (value, cls.__name__))
783+
raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
784784
new_member = cls._create_pseudo_member_(value)
785785
return new_member
786786

Lib/test/test_enum.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,12 +2260,13 @@ class Bizarre(Flag):
22602260
d = 4
22612261
f = 6
22622262
# Bizarre.c | Bizarre.d
2263-
self.assertRaisesRegex(ValueError, "5 is not a valid Bizarre", Bizarre, 5)
2264-
self.assertRaisesRegex(ValueError, "5 is not a valid Bizarre", Bizarre, 5)
2265-
self.assertRaisesRegex(ValueError, "2 is not a valid Bizarre", Bizarre, 2)
2266-
self.assertRaisesRegex(ValueError, "2 is not a valid Bizarre", Bizarre, 2)
2267-
self.assertRaisesRegex(ValueError, "1 is not a valid Bizarre", Bizarre, 1)
2268-
self.assertRaisesRegex(ValueError, "1 is not a valid Bizarre", Bizarre, 1)
2263+
name = "TestFlag.test_cascading_failure.<locals>.Bizarre"
2264+
self.assertRaisesRegex(ValueError, "5 is not a valid " + name, Bizarre, 5)
2265+
self.assertRaisesRegex(ValueError, "5 is not a valid " + name, Bizarre, 5)
2266+
self.assertRaisesRegex(ValueError, "2 is not a valid " + name, Bizarre, 2)
2267+
self.assertRaisesRegex(ValueError, "2 is not a valid " + name, Bizarre, 2)
2268+
self.assertRaisesRegex(ValueError, "1 is not a valid " + name, Bizarre, 1)
2269+
self.assertRaisesRegex(ValueError, "1 is not a valid " + name, Bizarre, 1)
22692270

22702271
def test_duplicate_auto(self):
22712272
class Dupes(Enum):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Exceptions from :mod:`enum` now use the ``__qualname`` of the enum class in
2+
the exception message instead of the ``__name__``.

0 commit comments

Comments
 (0)