Skip to content

Keep a decorator's own body when it uses functools.wraps (#808) - #910

Merged
craigbarratt merged 2 commits into
custom-components:masterfrom
HoneyBearTech:fix-wraps-decorators-808
Nov 21, 2026
Merged

craigbarratt merged 2 commits into
custom-components:masterfrom
HoneyBearTech:fix-wraps-decorators-808

Conversation

@HoneyBearTech

Copy link
Copy Markdown
Contributor

Closes #808

What was broken
A decorator whose inner wrapper uses functools.wraps ran the wrapped function's body instead of its own, so its try/except never ran:

from functools import wraps

def catch_all(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        try:
            return fn(*args, **kwargs)
        except Exception:
            log.error("gotcha")
    return wrapper

@service
@catch_all
def test2(**kwargs):
    raise Exception("will NOT be caught")   # escapes; "gotcha" is never logged

Why
User decorators were passed the raw EvalFunc. functools.update_wrapper does wrapper.__dict__.update(wrapped.__dict__), which copied the wrapped EvalFunc's fields (func_def, name, ...) over the wrapper's, so the wrapper became a copy of the wrapped function.

What I changed

  • User decorators now get an EvalFuncVar, like a Python function object. Its own fields are __slots__, so wraps() only updates its user-visible __dict__. EvalFuncVarClassInst gets __slots__ too.
  • EvalFuncVar gets __name__ and __doc__ properties (with setters) backed by the EvalFunc, so wraps() copies them like Python does.
  • The EvalFuncVar a 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

  • New cases in tests/test_unit_eval.py, each compared with CPython:
    • the issue's catch_all decorator;
    • arguments passed through and the result returned;
    • stacked decorators (plain on wraps, wraps on wraps);
    • a decorator returning the function unchanged (identity kept);
    • __name__, __doc__ and __wrapped__ after decoration;
    • a wraps decorator on a class method.
  • Also checked by hand: wraps decorators combined with @service, @state_trigger and @event_trigger in either order.
  • The full test suite, ruff, pylint, mypy and codespell pass on Python 3.14.

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 craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@HoneyBearTech

Copy link
Copy Markdown
Contributor Author

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.

@craigbarratt

Copy link
Copy Markdown
Member

Thanks @HoneyBearTech, looks great. Merging.

@craigbarratt
craigbarratt merged commit 06f8eba into custom-components:master Nov 21, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decorators won't catch exceptions

2 participants