Skip to content

Commit 8f93105

Browse files
authored
Merge pull request #4142 from HypothesisWorks/DRMacIver/qualname-in-pretty
Include namespace classes in pretty printing
2 parents ef9942d + 479ca98 commit 8f93105

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
This release improves pretty printing of nested classes to include the outer class name in their printed representation.

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,10 @@ def inner(obj, p, cycle):
582582
return inner
583583

584584

585+
def get_class_name(cls):
586+
return _safe_getattr(cls, "__qualname__", cls.__name__)
587+
588+
585589
def _set_pprinter_factory(start, end, basetype):
586590
"""Factory that returns a pprint function useful for sets and
587591
frozensets."""
@@ -600,7 +604,7 @@ def inner(obj, p, cycle):
600604
return p.text(start + "..." + end)
601605
if not obj:
602606
# Special case.
603-
p.text(basetype.__name__ + "()")
607+
p.text(get_class_name(basetype) + "()")
604608
else:
605609
step = len(start)
606610
with p.group(step, start, end):
@@ -733,7 +737,7 @@ def _repr_pprint(obj, p, cycle):
733737

734738

735739
def pprint_fields(obj, p, cycle, fields):
736-
name = obj.__class__.__name__
740+
name = get_class_name(obj.__class__)
737741
if cycle:
738742
return p.text(f"{name}(...)")
739743
with p.group(1, name + "(", ")"):
@@ -879,7 +883,7 @@ def _repr_dataframe(obj, p, cycle): # pragma: no cover
879883

880884

881885
def _repr_enum(obj, p, cycle):
882-
tname = type(obj).__name__
886+
tname = get_class_name(type(obj))
883887
if isinstance(obj, Flag):
884888
p.text(
885889
" | ".join(f"{tname}.{x.name}" for x in type(obj) if x & obj == x)

hypothesis-python/tests/cover/test_pretty.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,28 @@ def test_does_not_include_no_init_fields_in_attrs_printing():
876876
assert pretty.pretty(record) == "AttrsClassWithNoInitField(x=1)"
877877
record.y = 1
878878
assert pretty.pretty(record) == "AttrsClassWithNoInitField(x=1)"
879+
880+
881+
class Namespace:
882+
@dataclass
883+
class DC:
884+
x: int
885+
886+
@attrs.define
887+
class A:
888+
x: int
889+
890+
class E(Enum):
891+
A = 1
892+
893+
894+
NAMESPACED_VALUES = [
895+
Namespace.DC(x=1),
896+
Namespace.A(x=1),
897+
Namespace.E.A,
898+
]
899+
900+
901+
@pytest.mark.parametrize("obj", NAMESPACED_VALUES, ids=map(repr, NAMESPACED_VALUES))
902+
def test_includes_namespace_classes_in_pretty(obj):
903+
assert pretty.pretty(obj).startswith("Namespace.")

0 commit comments

Comments
 (0)