Walter Dörwald <[email protected]> added the comment:
Can we at least get the __qualname__ in exception messages?
Currently enum.Enum.__new__() and enum.Enum._missing_() use:
raise ValueError("%r is not a valid %s" % (value, cls.__name__))
IMHO this should be:
raise ValueError("%r is not a valid %s" % (value, cls.__qualname__))
in both spots.
Example code:
class Person:
class Type(enum.Enum):
EMPLOYEE = "employee"
CUSTOMER = "customer"
SUPPLIER = "supplier"
class Contact:
class Type(enum.Enum):
EMAIL = "email"
PHONE = "phone"
MOBILE = "mobile"
with this the following code:
Person.Type('foo')
raises the exception:
ValueError: 'foo' is not a valid Type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
"/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/enum.py",
line 310, in __call__
return cls.__new__(cls, value)
File
"/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/enum.py",
line 564, in __new__
raise exc
File
"/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/enum.py",
line 548, in __new__
result = cls._missing_(value)
File
"/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/enum.py",
line 577, in _missing_
raise ValueError("%r is not a valid %s" % (value, cls.__name__))
ValueError: 'foo' is not a valid Type
IMHO the exception message:
ValueError: 'foo' is not a valid Person.Type
would be much more helpful and unambiguous.
And BTW, maybe we should suppress exception chaining here, i.e. use:
raise ValueError("%r is not a valid %s" % (value, cls.__qualname__)) from
None
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue34443>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com