From 1d21609c1444edfe8f6365190c3f7ba7cb2dfb37 Mon Sep 17 00:00:00 2001 From: Tin Tvrtkovic Date: Wed, 16 Mar 2016 22:28:43 +0100 Subject: [PATCH] Fix reprs. --- src/attr/_make.py | 12 +++++++++++- tests/test_make.py | 16 ++++++++++------ tests/test_slots.py | 3 +++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/attr/_make.py b/src/attr/_make.py index 9f85bb76..7b860fcd 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -210,7 +210,17 @@ def attributes(maybe_cl=None, these=None, repr_ns=None, # It might not actually be in there, f.e. if using 'these'. cl_dict.pop(ca_name, None) cl_dict.pop('__dict__', None) - cl = type(cl.__name__, cl.__bases__, cl_dict) + + if repr_ns is None: + qualname = getattr(cl, "__qualname__", None) + if qualname is not None: + class_name = qualname.rsplit(">.", 1)[-1] + else: + class_name = cl.__name__ + else: + class_name = cl.__name__ + + cl = type(class_name, cl.__bases__, cl_dict) return cl diff --git a/tests/test_make.py b/tests/test_make.py index fef88a0f..e3051543 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -246,26 +246,30 @@ class TestAttributes(object): assert sentinel == getattr(C, method_name) @pytest.mark.skipif(not PY3, reason="__qualname__ is PY3-only.") - def test_repr_qualname(self): + @pytest.mark.parametrize('slots_outer', [True, False]) + @pytest.mark.parametrize('slots_inner', [True, False]) + def test_repr_qualname(self, slots_outer, slots_inner): """ On Python 3, the name in repr is the __qualname__. """ - @attributes + @attributes(slots=slots_outer) class C(object): - @attributes + @attributes(slots=slots_inner) class D(object): pass assert "C.D()" == repr(C.D()) assert "GC.D()" == repr(GC.D()) - def test_repr_fake_qualname(self): + @pytest.mark.parametrize('slots_outer', [True, False]) + @pytest.mark.parametrize('slots_inner', [True, False]) + def test_repr_fake_qualname(self, slots_outer, slots_inner): """ Setting repr_ns overrides a potentially guessed namespace. """ - @attributes + @attributes(slots=slots_outer) class C(object): - @attributes(repr_ns="C") + @attributes(repr_ns="C", slots=slots_inner) class D(object): pass assert "C.D()" == repr(C.D()) diff --git a/tests/test_slots.py b/tests/test_slots.py index cdf5dcaf..d6c26c87 100644 --- a/tests/test_slots.py +++ b/tests/test_slots.py @@ -68,6 +68,9 @@ def test_slots_being_used(): assert non_slot_instance.method() == 1 assert slot_instance.method() == 1 + assert attr.fields(C1Slots) == attr.fields(C1) + assert attr.asdict(slot_instance) == attr.asdict(non_slot_instance) + def test_basic_attr_funcs(): """Test basic attr functionality on a simple slots class."""