From 6e4335fc1db32a801df20268ae1c944a3e6009cd Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Mon, 11 May 2020 21:59:22 +0700 Subject: [PATCH] Fix @reify type hints --- aiohttp/helpers.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 77ecfe2df2d..4ae46f4b4af 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -25,6 +25,7 @@ Callable, Dict, Generator, + Generic, Iterable, Iterator, List, @@ -79,6 +80,7 @@ def all_tasks( _T = TypeVar('_T') +_S = TypeVar('_S') sentinel = object() # type: Any @@ -382,7 +384,7 @@ def is_expected_content_type(response_content_type: str, return expected_content_type in response_content_type -class reify: +class reify(Generic[_T): """Use as a class method decorator. It operates almost exactly like the Python `@property` decorator, but it puts the result of the method it decorates into the instance dict after the first call, @@ -391,12 +393,12 @@ class reify: """ - def __init__(self, wrapped: Callable[..., Any]) -> None: + def __init__(self, wrapped: Callable[..., _T]) -> None: self.wrapped = wrapped self.__doc__ = wrapped.__doc__ self.name = wrapped.__name__ - def __get__(self, inst: Any, owner: Any) -> Any: + def __get__(self, inst: _S, owner: Optional[Type[Any]] = None) -> _T: try: try: return inst._cache[self.name] @@ -409,7 +411,7 @@ def __get__(self, inst: Any, owner: Any) -> Any: return self raise - def __set__(self, inst: Any, value: Any) -> None: + def __set__(self, inst: _S, value: _T) -> None: raise AttributeError("reified property is read-only")