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

Generating mixed value for enum.Flag causes pretty-printer exception #3709

Closed
bartoszflis-silvair opened this issue Aug 2, 2023 · 2 comments · Fixed by #3710
Closed

Generating mixed value for enum.Flag causes pretty-printer exception #3709

bartoszflis-silvair opened this issue Aug 2, 2023 · 2 comments · Fixed by #3710
Assignees
Labels
bug something is clearly wrong here

Comments

@bartoszflis-silvair
Copy link

A self-contained reproduction:

import enum

from hypothesis import example, given
from hypothesis import strategies as st


class MyFlag(enum.Flag):
    ONE = 1
    TWO = 2


@given(st.sampled_from(MyFlag))
@example(MyFlag.ONE | MyFlag.TWO)
def test_pretty_printing(flag: MyFlag):
    assert isinstance(flag, MyFlag)

Tests fail with the following error message:

test_hypothesis_bug.py::test_pretty_printing Failed: [undefined]TypeError: can only concatenate str (not "NoneType") to str
@given(st.sampled_from(MyFlag))
>   @example(MyFlag.ONE | MyFlag.TWO)

tests/unit/test_hypothesis_bug.py:13: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib/python3.10/site-packages/hypothesis/core.py:1056: in _raise_to_user
    raise the_error_hypothesis_found
.venv/lib/python3.10/site-packages/hypothesis/core.py:470: in execute_explicit_examples
    bits = ", ".join(nicerepr(x) for x in arguments) + ", ".join(
.venv/lib/python3.10/site-packages/hypothesis/core.py:471: in <genexpr>
    f"{k}={nicerepr(v)}" for k, v in example_kwargs.items()
.venv/lib/python3.10/site-packages/hypothesis/internal/reflection.py:452: in nicerepr
    return re.sub(r"(\[)~([A-Z][a-z]*\])", r"\g<1>\g<2>", pretty(v))
.venv/lib/python3.10/site-packages/hypothesis/vendor/pretty.py:101: in pretty
    printer.pretty(obj)
.venv/lib/python3.10/site-packages/hypothesis/vendor/pretty.py:199: in pretty
    return printer(obj, self, cycle)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

obj = <MyFlag.TWO|ONE: 3>
p = <hypothesis.vendor.pretty.RepresentationPrinter object at 0x7fc633f41090>
cycle = False

    def _repr_enum(obj, p, cycle):
>       p.text(type(obj).__name__ + "." + obj.name)
E       TypeError: can only concatenate str (not "NoneType") to str

.venv/lib/python3.10/site-packages/hypothesis/vendor/pretty.py:804: TypeError

Root cause: mixed-value flags don't have a name.

Python 3.10
hypothesis 6.82.0

@Zac-HD Zac-HD added the bug something is clearly wrong here label Aug 3, 2023
@Zac-HD
Copy link
Member

Zac-HD commented Aug 3, 2023

Thanks for the report! This is a neat little bug, fortunately the solution should be pretty self-contained in that _repr_enum() function for whoever gets to it first.

@bartoszflis-silvair
Copy link
Author

Thank you for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants