Skip to content

Commit 4ecdde2

Browse files
authored
Type hinting __aenter__ with Self (#367)
* Added typing-extensions to dependencies for Python<=3.10 * Moved to using Self type for CachedSession.__aenter__ * Moved to using Self type for CachedResponse.__aenter__
1 parent 33b5421 commit 4ecdde2

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

aiohttp_client_cache/response.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import asyncio
66
import json
7+
import sys
78
from collections.abc import Mapping
89
from datetime import datetime
910
from functools import singledispatch
@@ -23,6 +24,11 @@
2324

2425
from aiohttp_client_cache.cache_control import utcnow
2526

27+
if sys.version_info >= (3, 11):
28+
from typing import Self
29+
else:
30+
from typing_extensions import Self
31+
2632
# CachedResponse attributes to not copy directly from ClientResponse
2733
EXCLUDE_ATTRS = {
2834
'_body',
@@ -259,7 +265,7 @@ def _released(self):
259265
def connection(self):
260266
return None
261267

262-
async def __aenter__(self) -> CachedResponse:
268+
async def __aenter__(self) -> Self:
263269
return self
264270

265271
async def __aexit__(self, *exc: Any) -> None:

aiohttp_client_cache/session.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ async def __aexit__(self, *excinfo):
4242
pass
4343

4444

45+
if sys.version_info >= (3, 11):
46+
from typing import Self
47+
else:
48+
from typing_extensions import Self
49+
50+
4551
@lru_cache(maxsize=16384)
4652
def _get_lock(_: int, __: str) -> Lock:
4753
return Lock()
@@ -245,5 +251,5 @@ class CachedSession(CacheMixin, ClientSession):
245251
options. If not provided, an in-memory cache will be used.
246252
"""
247253

248-
async def __aenter__(self) -> CachedSession:
254+
async def __aenter__(self) -> Self:
249255
return self

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ python = ">=3.9"
3434
aiohttp = "^3.8"
3535
attrs = ">=21.2"
3636
itsdangerous = ">=2.0"
37+
typing-extensions = {python="<=3.10", version=">=4"} # Pin v4 for Self addition
3738
url-normalize = "^2.2"
3839

3940
# Optional backend dependencies

0 commit comments

Comments
 (0)