{"id":22449121,"url":"https://github.com/askerka/polymorphism","last_synced_at":"2025-03-27T12:14:38.016Z","repository":{"id":57454070,"uuid":"163423763","full_name":"askerka/polymorphism","owner":"askerka","description":"Ad hoc polymorphism for Python classes!","archived":false,"fork":false,"pushed_at":"2019-02-24T12:01:47.000Z","size":22,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-20T06:35:00.994Z","etag":null,"topics":["annotations","dispatching","inheritance","oop","overloading","polymorphism","python3","typing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/askerka.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-28T15:21:56.000Z","updated_at":"2020-09-05T14:22:12.000Z","dependencies_parsed_at":"2022-08-29T15:00:15.599Z","dependency_job_id":null,"html_url":"https://github.com/askerka/polymorphism","commit_stats":null,"previous_names":["asduj/polymorphism"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/askerka%2Fpolymorphism","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/askerka%2Fpolymorphism/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/askerka%2Fpolymorphism/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/askerka%2Fpolymorphism/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/askerka","download_url":"https://codeload.github.com/askerka/polymorphism/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245841754,"owners_count":20681195,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotations","dispatching","inheritance","oop","overloading","polymorphism","python3","typing"],"created_at":"2024-12-06T05:07:46.639Z","updated_at":"2025-03-27T12:14:37.997Z","avatar_url":"https://github.com/askerka.png","language":"Python","readme":".. highlight:: python\n\nAd hoc polymorphism for Python classes!\n=====================================================\n\nInstallation\n------------\n::\n\n    pip install polymorphism\n\n``polymorphism`` support python 3.4+\n\nUsage\n-----\nTo use the ``polymorphism`` simply inherit from the ``Polymorphism`` class::\n\n\n    from polymorphism import Polymorphism\n\n\n    class Simple(Polymorphism):\n        def calc(self, x: int, y: int) -\u003e None:\n            pass\n\n        def calc(self, x: float, y: float) -\u003e None:\n            pass\n\nOr use it as metaclass::\n\n    from polymorphism import PolymorphismMeta\n\n    class Simple(metaclass=PolymorphismMeta):\n        ...\n\n\nSometimes adding another class to the inheritance is undesirable, then you can use the ``overload`` decorator::\n\n    from polymorphism import overload\n\n\n    class Simple(Polymorphism):\n        @overload\n        def calc(self, x: int, y: int) -\u003e None:\n            pass\n\n        @calc.overload\n        def calc(self, x: float, y: float) -\u003e None:\n            pass\n\nThe only difference between using a decorator and inheriting it is checking for method shading. With ``overload`` the next example will not raise exception::\n\n    from polymorphism import overload\n\n\n    class Simple(Polymorphism):\n        @overload\n        def calc(self, x: int, y: int) -\u003e None:\n            pass\n\n        calc = 5\n\nAnd ``calc`` would be only the attribute.\n\nWhy?\n----\nThe idea to implement polymorphism is not new. Many libraries `implement \u003chttps://github.com/mrocklin/multipledispatch\u003e`_ this idea. Even the `standard library \u003chttp://docs.python.org/3.4/library/functools.html#functools.singledispatch\u003e`_ has an implementation.\n\nBut they do not support use with classes or standard type annotation.\n\nThe basic idea of the implementation was inspired by the great book `Python Cookbook 3rd Edition \u003chttp://shop.oreilly.com/product/0636920027072.do\u003e`_ by David Beazley and Brian K. Jones. But the implementation in the book did not support usage of keyword arguments!\n\nAdvantages\n----------\n* Use standard and custom descriptors\n* Use naming (keyword) arguments\n* Checks for:\n\n  * Arguments for variable length\n  * Missed argument annotation\n  * Name of wrapped function of descriptor\n  * Shading method by attribute or data descriptor (like ``property``)\n  * Redefining the method with the same types\n\n* Using any name for instance, not only ``self``\n\nFor all checks is raised ``TypeError`` exception.\n\nLimitations\n-----------\n\n* Simple types for dispatching\n* ``overload`` should be top of decorators\n* Custom descriptor should save wrapped function  under \"__wrapped__\" name\n* Obvious, method argument can't be variable length (\\* and \\*\\*)\n\n\nExamples\n--------\nThere are no restrictions on the use of the number of decorators, you only need to comply the naming convention.\n\nFor example::\n\n    class Simple(Polymorphism):\n        def calc(self, x: int, y: int) -\u003e None:\n            pass\n\n        @classmethod\n        def calc(cls, x: float, y: float) -\u003e None:\n            pass\n\n        @staticmethod\n        def calc(x: str, y: str) -\u003e None:\n            pass\n\n    Simple().calc(1.0, y=2.0)\n\nWhile use ``overload`` decorator place it on top::\n\n    class Simple:\n        @overload\n        def calc(self, x: int, y: int) -\u003e None:\n            pass\n\n        @calc.overload\n        @classmethod\n        def calc_float(cls, x: float, y: float) -\u003e None:\n            pass\n\n        @calc.overload\n        @staticmethod\n        def calc_str(x: str, y: str) -\u003e None:\n            pass\n\nWith ``overload`` only first method name matter. Other methods can have any other names.\n\n``polymorphism`` checks the class at the creation time::\n\n    class Simple(Polymorphism):\n        def calc(self, x: int, y: int) -\u003e None:\n            pass\n\n        def calc(self, x: int, y: int, z: int = 3) -\u003e None:\n            pass\n\nThe above example will raise ``TypeError`` exception because ``calc`` method overloaded with ``z`` parameter with default value and it is impossible distinct last method from first.\n\n``polymorphism`` will raise ``TypeError`` exception on any wrong overloading, so you don't need worry about correctness of it.\n\nSee more examples in `tests.py \u003chttps://github.com/asduj/polymorphism/blob/master/tests.py\u003e`_.\n\nTo-do\n-----\n\n* Complex and builtin types for dispatching like ``List[int]`` or ``Number``","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faskerka%2Fpolymorphism","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faskerka%2Fpolymorphism","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faskerka%2Fpolymorphism/lists"}