Skip to main content

typed-proxy

Build status

A Python package that provides a metaclass for creating proxy classes with type annotations.

Help

See documentation for more details

Install

To install typed-proxy, simply use pip:

$ pip install typed-proxy

Examples

Basic Usage

You can simply create a proxy class using the Proxy metaclass. The following example shows how to define a class Bar that proxies the class Foo.

from proxy import Proxy


class Foo:
    def __init__(self, a: int, b: int) -> None:
        self.a = a
        self.b = b

    def f1(self) -> None:
        print(f"Foo(a={self.a}, b={self.b}).f1()")

    def f2(self) -> None:
        print(f"Foo(a={self.a}, b={self.b}).f2()")

    def f3(self) -> None:
        print(f"Foo(a={self.a}, b={self.b}).f3()")


class Bar(Foo, metaclass=Proxy):
    def f2(self) -> None:
        print(f"Bar(a={self.a}, b={self.b}).f2()")


bar = Bar.proxy(Foo(1, 2))
bar.f1() # Foo(a=1, b=2).f1()
bar.f2() # Bar(a=1, b=2).f2()
bar.f3() # Foo(a=1, b=2).f3()

Accessing the Proxied Object

You can access the proxied object via the __proxied__ attribute in the proxy class.

class Bar(Foo, metaclass=Proxy):
    def f2(self) -> None:
        print(f"Bar(a={self.a}, b={self.b}).f2()")
        self.__proxied__.f2()


bar = Bar.proxy(Foo(1, 2))
bar.f1()  # Foo(a=1, b=2).f1()
bar.f2()  # Bar(a=1, b=2).f2() / Foo(a=1, b=2).f2()
bar.f3()  # Foo(a=1, b=2).f3()

Explicitly Defining __proxied__

Since type checkers cannot statically resolve __proxied__, they might raise an issue such as error: "Bar" has no attribute "__proxied__". To resolve this, you can explicitly define the __proxied__ attribute as follows.

class Bar(Foo, metaclass=Proxy):
    __proxied__: Foo

    def f2(self) -> None:
        print(f"Bar(a={self.a}, b={self.b}).f2()")
        self.__proxied__.f2()


bar = Bar.proxy(Foo(1, 2))
bar.f1()  # Foo(a=1, b=2).f1()
bar.f2()  # Bar(a=1, b=2).f2() / Foo(a=1, b=2).f2()
bar.f3()  # Foo(a=1, b=2).f3()

Custom __init__() for Proxy Class

You can define a custom __init__() method in the proxy class and provide arguments using the proxy() method with *args and **kwargs.

class Bar(Foo, metaclass=Proxy):
    def __init__(self, a: int, b: int, c: int):
        self.b = a
        self.c = b
        self.d = c

    def f3(self) -> None:
        print(f"Bar(a={self.a}, b={self.b}, c={self.c}, d={self.d}).f3()")


bar = Bar.proxy(Foo(1, 2), 3, 4, 5)
bar.f1()  # Foo(a=1, b=2).f1()
bar.f2()  # Foo(a=1, b=2).f2()
bar.f3()  # Bar(a=1, b=3, c=4, d=5).f3()

Metadata

Release files for typed-proxy 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typed-proxy 1.0.1
File Size Uploaded Provenance
typed_proxy-1.0.1.tar.gz 4.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for typed-proxy 1.0.1
File Interpreter ABI Platform
typed_proxy-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.9 kB

Release files / typed_proxy-1.0.1.tar.gz

Download URL typed_proxy-1.0.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a22e1945974d7ea84dbf7f57ae55e9c55655ea1055eff6fecde04e648b834af3
BLAKE2b-256 checksum
How to use checksums
bf15522130c35ba35174e97aaf9108db8b4d05803bb7584248ce1a71b189f4e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / typed_proxy-1.0.1-py3-none-any.whl

Download URL typed_proxy-1.0.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
369f4800cdd65e77db9386a1105a5ce823267e9cba5176fcb4e12d7571cdb92f
BLAKE2b-256 checksum
How to use checksums
491d58d1f9acd15d4fdd56727da844253d40ad660886f84ce7b7fe92f599004a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Security for typed-proxy 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page