Skip to content

Commit

Permalink
Fix SetterAwareType if __slots__ is tuple.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkaklarck committed Oct 10, 2022
1 parent ec2e07a commit b2093b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/robot/utils/setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def __set__(self, instance, value):
class SetterAwareType(type):

def __new__(cls, name, bases, dct):
slots = dct.get('__slots__')
if slots is not None:
if '__slots__' in dct:
slots = list(dct['__slots__'])
for item in dct.values():
if isinstance(item, setter):
slots.append(item.attr_name)
dct['__slots__'] = slots
return type.__new__(cls, name, bases, dct)
22 changes: 17 additions & 5 deletions utest/utils/test_setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
from robot.utils import setter, SetterAwareType


class BaseWithMeta(metaclass=SetterAwareType):
__slots__ = []


class ExampleWithSlots(BaseWithMeta):
class ExampleWithSlots(metaclass=SetterAwareType):
__slots__ = []

@setter
Expand Down Expand Up @@ -54,6 +50,22 @@ def setUp(self):
def test_set_other_attr(self):
assert_raises(AttributeError, setattr, self.item, 'other_attr', 1)

def test_slots_as_tuple(self):
class XY(metaclass=SetterAwareType):
__slots__ = ('x',)

def __init__(self, x, y):
self.x = x
self.y = y

@setter
def y(self, y):
return y.upper()

xy = XY('x', 'y')
assert_equal((xy.x, xy.y), ('x', 'Y'))
assert_raises(AttributeError, setattr, xy, 'z', 'z')


if __name__ == '__main__':
unittest.main()

0 comments on commit b2093b8

Please sign in to comment.