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

enum always runs member_type when use_args is True #62745

Closed
lambacck mannequin opened this issue Jul 24, 2013 · 2 comments
Closed

enum always runs member_type when use_args is True #62745

lambacck mannequin opened this issue Jul 24, 2013 · 2 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@lambacck
Copy link
Mannequin

lambacck mannequin commented Jul 24, 2013

BPO 18545
Nosy @ethanfurman
Files
  • enum_value_transform.patch: Patch with a test demonstrating the problem and a fix.
  • 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 2013-07-25.20:51:10.991>
    created_at = <Date 2013-07-24.14:01:53.283>
    labels = ['type-bug']
    title = 'enum always runs member_type when use_args is True'
    updated_at = <Date 2013-07-25.20:51:10.989>
    user = 'https://bugs.python.org/lambacck'

    bugs.python.org fields:

    activity = <Date 2013-07-25.20:51:10.989>
    actor = 'python-dev'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2013-07-25.20:51:10.991>
    closer = 'python-dev'
    components = []
    creation = <Date 2013-07-24.14:01:53.283>
    creator = 'lambacck'
    dependencies = []
    files = ['31028']
    hgrepos = []
    issue_num = 18545
    keywords = ['patch']
    message_count = 2.0
    messages = ['193648', '193719']
    nosy_count = 3.0
    nosy_names = ['lambacck', 'ethan.furman', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue18545'
    versions = ['Python 3.4']

    @lambacck
    Copy link
    Mannequin Author

    lambacck mannequin commented Jul 24, 2013

    Starting at line 153 in enum.py there is:

    153 if not use_args:
    154 enum_member = __new__(enum_class)
    155 original_value = value
    156 else:
    157 enum_member = __new__(enum_class, *args)
    158 original_value = member_type(*args)
    159 if not hasattr(enum_member, '_value_'):
    160 enum_member._value_ = original_value

    When use_args is True, the member_type is always called with the arguments even if the return value from enum_member has a _value_ attr. If the __new__ function transforms the the *args then the call to member_type(*args) can fail even thought the value would not be used. I've attached a patch with a fix and a test to demonstrate the problem.

    The use case for the test in the attached patch is to populate Django ORM choices to a field. The first value in the tuple is the value used in the database (and as the Enum value) and the second value in the tuple is the label that Django shows in select boxes.

    @lambacck lambacck mannequin added the type-bug An unexpected behavior, bug, or error label Jul 24, 2013
    @ethanfurman ethanfurman self-assigned this Jul 24, 2013
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 25, 2013

    New changeset 95e1d0efd896 by Ethan Furman in branch 'default':
    Close bpo-18545: now only executes member_type if no _value_ is assigned in __new__.
    http://hg.python.org/cpython/rev/95e1d0efd896

    @python-dev python-dev mannequin closed this as completed Jul 25, 2013
    @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
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant