typed-proxy


Nametyped-proxy JSON
Version 1.0.1 PyPI version JSON
download
home_pagehttps://github.com/jonghwanhyeon/typed-proxy
SummaryA Python package that provides a metaclass for creating proxy classes with type annotations.
upload_time2024-08-02 13:15:16
maintainerNone
docs_urlNone
authorJonghwan Hyeon
requires_python>=3.7
licenseMIT
keywords metaclass proxy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # typed-proxy
![Build status](https://github.com/jonghwanhyeon/typed-proxy/actions/workflows/publish.yml/badge.svg)

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

## Help
See [documentation](https://typed-proxy.readthedocs.io) for more details

## Install
To install **typed-proxy**, simply use pip:

```console
$ 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`.

```python
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.

```python
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.

```python
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`.

```python
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()
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jonghwanhyeon/typed-proxy",
    "name": "typed-proxy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "metaclass, proxy",
    "author": "Jonghwan Hyeon",
    "author_email": "jonghwanhyeon93@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/bf/15/522130c35ba35174e97aaf9108db8b4d05803bb7584248ce1a71b189f4e2/typed_proxy-1.0.1.tar.gz",
    "platform": null,
    "description": "# typed-proxy\n![Build status](https://github.com/jonghwanhyeon/typed-proxy/actions/workflows/publish.yml/badge.svg)\n\nA Python package that provides a metaclass for creating proxy classes with type annotations.\n\n## Help\nSee [documentation](https://typed-proxy.readthedocs.io) for more details\n\n## Install\nTo install **typed-proxy**, simply use pip:\n\n```console\n$ pip install typed-proxy\n```\n\n## Examples\n### Basic Usage\nYou 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`.\n\n```python\nfrom proxy import Proxy\n\n\nclass Foo:\n    def __init__(self, a: int, b: int) -> None:\n        self.a = a\n        self.b = b\n\n    def f1(self) -> None:\n        print(f\"Foo(a={self.a}, b={self.b}).f1()\")\n\n    def f2(self) -> None:\n        print(f\"Foo(a={self.a}, b={self.b}).f2()\")\n\n    def f3(self) -> None:\n        print(f\"Foo(a={self.a}, b={self.b}).f3()\")\n\n\nclass Bar(Foo, metaclass=Proxy):\n    def f2(self) -> None:\n        print(f\"Bar(a={self.a}, b={self.b}).f2()\")\n\n\nbar = Bar.proxy(Foo(1, 2))\nbar.f1() # Foo(a=1, b=2).f1()\nbar.f2() # Bar(a=1, b=2).f2()\nbar.f3() # Foo(a=1, b=2).f3()\n```\n\n### Accessing the Proxied Object\nYou can access the proxied object via the `__proxied__` attribute in the proxy class.\n\n```python\nclass Bar(Foo, metaclass=Proxy):\n    def f2(self) -> None:\n        print(f\"Bar(a={self.a}, b={self.b}).f2()\")\n        self.__proxied__.f2()\n\n\nbar = Bar.proxy(Foo(1, 2))\nbar.f1()  # Foo(a=1, b=2).f1()\nbar.f2()  # Bar(a=1, b=2).f2() / Foo(a=1, b=2).f2()\nbar.f3()  # Foo(a=1, b=2).f3()\n```\n\n### Explicitly Defining `__proxied__`\nSince type checkers cannot statically resolve `__proxied__`, they might raise an issue such as `error: \"Bar\" has no attribute \"__proxied__\"`.\nTo resolve this, you can explicitly define the `__proxied__` attribute as follows.\n\n```python\nclass Bar(Foo, metaclass=Proxy):\n    __proxied__: Foo\n\n    def f2(self) -> None:\n        print(f\"Bar(a={self.a}, b={self.b}).f2()\")\n        self.__proxied__.f2()\n\n\nbar = Bar.proxy(Foo(1, 2))\nbar.f1()  # Foo(a=1, b=2).f1()\nbar.f2()  # Bar(a=1, b=2).f2() / Foo(a=1, b=2).f2()\nbar.f3()  # Foo(a=1, b=2).f3()\n```\n\n### Custom `__init__()` for Proxy Class\nYou can define a custom `__init__()` method in the proxy class and provide arguments using the `proxy()` method with `*args` and `**kwargs`.\n\n```python\nclass Bar(Foo, metaclass=Proxy):\n    def __init__(self, a: int, b: int, c: int):\n        self.b = a\n        self.c = b\n        self.d = c\n\n    def f3(self) -> None:\n        print(f\"Bar(a={self.a}, b={self.b}, c={self.c}, d={self.d}).f3()\")\n\n\nbar = Bar.proxy(Foo(1, 2), 3, 4, 5)\nbar.f1()  # Foo(a=1, b=2).f1()\nbar.f2()  # Foo(a=1, b=2).f2()\nbar.f3()  # Bar(a=1, b=3, c=4, d=5).f3()\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python package that provides a metaclass for creating proxy classes with type annotations.",
    "version": "1.0.1",
    "project_urls": {
        "Homepage": "https://github.com/jonghwanhyeon/typed-proxy"
    },
    "split_keywords": [
        "metaclass",
        " proxy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "491d58d1f9acd15d4fdd56727da844253d40ad660886f84ce7b7fe92f599004a",
                "md5": "9c44ae22fbd255a2c9a1edd90ae734ba",
                "sha256": "369f4800cdd65e77db9386a1105a5ce823267e9cba5176fcb4e12d7571cdb92f"
            },
            "downloads": -1,
            "filename": "typed_proxy-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9c44ae22fbd255a2c9a1edd90ae734ba",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 5071,
            "upload_time": "2024-08-02T13:15:15",
            "upload_time_iso_8601": "2024-08-02T13:15:15.597659Z",
            "url": "https://files.pythonhosted.org/packages/49/1d/58d1f9acd15d4fdd56727da844253d40ad660886f84ce7b7fe92f599004a/typed_proxy-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bf15522130c35ba35174e97aaf9108db8b4d05803bb7584248ce1a71b189f4e2",
                "md5": "cbcca5e7f4004a8dcac97af21edf26fd",
                "sha256": "a22e1945974d7ea84dbf7f57ae55e9c55655ea1055eff6fecde04e648b834af3"
            },
            "downloads": -1,
            "filename": "typed_proxy-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "cbcca5e7f4004a8dcac97af21edf26fd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 4873,
            "upload_time": "2024-08-02T13:15:16",
            "upload_time_iso_8601": "2024-08-02T13:15:16.915940Z",
            "url": "https://files.pythonhosted.org/packages/bf/15/522130c35ba35174e97aaf9108db8b4d05803bb7584248ce1a71b189f4e2/typed_proxy-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-02 13:15:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jonghwanhyeon",
    "github_project": "typed-proxy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "typed-proxy"
}
        
Elapsed time: 3.22652s