Skip to content

API Reference

Proxied

A metaclass that proxies attribute access to another object.

Source code in proxy/proxy.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Proxied(type):
    """A metaclass that proxies attribute access to another object."""

    def __new__(cls, name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> Any:
        """Create a new class with proxied attribute access."""
        if "__init__" not in namespace:
            namespace["__init__"] = cls.__proxied_init__

        namespace["__getattribute__"] = cls.__proxied_getattribute__
        namespace["__repr__"] = cls.__proxied_repr__

        return super().__new__(cls, name, bases, namespace)

    def proxy(cls: type[T], target: Any, *args: Any, **kwargs: Any) -> T:
        """Create a proxy instance for the given `target`.

        The given `target` can be accessed via `__proxied__` attribute.
        If a proxy class defines its own `__init__()` method, you can provide arguments using `*args` and `**kwargs`.

        Args:
            cls: The class of the proxy.
            target: The target object to be proxied.
            *args: Additional arguments for `__init__()` method of the proxy class.
            **kwargs: Additional keyword arguments for `__init__()` method of the proxy class.

        Returns:
            A proxy instance for the target object.
        """
        instance = cls(*args, **kwargs)
        setattr(instance, "__proxied__", target)
        return instance

    def __proxied_init__(self: object) -> None:  # noqa: D105
        pass

    def __proxied_getattribute__(self: object, name: str) -> Any:  # noqa: D105
        if _should_intercept(self, name):
            return object.__getattribute__(self, name)

        proxied = object.__getattribute__(self, "__proxied__")
        return object.__getattribute__(proxied, name)

    def __proxied_repr__(self: object) -> str:  # noqa: D105
        proxied = object.__getattribute__(self, "__proxied__")
        return f"{type(self).__name__}({proxied})"

__new__

__new__(
    name: str,
    bases: tuple[type, ...],
    namespace: dict[str, Any],
) -> Any

Create a new class with proxied attribute access.

Source code in proxy/proxy.py
31
32
33
34
35
36
37
38
39
def __new__(cls, name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> Any:
    """Create a new class with proxied attribute access."""
    if "__init__" not in namespace:
        namespace["__init__"] = cls.__proxied_init__

    namespace["__getattribute__"] = cls.__proxied_getattribute__
    namespace["__repr__"] = cls.__proxied_repr__

    return super().__new__(cls, name, bases, namespace)

proxy

proxy(target: Any, *args: Any, **kwargs: Any) -> T

Create a proxy instance for the given target.

The given target can be accessed via __proxied__ attribute. If a proxy class defines its own __init__() method, you can provide arguments using *args and **kwargs.

Parameters:

Name Type Description Default
cls type[T]

The class of the proxy.

required
target Any

The target object to be proxied.

required
*args Any

Additional arguments for __init__() method of the proxy class.

()
**kwargs Any

Additional keyword arguments for __init__() method of the proxy class.

{}

Returns:

Type Description
T

A proxy instance for the target object.

Source code in proxy/proxy.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def proxy(cls: type[T], target: Any, *args: Any, **kwargs: Any) -> T:
    """Create a proxy instance for the given `target`.

    The given `target` can be accessed via `__proxied__` attribute.
    If a proxy class defines its own `__init__()` method, you can provide arguments using `*args` and `**kwargs`.

    Args:
        cls: The class of the proxy.
        target: The target object to be proxied.
        *args: Additional arguments for `__init__()` method of the proxy class.
        **kwargs: Additional keyword arguments for `__init__()` method of the proxy class.

    Returns:
        A proxy instance for the target object.
    """
    instance = cls(*args, **kwargs)
    setattr(instance, "__proxied__", target)
    return instance

Proxy

A metaclass that combines Proxied and type.

Source code in proxy/proxy.py
75
76
class Proxy(Proxied, type):
    """A metaclass that combines [`Proxied`][proxy.Proxied] and [`type`][type]."""

__new__

__new__(
    name: str,
    bases: tuple[type, ...],
    namespace: dict[str, Any],
) -> Any

Create a new class with proxied attribute access.

Source code in proxy/proxy.py
31
32
33
34
35
36
37
38
39
def __new__(cls, name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> Any:
    """Create a new class with proxied attribute access."""
    if "__init__" not in namespace:
        namespace["__init__"] = cls.__proxied_init__

    namespace["__getattribute__"] = cls.__proxied_getattribute__
    namespace["__repr__"] = cls.__proxied_repr__

    return super().__new__(cls, name, bases, namespace)

proxy

proxy(target: Any, *args: Any, **kwargs: Any) -> T

Create a proxy instance for the given target.

The given target can be accessed via __proxied__ attribute. If a proxy class defines its own __init__() method, you can provide arguments using *args and **kwargs.

Parameters:

Name Type Description Default
cls type[T]

The class of the proxy.

required
target Any

The target object to be proxied.

required
*args Any

Additional arguments for __init__() method of the proxy class.

()
**kwargs Any

Additional keyword arguments for __init__() method of the proxy class.

{}

Returns:

Type Description
T

A proxy instance for the target object.

Source code in proxy/proxy.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def proxy(cls: type[T], target: Any, *args: Any, **kwargs: Any) -> T:
    """Create a proxy instance for the given `target`.

    The given `target` can be accessed via `__proxied__` attribute.
    If a proxy class defines its own `__init__()` method, you can provide arguments using `*args` and `**kwargs`.

    Args:
        cls: The class of the proxy.
        target: The target object to be proxied.
        *args: Additional arguments for `__init__()` method of the proxy class.
        **kwargs: Additional keyword arguments for `__init__()` method of the proxy class.

    Returns:
        A proxy instance for the target object.
    """
    instance = cls(*args, **kwargs)
    setattr(instance, "__proxied__", target)
    return instance

ABCProxy

A metaclass that combines Proxied and ABCMeta.

Source code in proxy/proxy.py
79
80
class ABCProxy(Proxied, ABCMeta):
    """A metaclass that combines [`Proxied`][proxy.Proxied] and [`ABCMeta`][abc.ABCMeta]."""

__new__

__new__(
    name: str,
    bases: tuple[type, ...],
    namespace: dict[str, Any],
) -> Any

Create a new class with proxied attribute access.

Source code in proxy/proxy.py
31
32
33
34
35
36
37
38
39
def __new__(cls, name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> Any:
    """Create a new class with proxied attribute access."""
    if "__init__" not in namespace:
        namespace["__init__"] = cls.__proxied_init__

    namespace["__getattribute__"] = cls.__proxied_getattribute__
    namespace["__repr__"] = cls.__proxied_repr__

    return super().__new__(cls, name, bases, namespace)

proxy

proxy(target: Any, *args: Any, **kwargs: Any) -> T

Create a proxy instance for the given target.

The given target can be accessed via __proxied__ attribute. If a proxy class defines its own __init__() method, you can provide arguments using *args and **kwargs.

Parameters:

Name Type Description Default
cls type[T]

The class of the proxy.

required
target Any

The target object to be proxied.

required
*args Any

Additional arguments for __init__() method of the proxy class.

()
**kwargs Any

Additional keyword arguments for __init__() method of the proxy class.

{}

Returns:

Type Description
T

A proxy instance for the target object.

Source code in proxy/proxy.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def proxy(cls: type[T], target: Any, *args: Any, **kwargs: Any) -> T:
    """Create a proxy instance for the given `target`.

    The given `target` can be accessed via `__proxied__` attribute.
    If a proxy class defines its own `__init__()` method, you can provide arguments using `*args` and `**kwargs`.

    Args:
        cls: The class of the proxy.
        target: The target object to be proxied.
        *args: Additional arguments for `__init__()` method of the proxy class.
        **kwargs: Additional keyword arguments for `__init__()` method of the proxy class.

    Returns:
        A proxy instance for the target object.
    """
    instance = cls(*args, **kwargs)
    setattr(instance, "__proxied__", target)
    return instance