Skip to content

Commit

Permalink
Fix: TypeError when using properties, super(), and slots=True (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwist-sgr committed Jan 8, 2021
1 parent 72f94a3 commit 99d6348
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/747.change.rst
@@ -0,0 +1 @@
Fixed ``TypeError`` when using properties, ``super()``, and ``slots=True``.
7 changes: 7 additions & 0 deletions src/attr/_make.py
Expand Up @@ -795,6 +795,13 @@ def _create_slots_class(self):
# Class- and staticmethods hide their functions inside.
# These might need to be rewritten as well.
closure_cells = getattr(item.__func__, "__closure__", None)
elif isinstance(item, property):
# Workaround for property `super()` (PY3-only).
# There is no universal way for other descriptors.
if PY2:
closure_cells = None
else:
closure_cells = getattr(item.fget, "__closure__", None)
else:
closure_cells = getattr(item, "__closure__", None)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_slots.py
Expand Up @@ -689,3 +689,27 @@ def test_getstate_set_state_force_true(self, cls):
"""
assert None is not getattr(cls, "__getstate__", None)
assert None is not getattr(cls, "__setstate__", None)


@pytest.mark.skipif(PY2, reason="shortcut super() is PY3-only.")
def test_slots_super_property_get():
"""
In Python 3, the `super()` shortcut is allowed.
"""

@attr.s(slots=True)
class A(object):
x = attr.ib()

@property
def f(self):
return self.x

@attr.s(slots=True)
class B(A):
@property
def f(self):
return super().f ** 2

assert B(11).f == 121
assert B(17).f == 289

0 comments on commit 99d6348

Please sign in to comment.