We can start with a base such as this:
from functools import partial, update_wrapper
class Decorator:
"""A transparent decorator -- to be subclassed"""
def __new__(cls, func=None, **kwargs):
if func is None:
return partial(cls, **kwargs)
else:
self = super().__new__(cls)
self.func = func
for attr_name, attr_val in kwargs.items():
setattr(self, attr_name, attr_val)
return update_wrapper(self, func)
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)
It allows one to define decorator factories by subclassing and defining a __new__
method (that should call its parent).
Then, one can use __init_subclass__
to extract the desired arguments of that __new__
directly from attributes of the subclass, as such:
from inspect import Signature, Parameter
PK, KO = Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY
class Literal:
"""An object to indicate that the value should be considered literally"""
def __init__(self, val):
self.val = val
class Decora(Decorator):
_injected_deco_params = ()
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if '__new__' not in cls.__dict__: # if __new__ hasn't been defined in the subclass...
params = ([Parameter('self', PK), Parameter('func', PK, default=None)])
cls_annots = getattr(cls, '__annotations__', {})
injected_deco_params = set()
for attr_name in (a for a in cls.__dict__ if not a.startswith('__')):
attr_obj = cls.__dict__[attr_name] # get the attribute
if not isinstance(attr_obj, Literal):
setattr(cls, attr_name, attr_obj) # what we would have done anyway...
# ... but also add a parameter to the list of params
params.append(Parameter(attr_name, KO, default=attr_obj,
annotation=cls_annots.get(attr_name, Parameter.empty)))
injected_deco_params.add(attr_name)
else: # it is a Literal, so
setattr(cls, attr_name, attr_obj.val) # just assign the literal value
cls._injected_deco_params = injected_deco_params
def __new__(cls, func=None, **kwargs):
if cls._injected_deco_params and not set(kwargs).issubset(cls._injected_deco_params):
raise TypeError("TypeError: __new__() got unexpected keyword arguments: "
f"{kwargs.keys() - cls._injected_deco_params}")
if func is None:
return partial(cls, **kwargs)
else:
return Decorator.__new__(cls, func, **kwargs)
__new__.__signature__ = Signature(params)
cls.__new__ = __new__
It passes the tests listed in the question, but I'm not super experienced with this kind of object oriented magic, so would be curious to see alternative solutions.