{"id":16346229,"url":"https://github.com/jorenham/classy-decorators","last_synced_at":"2025-11-08T09:30:26.486Z","repository":{"id":56081270,"uuid":"313278030","full_name":"jorenham/classy-decorators","owner":"jorenham","description":"Hassle-free creation of decorators for functions and methods, OO-style.","archived":false,"fork":false,"pushed_at":"2020-11-26T14:56:59.000Z","size":60,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-29T11:47:56.982Z","etag":null,"topics":["python-decorators"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/classy-decorators/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jorenham.png","metadata":{"files":{"readme":"README.md","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":"2020-11-16T11:18:58.000Z","updated_at":"2020-11-26T14:57:03.000Z","dependencies_parsed_at":"2022-08-15T12:50:18.690Z","dependency_job_id":null,"html_url":"https://github.com/jorenham/classy-decorators","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jorenham%2Fclassy-decorators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jorenham%2Fclassy-decorators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jorenham%2Fclassy-decorators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jorenham%2Fclassy-decorators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jorenham","download_url":"https://codeload.github.com/jorenham/classy-decorators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239550272,"owners_count":19657541,"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":["python-decorators"],"created_at":"2024-10-11T00:34:49.682Z","updated_at":"2025-11-08T09:30:26.408Z","avatar_url":"https://github.com/jorenham.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Classy Decorators\n\n[![PyPI version](https://badge.fury.io/py/classy-decorators.svg)](https://badge.fury.io/py/classy-decorators)\n\nHassle-free creation of decorators for functions and methods, OO-style.\n\n## Features\n\n - One decorator to rule them all; it works on functions, methods, \n classmethods and staticmethods.\n - Easily define parameters with a dataclass-like annotated attribute syntax.\n - Runtime type-checking of decorator paremeters.\n - For decorators with all-default or no parameters, parentheses are optional: \n `@spam() == @spam`\n - Inheritance is supported.\n - Any decorator parameters and instance attributes are accessible (as copies) \n in bound methods as well; no need to worry about those pesky descriptors.\n - MyPy compatible and 100% test coverage.\n\n\n\n## Dependencies\n\nPython 3.8 or 3.9.\n\n\n## Install\n\n```bash\npip install classy-decorators\n```\n\nfor better runtime type checking:\n\n```bash\npip install classy-decorators[typeguard]\n```\n\n\n## Usage \n\n*The following code can also be found in the \n[examples](https://github.com/jorenham/classy-decorators/tree/master/examples).*\n\nCreate a decorator by subclassing `classy_decorators.Decorator`. \nYou can override the the decorated function or method using the \n`__call_inner__` method (`__call__` is meant for internal use only and should \nnot be used for this).\n\n### Simple decorator\n\n```python\nfrom classy_decorators import Decorator\n\nclass Double(Decorator):\n    def __call_inner__(self, *args, **kwargs) -\u003e float:\n        return super().__call_inner__(*args, **kwargs) * 2\n```\n\nTo see it in action, let's decorate a function:\n\n```python\n@Double\ndef add(a, b):\n    return a + b\n\nassert add(7, 14) == 42\n```\n\nYou can also decorate methods and classmethods:\n\n```python\nclass AddConstant:\n    default_constant = 319\n\n    def __init__(self, constant=default_constant):\n        self.constant = constant\n\n    @Double\n    def add_to(self, value):\n        return value + self.constant\n\n    @Double\n    @classmethod\n    def add_default(cls, value):\n        return value + cls.default_constant\n\nassert AddConstant(7).add_to(14) == 42\nassert AddConstant.add_default(14) == 666\n```\n\n### Decorator parameters\n\nOur `Double` decorator is pretty nice, but we can do better! So let's create a\ndecorator that is able to multiply results by any number instead of only by `2`:\n\n```python\nclass Multiply(Decorator):\n    factor: int\n\n    def __call_inner__(self, *args, **kwargs) -\u003e float:\n        return super().__call_inner__(*args, **kwargs) * self.factor\n```\n\nBy simply setting the type-annotated `factor` attribute, we can use it as \ndecorator parameter. If you are familiar with \n[dataclasses](https://docs.python.org/3/library/dataclasses.html), you can see\nthat this is very similar to defining dataclass fields.\n\n\n```python\n@Multiply(2)\ndef add_and_double(a, b):\n    return a + b\n\n@Multiply(factor=3)\ndef add_and_triple(a, b):\n    return a + b\n\nassert add_and_double(8, 15) == 46\nassert add_and_triple(8, 15) == 69\n```\n\n\n### Default parameters and inheritance\n\nIt's classy to be DRY, so let's combine our `Double` and `Multiply` decorators \ninto one that multiplies by `2`, unless specified otherwise:\n\n```python\nclass DoubleOrMultiply(Multiply):\n    factor = 2\n\n@DoubleOrMultiply\ndef add_and_double(a, b):\n    return a + b\n\n@DoubleOrMultiply(factor=3)\ndef add_and_triple(a, b):\n    return a + b\n\nassert add_and_double(7, 14) == 42\nassert add_and_triple(8, 15) == 69\n```\n\n\n### Advanced dataclass methods \n\nThe `Decorator` base class provided, aside from `__call_inner__`, two other\ninterface methods you can override:\n\n- `Decorator.__decorate__(self, **params)`, which is called just after a \nfunction method is decorated, with all decorator parameter values or defaults as\nkeyword arguments, i.e. `DoubleOrMultiply.__decorate__(self, factor: int = 2)`.\n- `Decorator.__bind__(self, instance_or_class)`, which is called when a method \n(not for functions), is bound to an instance, or when a class/static method is \nbound to a class.\n\nAdditionally, these properties can be used for figuring out what's been \ndecorated:\n\n- `is_function`\n- `is_method`; either an instance, class- or static method\n- `is_instancemethod` \n- `is_classmethod`\n- `is_staticmethod`\n\nAnd for methods, `is_bound` and `is_unbound` are provided.\n\nIf you're looking for the original wrapped function, you can find it at \n`__func__`.\n\n---\n\n*Classy, eh?*\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjorenham%2Fclassy-decorators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjorenham%2Fclassy-decorators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjorenham%2Fclassy-decorators/lists"}