Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1164) Add types to cache.py #1199

Merged
merged 1 commit into from
Dec 29, 2019
Merged
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
9 changes: 6 additions & 3 deletions praw/util/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Caching utilities."""
from typing import Any, Callable, Optional


class cachedproperty:
Expand All @@ -18,15 +19,17 @@ class cachedproperty:
.. versionadded:: 6.3.0
"""

def __init__(self, func, doc=None):
def __init__(self, func: Callable[[Any], Any], doc: Optional[str] = None):
"""Initialize the descriptor."""
self.func = self.__wrapped__ = func

if doc is None:
doc = func.__doc__
self.__doc__ = doc

def __get__(self, obj, objtype=None):
def __get__(
self, obj: Optional[Any], objtype: Optional[Any] = None
) -> Any:
"""Implement descriptor getter.

Calculate the property's value and then store it in the
Expand All @@ -38,6 +41,6 @@ def __get__(self, obj, objtype=None):
value = obj.__dict__[self.func.__name__] = self.func(obj)
return value

def __repr__(self):
def __repr__(self) -> str:
"""Return repr(self)."""
return "<%s %s>" % (self.__class__.__name__, self.func)