From 77b0bbb21ad95739c715035020a46d052f0d7a90 Mon Sep 17 00:00:00 2001 From: "David R. MacIver" Date: Thu, 26 Jan 2023 10:25:22 +0000 Subject: [PATCH 1/2] Define a pretty printer for enums --- hypothesis-python/src/hypothesis/vendor/pretty.py | 5 +++++ hypothesis-python/tests/cover/test_pretty.py | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/hypothesis-python/src/hypothesis/vendor/pretty.py b/hypothesis-python/src/hypothesis/vendor/pretty.py index 77c149ab28..bb0c16a5a7 100644 --- a/hypothesis-python/src/hypothesis/vendor/pretty.py +++ b/hypothesis-python/src/hypothesis/vendor/pretty.py @@ -762,9 +762,14 @@ def _repr_dataframe(obj, p, cycle): # pragma: no cover p.break_() +def _repr_enum(obj, p, cycle): + p.text(type(obj).__name__ + "." + obj.name) + + for_type_by_name("collections", "defaultdict", _defaultdict_pprint) for_type_by_name("collections", "OrderedDict", _ordereddict_pprint) for_type_by_name("ordereddict", "OrderedDict", _ordereddict_pprint) for_type_by_name("collections", "deque", _deque_pprint) for_type_by_name("collections", "Counter", _counter_pprint) for_type_by_name("pandas.core.frame", "DataFrame", _repr_dataframe) +for_type_by_name("enum", "Enum", _repr_enum) diff --git a/hypothesis-python/tests/cover/test_pretty.py b/hypothesis-python/tests/cover/test_pretty.py index a14333c1bb..b89a9eadf2 100644 --- a/hypothesis-python/tests/cover/test_pretty.py +++ b/hypothesis-python/tests/cover/test_pretty.py @@ -50,6 +50,7 @@ import re import warnings from collections import Counter, OrderedDict, defaultdict, deque +from enum import Enum import pytest @@ -620,3 +621,11 @@ def test_repr_call(func_name): assert _repr_call(func_name, (aas,), {}) == f"{fn}(\n {aas!r},\n)" assert _repr_call(func_name, (), {"a": 1, "b": 2}) == f"{fn}(a=1, b=2)" assert _repr_call(func_name, (), {"x": aas}) == f"{fn}(\n x={aas!r},\n)" + + +class AnEnum(Enum): + SOME_MEMBER = 1 + + +def test_pretty_prints_enums_as_code(): + assert pretty.pretty(AnEnum.SOME_MEMBER) == "AnEnum.SOME_MEMBER" From b2f7e6dd4814c1310d67635e8b052bb5922e1c73 Mon Sep 17 00:00:00 2001 From: "David R. MacIver" Date: Thu, 26 Jan 2023 10:58:37 +0000 Subject: [PATCH 2/2] Add missing release file --- hypothesis-python/RELEASE.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 hypothesis-python/RELEASE.rst diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst new file mode 100644 index 0000000000..ebf48345ba --- /dev/null +++ b/hypothesis-python/RELEASE.rst @@ -0,0 +1,4 @@ +RELEASE_TYPE: patch + +This release improves the pretty-printing of enums in falsifying examples, +so that they print as their full identifier rather than their repr.