Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions pytools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,17 @@
pass


class _MemoizedFunctionWithClear(Generic[T_contra, P, R], Protocol):

Check failure on line 754 in pytools/__init__.py

View workflow job for this annotation

GitHub Actions / basedpyright

Type argument for "Generic" must be a type variable (reportInvalidTypeForm)

Check failure on line 754 in pytools/__init__.py

View workflow job for this annotation

GitHub Actions / basedpyright

"T_contra" is not defined (reportUndefinedVariable)

Check warning on line 754 in pytools/__init__.py

View workflow job for this annotation

GitHub Actions / basedpyright

Type variable "R" used in generic Protocol "_MemoizedFunctionWithClear" should be covariant (reportInvalidTypeVarUse)
def __call__(self, obj: T_contra, *args: P.args, **kwargs: P.kwargs) -> R:

Check failure on line 755 in pytools/__init__.py

View workflow job for this annotation

GitHub Actions / basedpyright

"T_contra" is not defined (reportUndefinedVariable)

Check warning on line 755 in pytools/__init__.py

View workflow job for this annotation

GitHub Actions / basedpyright

Type of parameter "obj" is unknown (reportUnknownParameterType)
...

def clear_cache(self) -> None:
pass


def memoize_on_first_arg(
function: Callable[Concatenate[T, P], R], *,
cache_dict_name: str | None = None) -> Callable[Concatenate[T, P], R]:
cache_dict_name: str | None = None) -> _MemoizedFunctionWithClear[T, P, R]:
"""Like :func:`memoize_method`, but for functions that take the object
in which do memoization information is stored as first argument.

Expand Down Expand Up @@ -783,17 +791,17 @@
getattr(obj, cache_dict_name)[key] = result
return result

def clear_cache(obj):
def clear_cache(obj: _MemoizedFunctionWithClear[T, P, R]):
object.__delattr__(obj, cache_dict_name)

from functools import update_wrapper
new_wrapper = update_wrapper(wrapper, function)
new_wrapper = cast(
"type[_MemoizedFunctionWithClear[T, P, R]]", cast("object",
update_wrapper(wrapper, function)))

# type-ignore because mypy has a point here, stuffing random attributes
# into the function's dict is moderately sketchy.
new_wrapper.clear_cache = clear_cache # type: ignore[attr-defined]
new_wrapper.clear_cache = clear_cache

Check failure on line 802 in pytools/__init__.py

View workflow job for this annotation

GitHub Actions / basedpyright

Cannot assign to attribute "clear_cache" for class "type[_MemoizedFunctionWithClear[T@memoize_on_first_arg, P@memoize_on_first_arg, R@memoize_on_first_arg]]"   Type "(obj: _MemoizedFunctionWithClear[T@memoize_on_first_arg, P@memoize_on_first_arg, R@memoize_on_first_arg]) -> None" is not assignable to type "(self: _MemoizedFunctionWithClear[T@memoize_on_first_arg, P@memoize_on_first_arg, R@memoize_on_first_arg]) -> None"     Parameter name mismatch: "self" versus "obj" (reportAttributeAccessIssue)

return new_wrapper

Check failure on line 804 in pytools/__init__.py

View workflow job for this annotation

GitHub Actions / basedpyright

Type "type[_MemoizedFunctionWithClear[T@memoize_on_first_arg, P@memoize_on_first_arg, R@memoize_on_first_arg]]" is not assignable to return type "_MemoizedFunctionWithClear[T@memoize_on_first_arg, P@memoize_on_first_arg, R@memoize_on_first_arg]"   "clear_cache" is an incompatible type     Type "(self: _MemoizedFunctionWithClear[T@memoize_on_first_arg, P@memoize_on_first_arg, R@memoize_on_first_arg]) -> None" is not assignable to type "() -> None"       Extra parameter "self" (reportReturnType)


def memoize_method(
Expand Down
Loading