[New-bugs-announce] [issue31801] vars() manipulation encounters problems with Enum

Ethan Furman report at bugs.python.org
Mon Oct 16 20:21:55 EDT 2017


New submission from Ethan Furman <ethan at stoneleaf.us>:

The following code should work (from https://stackoverflow.com/a/46780742/208880):


    class BaudRate(Enum):

        cls = vars()
        regexp = r"(?:^|,)B(?P<rate>\d+)"
        rates = sorted(map(int, re.findall(regexp, ",".join(dir(termios)))))
        for value in rates:
            cls['B%d' % value] = value

        @classmethod
        def valid_rate(cls, value):
            return (any(value == item.value for item in cls))


This doesn't work because EnumMeta does not allow names to be reassigned nor deleted.  aenum gets around this by allowing an _ignore_ attribute which contains the names that should not be tracked (and, in fact, those names are removed from the final class).

Unless a better idea is put forward, I will add _ignore_ support to Enum.

----------
assignee: ethan.furman
messages: 304487
nosy: barry, eli.bendersky, ethan.furman
priority: normal
severity: normal
stage: test needed
status: open
title: vars() manipulation encounters problems with Enum
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31801>
_______________________________________


More information about the New-bugs-announce mailing list