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 tests: Error path in _missing_() is not covered for Flag and IntFlag #90420

Closed
sobolevn opened this issue Jan 4, 2022 · 2 comments
Closed
Assignees
Labels
3.11 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Jan 4, 2022

BPO 46262
Nosy @ethanfurman, @sobolevn
PRs
  • bpo-46262: cover error path in enum.Flag._missing_ #30408
  • 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-05.00:12:08.634>
    created_at = <Date 2022-01-04.22:36:29.438>
    labels = ['type-bug', 'tests', '3.11']
    title = 'Enum tests: Error path in `_missing_()` is not covered for `Flag` and `IntFlag`'
    updated_at = <Date 2022-01-05.00:12:08.633>
    user = 'https://github.com/sobolevn'

    bugs.python.org fields:

    activity = <Date 2022-01-05.00:12:08.633>
    actor = 'ethan.furman'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2022-01-05.00:12:08.634>
    closer = 'ethan.furman'
    components = ['Tests']
    creation = <Date 2022-01-04.22:36:29.438>
    creator = 'sobolevn'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46262
    keywords = ['patch']
    message_count = 2.0
    messages = ['409717', '409728']
    nosy_count = 2.0
    nosy_names = ['ethan.furman', 'sobolevn']
    pr_nums = ['30408']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46262'
    versions = ['Python 3.11']

    @sobolevn
    Copy link
    Member Author

    sobolevn commented Jan 4, 2022

    I've noticed that enum.Flag._missing_ has some uncovered paths.

    For example, this error is never checked:

    cpython/Lib/enum.py

    Lines 1222 to 1225 in 31e43cb

    if not isinstance(value, int):
    raise ValueError(
    "%r is not a valid %s" % (value, cls.__qualname__)
    )

    The same method for Enum has good coverage:

    cpython/Lib/test/test_enum.py

    Lines 2278 to 2325 in 31e43cb

    def test_default_missing(self):
    class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3
    try:
    Color(7)
    except ValueError as exc:
    self.assertTrue(exc.__context__ is None)
    else:
    raise Exception('Exception not raised.')
    def test_missing(self):
    class Color(Enum):
    red = 1
    green = 2
    blue = 3
    @classmethod
    def _missing_(cls, item):
    if item == 'three':
    return cls.blue
    elif item == 'bad return':
    # trigger internal error
    return 5
    elif item == 'error out':
    raise ZeroDivisionError
    else:
    # trigger not found
    return None
    self.assertIs(Color('three'), Color.blue)
    try:
    Color(7)
    except ValueError as exc:
    self.assertTrue(exc.__context__ is None)
    else:
    raise Exception('Exception not raised.')
    try:
    Color('bad return')
    except TypeError as exc:
    self.assertTrue(isinstance(exc.__context__, ValueError))
    else:
    raise Exception('Exception not raised.')
    try:
    Color('error out')
    except ZeroDivisionError as exc:
    self.assertTrue(isinstance(exc.__context__, ValueError))
    else:
    raise Exception('Exception not raised.')

    I've added two simple test cases for Flag and IntFlag that check this.

    @sobolevn sobolevn added 3.11 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error labels Jan 4, 2022
    @AlexWaygood AlexWaygood changed the title Error path in _missing_() is not covered for enum.Flag and enum.IntFlag Enum tests: Error path in _missing_() is not covered for Flag and IntFlag Jan 4, 2022
    @AlexWaygood AlexWaygood changed the title Error path in _missing_() is not covered for enum.Flag and enum.IntFlag Enum tests: Error path in _missing_() is not covered for Flag and IntFlag Jan 4, 2022
    @ethanfurman
    Copy link
    Member

    New changeset 91bc6f9 by Nikita Sobolev in branch 'main':
    bpo-46262: [Enum] test error path in Flag._missing_ (GH-30408)
    91bc6f9

    @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 type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants