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

Unreachable condition: if enum_class._member_type_ is object #90454

Closed
sobolevn opened this issue Jan 7, 2022 · 5 comments
Closed

Unreachable condition: if enum_class._member_type_ is object #90454

sobolevn opened this issue Jan 7, 2022 · 5 comments
Assignees
Labels
3.11 only security fixes tests Tests in the Lib/test dir

Comments

@sobolevn
Copy link
Member

sobolevn commented Jan 7, 2022

BPO 46296
Nosy @ethanfurman, @sobolevn
PRs
  • bpo-46296: add a test case for enum.py which covers _use_args == True and _member_type_ == object #30458
  • 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 2022-01-07.21:45:40.560>
    created_at = <Date 2022-01-07.17:36:29.337>
    labels = ['tests', '3.11']
    title = 'Unreachable condition: `if enum_class._member_type_ is object`'
    updated_at = <Date 2022-01-07.21:45:40.560>
    user = 'https://github.com/sobolevn'

    bugs.python.org fields:

    activity = <Date 2022-01-07.21:45:40.560>
    actor = 'ethan.furman'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2022-01-07.21:45:40.560>
    closer = 'ethan.furman'
    components = ['Tests']
    creation = <Date 2022-01-07.17:36:29.337>
    creator = 'sobolevn'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46296
    keywords = ['patch']
    message_count = 5.0
    messages = ['409989', '410026', '410033', '410040', '410041']
    nosy_count = 2.0
    nosy_names = ['ethan.furman', 'sobolevn']
    pr_nums = ['30458']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue46296'
    versions = ['Python 3.11']

    @sobolevn
    Copy link
    Member Author

    sobolevn commented Jan 7, 2022

    This condition can never be True:

    cpython/Lib/enum.py

    Lines 222 to 223 in b127e70

    if enum_class._member_type_ is object:
    enum_member._value_ = value

    Why? Because:

    1. It is executed only when `if not enum_class._use_args_:`:
      if not enum_class._use_args_:
    2. But, `enum_class._use_args_` will always be `False` for cases when `enum_class._member_type_` is `object`
    3. It is defined here:
      if first_enum is None or __new__ in (Enum.__new__, object.__new__):

    So, I guess that removing this condition can simplify the enum.py.
    Coverage shows that this part is not tested.

    I will send a PR shortly.

    @sobolevn sobolevn added stdlib Python modules in the Lib dir 3.11 only security fixes labels Jan 7, 2022
    @ethanfurman
    Copy link
    Member

    (2) is false.

    >>> from enum import Enum
    >>> 
    >>> class MyEnum(Enum):
    ...     def __new__(cls, value):
    ...         member = object.__new__(cls)
    ...         return member
    ...     ONE = 1
    ... 
    
    >>> MyEnum.ONE
    MyEnum.ONE
    
    >>> MyEnum._use_args_
    True
    
    >>> MyEnum._member_type_
    <class 'object'>

    If coverage shows a branch not being tested, design a test for it instead of removing the branch.

    @sobolevn
    Copy link
    Member Author

    sobolevn commented Jan 7, 2022

    Yes, this is just a missing test. Not a wrong condition!

    @sobolevn sobolevn added tests Tests in the Lib/test dir and removed stdlib Python modules in the Lib dir labels Jan 7, 2022
    @ethanfurman
    Copy link
    Member

    New changeset 74d1663 by Nikita Sobolev in branch 'main':
    bpo-46296: [Enum] add a test for missing value recovery (GH-30458)
    74d1663

    @ethanfurman
    Copy link
    Member

    Thank you, Nikita!

    @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.11 only security fixes tests Tests in the Lib/test dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants