{"id":15673164,"url":"https://github.com/ikalnytskyi/singledispatchmethod","last_synced_at":"2025-10-21T06:09:43.377Z","repository":{"id":52058075,"uuid":"201773956","full_name":"ikalnytskyi/singledispatchmethod","owner":"ikalnytskyi","description":"Backport of 'functools.singledispatchmethod' from Python 3.8 to Python 2.7 - 3.7.","archived":true,"fork":false,"pushed_at":"2019-08-12T07:15:12.000Z","size":7,"stargazers_count":15,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-08T02:03:53.277Z","etag":null,"topics":["backport","python","singledispatch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ikalnytskyi.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":"2019-08-11T14:18:07.000Z","updated_at":"2024-10-24T18:11:52.000Z","dependencies_parsed_at":"2022-09-19T09:01:50.296Z","dependency_job_id":null,"html_url":"https://github.com/ikalnytskyi/singledispatchmethod","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ikalnytskyi%2Fsingledispatchmethod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ikalnytskyi%2Fsingledispatchmethod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ikalnytskyi%2Fsingledispatchmethod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ikalnytskyi%2Fsingledispatchmethod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ikalnytskyi","download_url":"https://codeload.github.com/ikalnytskyi/singledispatchmethod/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242851770,"owners_count":20195717,"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":["backport","python","singledispatch"],"created_at":"2024-10-03T15:38:02.164Z","updated_at":"2025-10-21T06:09:43.069Z","avatar_url":"https://github.com/ikalnytskyi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"singledispatchmethod\n====================\n\nBackport of ``@functools.singledispatchmethod`` decorator [1]_ from\nPython 3.8 to Python 2.7-3.7. These are merely ~30 lines of code, but\nwhy bother yourself with copypasta?\n\n.. code:: bash\n\n   $ pip install singledispatchmethod\n\nThe decorator transforms a method into a single-dispatch [2]_ generic\nfunction [3]_. Note that since the dispatch happens on the type of the\nfirst non-self or non-cls argument, you have to create your function\naccordingly:\n\n.. code:: python\n\n    from singledispatchmethod import singledispatchmethod\n\n    class Negator:\n\n        @singledispatchmethod\n        def neg(self, arg):\n            raise NotImplementedError(\"Cannot negate a\")\n\n        @neg.register\n        def _(self, arg: int):\n            return -arg\n\n        @neg.register\n        def _(self, arg: bool):\n            return not arg\n\n``@singledispatchmethod`` supports nesting with other decorators such as\n``@classmethod``. However, in order to expose ``dispatcher.register``,\n``@singledispatchmethod`` must be the *outer most* decorator. Here is\nthe ``Negator`` class with the ``neg`` methods being class bound:\n\n.. code:: python\n\n    from singledispatchmethod import singledispatchmethod\n\n    class Negator:\n\n        @singledispatchmethod\n        @classmethod\n        def neg(cls, arg):\n            raise NotImplementedError(\"Cannot negate a\")\n\n        @neg.register\n        @classmethod\n        def _(cls, arg: int):\n            return -arg\n\n        @neg.register\n        @classmethod\n        def _(cls, arg: bool):\n            return not arg\n\nThe same pattern can be used for other similar decorators, such as\n``@staticmethod`` or ``@abstractmethod``. Please note, since\n``@singledispatchmethod`` decorator is based on\n``@functools.singledispatch``, type annotations are supported by\n``dispatcher.register`` only since Python 3.7.\n\n.. [1] https://docs.python.org/3.8/library/functools.html#functools.singledispatchmethod\n.. [2] https://docs.python.org/3.8/glossary.html#term-single-dispatch\n.. [3] https://docs.python.org/3.8/glossary.html#term-generic-function\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fikalnytskyi%2Fsingledispatchmethod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fikalnytskyi%2Fsingledispatchmethod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fikalnytskyi%2Fsingledispatchmethod/lists"}