Skip to content

Commit d5b0dfa

Browse files
authored
Merge pull request #3710 from Zac-HD/enum-reprs
2 parents 41dd521 + 5a4a591 commit d5b0dfa

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes pretty-printing of combinations of :class:`enum.Flag`
4+
values, which was previously an error (:issue:`3709`).

hypothesis-python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
def local_file(name):
28-
return Path(__file__).parent.joinpath(name).relative_to(Path.cwd())
28+
return Path(__file__).absolute().parent.joinpath(name).relative_to(Path.cwd())
2929

3030

3131
SOURCE = str(local_file("src"))

hypothesis-python/src/hypothesis/vendor/pretty.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def _repr_pretty_(self, p, cycle):
6969
import warnings
7070
from collections import defaultdict, deque
7171
from contextlib import contextmanager
72+
from enum import Flag
7273
from io import StringIO
7374
from math import copysign, isnan
7475

@@ -801,7 +802,11 @@ def _repr_dataframe(obj, p, cycle): # pragma: no cover
801802

802803

803804
def _repr_enum(obj, p, cycle):
804-
p.text(type(obj).__name__ + "." + obj.name)
805+
tname = type(obj).__name__
806+
if isinstance(obj, Flag):
807+
p.text(" | ".join(f"{tname}.{x.name}" for x in type(obj) if x & obj == x))
808+
else:
809+
p.text(f"{tname}.{obj.name}")
805810

806811

807812
for_type_by_name("collections", "defaultdict", _defaultdict_pprint)

hypothesis-python/tests/cover/test_pretty.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import re
5151
import warnings
5252
from collections import Counter, OrderedDict, defaultdict, deque
53-
from enum import Enum
53+
from enum import Enum, Flag
5454

5555
import pytest
5656

@@ -628,5 +628,40 @@ class AnEnum(Enum):
628628
SOME_MEMBER = 1
629629

630630

631-
def test_pretty_prints_enums_as_code():
632-
assert pretty.pretty(AnEnum.SOME_MEMBER) == "AnEnum.SOME_MEMBER"
631+
class Options(Flag):
632+
A = 1
633+
B = 2
634+
C = 4
635+
636+
637+
class EvilReprOptions(Flag):
638+
A = 1
639+
B = 2
640+
641+
def __repr__(self):
642+
return "can't parse this nonsense"
643+
644+
645+
class LyingReprOptions(Flag):
646+
A = 1
647+
B = 2
648+
649+
def __repr__(self):
650+
return "LyingReprOptions.A|B|C"
651+
652+
653+
@pytest.mark.parametrize(
654+
"rep",
655+
[
656+
"AnEnum.SOME_MEMBER",
657+
"Options.A",
658+
"Options.A | Options.B",
659+
"Options.A | Options.B | Options.C",
660+
"EvilReprOptions.A",
661+
"LyingReprOptions.A",
662+
"EvilReprOptions.A | EvilReprOptions.B",
663+
"LyingReprOptions.A | LyingReprOptions.B",
664+
],
665+
)
666+
def test_pretty_prints_enums_as_code(rep):
667+
assert pretty.pretty(eval(rep)) == rep

0 commit comments

Comments
 (0)