Skip to content

Commit 51698fb

Browse files
authored
Replace expensive isinstance checks with faster alternatives (#11085)
1 parent 5da0231 commit 51698fb

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,
@@ -1006,7 +1006,7 @@ def update_cookies(self, cookies: Optional[LooseCookies]) -> None:
10061006
c.load(self.headers.get(hdrs.COOKIE, ""))
10071007
del self.headers[hdrs.COOKIE]
10081008

1009-
if isinstance(cookies, Mapping):
1009+
if isinstance(cookies, ABCMapping):
10101010
iter_cookies = cookies.items()
10111011
else:
10121012
iter_cookies = cookies # type: ignore[assignment]

aiohttp/cookiejar.py

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

240-
if isinstance(cookies, Mapping):
240+
if isinstance(cookies, ABCMapping):
241241
cookies = cookies.items()
242242

243243
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,
@@ -957,12 +958,12 @@ def append_form(
957958
headers: Optional[Mapping[str, str]] = None,
958959
) -> Payload:
959960
"""Helper to append form urlencoded part."""
960-
assert isinstance(obj, (Sequence, Mapping))
961+
assert isinstance(obj, (ABCSequence, ABCMapping))
961962

962963
if headers is None:
963964
headers = CIMultiDict()
964965

965-
if isinstance(obj, Mapping):
966+
if isinstance(obj, ABCMapping):
966967
obj = list(obj.items())
967968
data = urlencode(obj, doseq=True)
968969

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)