Commit 323842c2 authored by Walter Dörwald's avatar Walter Dörwald Committed by Ethan Furman

bpo-34443: Use __qualname__ instead of __name__ in enum exception messages. (GH-14809)

* Use __qualname__ instead of __name__ in enum exception messages.
parent af2f5b17
...@@ -577,7 +577,7 @@ class Enum(metaclass=EnumMeta): ...@@ -577,7 +577,7 @@ class Enum(metaclass=EnumMeta):
if isinstance(result, cls): if isinstance(result, cls):
return result return result
else: else:
ve_exc = ValueError("%r is not a valid %s" % (value, cls.__name__)) ve_exc = ValueError("%r is not a valid %s" % (value, cls.__qualname__))
if result is None and exc is None: if result is None and exc is None:
raise ve_exc raise ve_exc
elif exc is None: elif exc is None:
...@@ -599,7 +599,7 @@ class Enum(metaclass=EnumMeta): ...@@ -599,7 +599,7 @@ class Enum(metaclass=EnumMeta):
@classmethod @classmethod
def _missing_(cls, value): def _missing_(cls, value):
raise ValueError("%r is not a valid %s" % (value, cls.__name__)) raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
def __repr__(self): def __repr__(self):
return "<%s.%s: %r>" % ( return "<%s.%s: %r>" % (
...@@ -706,7 +706,7 @@ class Flag(Enum): ...@@ -706,7 +706,7 @@ class Flag(Enum):
# verify all bits are accounted for # verify all bits are accounted for
_, extra_flags = _decompose(cls, value) _, extra_flags = _decompose(cls, value)
if extra_flags: if extra_flags:
raise ValueError("%r is not a valid %s" % (value, cls.__name__)) raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
# construct a singleton enum pseudo-member # construct a singleton enum pseudo-member
pseudo_member = object.__new__(cls) pseudo_member = object.__new__(cls)
pseudo_member._name_ = None pseudo_member._name_ = None
...@@ -780,7 +780,7 @@ class IntFlag(int, Flag): ...@@ -780,7 +780,7 @@ class IntFlag(int, Flag):
@classmethod @classmethod
def _missing_(cls, value): def _missing_(cls, value):
if not isinstance(value, int): if not isinstance(value, int):
raise ValueError("%r is not a valid %s" % (value, cls.__name__)) raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
new_member = cls._create_pseudo_member_(value) new_member = cls._create_pseudo_member_(value)
return new_member return new_member
......
...@@ -2260,12 +2260,13 @@ class TestFlag(unittest.TestCase): ...@@ -2260,12 +2260,13 @@ class TestFlag(unittest.TestCase):
d = 4 d = 4
f = 6 f = 6
# Bizarre.c | Bizarre.d # Bizarre.c | Bizarre.d
self.assertRaisesRegex(ValueError, "5 is not a valid Bizarre", Bizarre, 5) name = "TestFlag.test_cascading_failure.<locals>.Bizarre"
self.assertRaisesRegex(ValueError, "5 is not a valid Bizarre", Bizarre, 5) self.assertRaisesRegex(ValueError, "5 is not a valid " + name, Bizarre, 5)
self.assertRaisesRegex(ValueError, "2 is not a valid Bizarre", Bizarre, 2) self.assertRaisesRegex(ValueError, "5 is not a valid " + name, Bizarre, 5)
self.assertRaisesRegex(ValueError, "2 is not a valid Bizarre", Bizarre, 2) self.assertRaisesRegex(ValueError, "2 is not a valid " + name, Bizarre, 2)
self.assertRaisesRegex(ValueError, "1 is not a valid Bizarre", Bizarre, 1) self.assertRaisesRegex(ValueError, "2 is not a valid " + name, Bizarre, 2)
self.assertRaisesRegex(ValueError, "1 is not a valid Bizarre", Bizarre, 1) self.assertRaisesRegex(ValueError, "1 is not a valid " + name, Bizarre, 1)
self.assertRaisesRegex(ValueError, "1 is not a valid " + name, Bizarre, 1)
def test_duplicate_auto(self): def test_duplicate_auto(self):
class Dupes(Enum): class Dupes(Enum):
......
Exceptions from :mod:`enum` now use the ``__qualname`` of the enum class in
the exception message instead of the ``__name__``.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment