Skip to content

Commit 73cca7c

Browse files
[PR #11085/51698fb1 backport][3.12] Replace expensive isinstance checks with faster alternatives (#11086)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 0cbdc67 commit 73cca7c

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

CHANGES/11085.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved performance of isinstance checks by using collections.abc types instead of typing module equivalents -- by :user:`bdraco`.

aiohttp/client_reqrep.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import traceback
99
import warnings
10+
from collections.abc import Mapping as ABCMapping
1011
from hashlib import md5, sha1, sha256
1112
from http.cookies import CookieError, Morsel, SimpleCookie
1213
from types import MappingProxyType, TracebackType
@@ -18,7 +19,6 @@
1819
Iterable,
1920
List,
2021
Literal,
21-
Mapping,
2222
NamedTuple,
2323
Optional,
2424
Tuple,
@@ -1085,7 +1085,7 @@ def update_cookies(self, cookies: Optional[LooseCookies]) -> None:
10851085
c.load(self.headers.get(hdrs.COOKIE, ""))
10861086
del self.headers[hdrs.COOKIE]
10871087

1088-
if isinstance(cookies, Mapping):
1088+
if isinstance(cookies, ABCMapping):
10891089
iter_cookies = cookies.items()
10901090
else:
10911091
iter_cookies = cookies # type: ignore[assignment]

aiohttp/cookiejar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import time
1212
import warnings
1313
from collections import defaultdict
14+
from collections.abc import Mapping as ABCMapping
1415
from http.cookies import BaseCookie, Morsel, SimpleCookie
1516
from typing import (
1617
DefaultDict,
1718
Dict,
1819
Iterable,
1920
Iterator,
2021
List,
21-
Mapping,
2222
Optional,
2323
Set,
2424
Tuple,
@@ -236,7 +236,7 @@ def update_cookies(self, cookies: LooseCookies, response_url: URL = URL()) -> No
236236
# Don't accept cookies from IPs
237237
return
238238

239-
if isinstance(cookies, Mapping):
239+
if isinstance(cookies, ABCMapping):
240240
cookies = cookies.items()
241241

242242
for name, cookie in cookies:

aiohttp/multipart.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import uuid
77
import warnings
88
from collections import deque
9+
from collections.abc import Mapping as ABCMapping, Sequence as ABCSequence
910
from types import TracebackType
1011
from typing import (
1112
TYPE_CHECKING,
@@ -953,12 +954,12 @@ def append_form(
953954
headers: Optional[Mapping[str, str]] = None,
954955
) -> Payload:
955956
"""Helper to append form urlencoded part."""
956-
assert isinstance(obj, (Sequence, Mapping))
957+
assert isinstance(obj, (ABCSequence, ABCMapping))
957958

958959
if headers is None:
959960
headers = CIMultiDict()
960961

961-
if isinstance(obj, Mapping):
962+
if isinstance(obj, ABCMapping):
962963
obj = list(obj.items())
963964
data = urlencode(obj, doseq=True)
964965

aiohttp/payload.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import warnings
99
from abc import ABC, abstractmethod
10+
from collections.abc import Iterable as ABCIterable
1011
from itertools import chain
1112
from typing import (
1213
IO,
@@ -137,7 +138,7 @@ def register(
137138
self._first.append((factory, type))
138139
elif order is Order.normal:
139140
self._normal.append((factory, type))
140-
if isinstance(type, Iterable):
141+
if isinstance(type, ABCIterable):
141142
for t in type:
142143
self._normal_lookup[t] = factory
143144
else:

0 commit comments

Comments
 (0)