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

Define a pretty printer for enums #3561

Merged
merged 2 commits into from Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions 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.
5 changes: 5 additions & 0 deletions hypothesis-python/src/hypothesis/vendor/pretty.py
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions hypothesis-python/tests/cover/test_pretty.py
Expand Up @@ -50,6 +50,7 @@
import re
import warnings
from collections import Counter, OrderedDict, defaultdict, deque
from enum import Enum

import pytest

Expand Down Expand Up @@ -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"