Skip to content

robot.utils.setter

setter

setter(method: Callable[[T, V], A])

Bases: Generic[T, V, A]

Modify instance attributes only when they are set, not when they are get.

Usage::

1
2
3
@setter
def source(self, source: str|Path) -> Path:
    return source if isinstance(source, Path) else Path(source)

The setter method is called when the attribute is assigned like::

1
instance.source = 'example.txt'

and the returned value is stored in the instance in an attribute like _setter__source. When the attribute is accessed, the stored value is returned.

The above example is equivalent to using the standard property as follows. The main benefit of using setter is that it avoids a dummy getter method::

1
2
3
4
5
6
7
@property
def source(self) -> Path:
    return self._source

@source.setter
def source(self, source: src|Path):
    self._source = source if isinstance(source, Path) else Path(source)

When using setter with __slots__, the special _setter__xxx attributes needs to be added to __slots__ as well. The provided :class:SetterAwareType metaclass can take care of that automatically.

Source code in src/robot/utils/setter.py
def __init__(self, method: Callable[[T, V], A]):
    self.method = method
    self.attr_name = '_setter__' + method.__name__
    self.__doc__ = method.__doc__

SetterAwareType

Bases: type

Metaclass for adding attributes used by :class:setter to __slots__.