diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e4748e2..71f5745f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - #922 Classify autoimport project sources by path containment (@marlon-costa-dc) - #926, #929 Raise RefactoringError instead of IndexError/ModuleSyntaxError when extracting an empty or whitespace-only region (@aliassar) - #933 Add type annotations to project.py (@lieryan) +- #919 Type `saveit` so decorated members keep their return type (@marlon-costa-dc) - #119, #928 Fix IndexError in rename and find occurrences when a `from` import is the last thing in a module without a trailing newline (@mcepl) - #921 Patch the else suite of try statements that also have a finally (@marlon-costa-dc) - #937 Preserve non-ASCII argument expressions and default values when changing function signatures (@Jinyong-Li) diff --git a/rope/base/utils/__init__.py b/rope/base/utils/__init__.py index c96c96428..bdd5da0a7 100644 --- a/rope/base/utils/__init__.py +++ b/rope/base/utils/__init__.py @@ -1,13 +1,20 @@ import sys import warnings +from typing import Callable, Concatenate, ParamSpec, TypeVar +_Self = TypeVar("_Self") +_Params = ParamSpec("_Params") +_Result = TypeVar("_Result") -def saveit(func): + +def saveit( + func: Callable[Concatenate[_Self, _Params], _Result], +) -> Callable[Concatenate[_Self, _Params], _Result]: """A decorator that caches the return value of a function""" name = "_" + func.__name__ - def _wrapper(self, *args, **kwds): + def _wrapper(self: _Self, *args: _Params.args, **kwds: _Params.kwargs) -> _Result: if not hasattr(self, name): setattr(self, name, func(self, *args, **kwds)) return getattr(self, name)