calypso.utils.proxy_attrs module

calypso.utils.proxy_attrs.proxy_attrs(hinted_class: Type[C])

A decorator to proxy attributes and methods for class

Parameters:

hinted_class (type) – The class to be proxied. Only hinted attributes and normal methods are proxied, and will be called from _data of the decorated class.

Notes

This decorator is usually used to proxy dataclass.

Examples

>>> class Data:
...     x: str
...     def __init__(self):
...         x = "hinted_x"
...         y = "unhinted_y"
...     def fn(self):
...         return "data"
...     @property
...     def prop(self):
...         return "prop"
...
>>> @proxy_attrs(Data)
... class ProxyData:
...     def __init__(self):
...         self._data = Data()
...
>>> proxyed_data = ProxyData()
>>> proxyed_data.fn()
'data'
>>> proxyed_data.x
'hinted_x'
>>> proxied_data.y  # not hinted attributes
AttributeError: 'ProxyData' object has no attribute 'y'
>>> proxied_data.prop  # not a normal method
AttributeError: 'ProxyData' object has no attribute 'prop'