Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vars() manipulation encounters problems with Enum #75982

Closed
ethanfurman opened this issue Oct 17, 2017 · 3 comments
Closed

vars() manipulation encounters problems with Enum #75982

ethanfurman opened this issue Oct 17, 2017 · 3 comments
Assignees
Labels
3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error

Comments

@ethanfurman
Copy link
Member

BPO 31801
Nosy @warsaw, @ethanfurman, @eric-wieser
PRs
  • bpo-31801: Enum: add _ignore_ as class option #5237
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ethanfurman'
    closed_at = <Date 2018-09-10.18:30:27.608>
    created_at = <Date 2017-10-17.00:21:55.188>
    labels = ['type-bug', '3.7']
    title = 'vars() manipulation encounters problems with Enum'
    updated_at = <Date 2018-09-10.18:30:27.607>
    user = 'https://github.com/ethanfurman'

    bugs.python.org fields:

    activity = <Date 2018-09-10.18:30:27.607>
    actor = 'ethan.furman'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2018-09-10.18:30:27.608>
    closer = 'ethan.furman'
    components = []
    creation = <Date 2017-10-17.00:21:55.188>
    creator = 'ethan.furman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31801
    keywords = ['patch']
    message_count = 3.0
    messages = ['304487', '306257', '310425']
    nosy_count = 4.0
    nosy_names = ['barry', 'eli.bendersky', 'ethan.furman', 'Eric.Wieser']
    pr_nums = ['5237']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31801'
    versions = ['Python 3.7']

    @ethanfurman
    Copy link
    Member Author

    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.

    @ethanfurman ethanfurman added the 3.7 (EOL) end of life label Oct 17, 2017
    @ethanfurman ethanfurman self-assigned this Oct 17, 2017
    @ethanfurman ethanfurman added the type-bug An unexpected behavior, bug, or error label Oct 17, 2017
    @eric-wieser
    Copy link
    Mannequin

    eric-wieser mannequin commented Nov 15, 2017

    Not necessarily an argument against this feature, but two workarounds exist for this already:

    1. Use nonlocal to prevent value going into the class namespace:
        value = None
        
        class BaudRate(enum.Enum):    
            nonlocal value
            for value in rates:
                locals()['B%d' % value] = value
        
            @classmethod
            def valid_rate(cls, value):
                return (any(value == item.value for item in cls))
    1. Use types.new_class, which is more suited to dynamic class creation anyway:
        def make_cls(ns):
            for value in rates:
                ns['B%d' % value] = value
    
            @classmethod
            def valid_rate(cls, value):
                return (any(value == item.value for item in cls))
        ns['valid_rate'] = valid_rate
    
    types.new_class('BaudRate', (enum.Enum,), exec_body=make_cls)
    

    @ethanfurman
    Copy link
    Member Author

    New changeset a4b1bb4 by Ethan Furman in branch 'master':
    bpo-31801: Enum: add _ignore_ as class option (bpo-5237)
    a4b1bb4

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant