Repository navigation
Keep a decorator's own body when it uses functools.wraps (#808) - #910
craigbarratt merged 2 commits into
Conversation
User decorators got the raw EvalFunc, so functools.wraps() copied the wrapped function's EvalFunc fields over the wrapper's and the wrapper ran the wrapped body instead of its own (custom-components#808). Decorators now get an EvalFuncVar, whose own fields are slots that wraps() can't overwrite, the EvalFuncVar a decorator returns is kept so __wrapped__ and other attributes survive, and __name__ and __doc__ work like Python's. Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
craigbarratt
left a comment
There was a problem hiding this comment.
Thanks @HoneyBearTech. Passing decorators an EvalFuncVar makes them behave much more like Python: __name__, __doc__, attributes set by decorators, vars() and @abstractmethod all now match CPython.
One fix: EvalFuncVarClassInst's class doc string hides the inherited __doc__ property, so obj.method.__doc__ returns "Class for a callable pyscript class instance function.", with or without wraps. Adding __doc__ = EvalFuncVar.__doc__ after its __slots__ fixes it.
A minor note, no change needed: since the returned function is renamed to the decorated name, a decorator that returns an existing function (def deco(fn): return other) renames other too.
EvalFuncVarClassInst's class doc string hid the __doc__ property it inherits from EvalFuncVar, so obj.method.__doc__ returned "Class for a callable pyscript class instance function." instead of the method's doc string, with or without functools.wraps. Point its __doc__ back at the inherited property, and test plain and wrapped methods against Python's results. Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
|
Thanks @craigbarratt! I added doc = EvalFuncVar.doc to EvalFuncVarClassInst, as you suggested. There's a test compared with CPython for plain, wraps-wrapped and undocumented methods, read both from the instance and from the class. It fails without the fix. And noted about a decorator that returns an existing function; I've left that as it is. |
|
Thanks @HoneyBearTech, looks great. Merging. |
Closes #808
What was broken
A decorator whose inner wrapper uses
functools.wrapsran the wrapped function's body instead of its own, so itstry/exceptnever ran:Why
User decorators were passed the raw
EvalFunc.functools.update_wrapperdoeswrapper.__dict__.update(wrapped.__dict__), which copied the wrappedEvalFunc's fields (func_def,name, ...) over the wrapper's, so the wrapper became a copy of the wrapped function.What I changed
EvalFuncVar, like a Python function object. Its own fields are__slots__, sowraps()only updates its user-visible__dict__.EvalFuncVarClassInstgets__slots__too.EvalFuncVargets__name__and__doc__properties (with setters) backed by theEvalFunc, sowraps()copies them like Python does.EvalFuncVara decorator returns is kept, not unwrapped and re-created. So a decorator that stores the function (for example in a registry) holds the same working object, and attributes it sets, like__wrapped__, survive.remove_func()had no other callers and is removed.Testing
tests/test_unit_eval.py, each compared with CPython:catch_alldecorator;wraps,wrapsonwraps);__name__,__doc__and__wrapped__after decoration;wrapsdecorator on a class method.wrapsdecorators combined with@service,@state_triggerand@event_triggerin either order.