This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author doerwalter
Recipients barry, danishprakash, doerwalter, eli.bendersky, enedil, ethan.furman, orlnub123, serhiy.storchaka, underscore_asterisk
Date 2019-07-16.12:03:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1563278621.12.0.725151841015.issue34443@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2019-07-16 12:03:41doerwaltersetrecipients: + doerwalter, barry, eli.bendersky, ethan.furman, serhiy.storchaka, enedil, orlnub123, danishprakash, underscore_asterisk
2019-07-16 12:03:41doerwaltersetmessageid: <1563278621.12.0.725151841015.issue34443@roundup.psfhosted.org>
2019-07-16 12:03:41doerwalterlinkissue34443 messages
2019-07-16 12:03:40doerwaltercreate