Skip to content

Commit

Permalink
Added decorator signature transfer to AdaptedDecorator meta-decorator…
Browse files Browse the repository at this point in the history
… class
  • Loading branch information
timbernat committed Apr 24, 2024
1 parent b8413a9 commit 2fce2be
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions polymerist/genutils/decorators/meta.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''Decorators for modifying other decorators'''

from typing import Concatenate, Callable, ParamSpec, TypeAlias, TypeVar
from functools import update_wrapper
from functools import update_wrapper, wraps

from ..typetools.parametric import C, O, P, R, Args, KWArgs
Decorator : TypeAlias = Callable[[Callable[P, R]], Callable[P, R]]
Expand All @@ -11,16 +11,16 @@
def extend_to_methods(dec : Decorator) -> Decorator:
'''Meta-decorator; modifies an existing decorator definition to be transferrable to methods with no additional code
The modified decorator can be used interchangably to decorate both ordinary functions AND methods of classes'''
class AdaptedDecorator:
__doc__ = dec.__doc__ # copy docstring for annotation
ReturnSignature = dec.__annotations__.get('return')

@wraps(dec, updated=()) # transfer decorator signature to decorator adapter class, without updating the __dict__ field
class AdaptedDecorator:
def __init__(self, funct : Callable[P, R]) -> None:
'''Record function'''
self.funct = funct
update_wrapper(self, funct) # equivalent to functools.wraps, transfers docstring, module, etc. for documentation
# TODO : figure out how to transfer decorator signature updates to this level

def __call__(self, *args : Args, **kwargs : KWArgs) -> dec.__annotations__.get('return'): # TODO : fix this to reflect the decorator's return signature
def __call__(self, *args : Args, **kwargs : KWArgs) -> ReturnSignature: # TODO : fix this to reflect the decorator's return signature
'''Apply decorator to function, then call decorated function'''
return dec(self.funct)(*args, **kwargs)

Expand Down

0 comments on commit 2fce2be

Please sign in to comment.