Skip to content

Commit b770b1a

Browse files
Fix type of SSLContext for some static type checkers (#10099) (#10145)
(cherry picked from commit 6200513) Co-authored-by: AlanBogarin <bogarin01alan@gmail.com>
1 parent 489b664 commit b770b1a

File tree

9 files changed

+51
-25
lines changed

9 files changed

+51
-25
lines changed

CHANGES/10099.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed type of ``SSLContext`` for some static type checkers (e.g. pyright).

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Adam Mills
99
Adrian Krupa
1010
Adrián Chaves
1111
Ahmed Tahri
12+
Alan Bogarin
1213
Alan Tse
1314
Alec Hanefeld
1415
Alejandro Gómez

aiohttp/client_exceptions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
from .typedefs import StrOrURL
1010

11-
try:
11+
if TYPE_CHECKING:
1212
import ssl
1313

1414
SSLContext = ssl.SSLContext
15-
except ImportError: # pragma: no cover
16-
ssl = SSLContext = None # type: ignore[assignment]
15+
else:
16+
try:
17+
import ssl
1718

19+
SSLContext = ssl.SSLContext
20+
except ImportError: # pragma: no cover
21+
ssl = SSLContext = None # type: ignore[assignment]
1822

1923
if TYPE_CHECKING:
2024
from .client_reqrep import ClientResponse, ConnectionKey, Fingerprint, RequestInfo

aiohttp/client_reqrep.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@
7272
RawHeaders,
7373
)
7474

75-
try:
75+
if TYPE_CHECKING:
7676
import ssl
7777
from ssl import SSLContext
78-
except ImportError: # pragma: no cover
79-
ssl = None # type: ignore[assignment]
80-
SSLContext = object # type: ignore[misc,assignment]
78+
else:
79+
try:
80+
import ssl
81+
from ssl import SSLContext
82+
except ImportError: # pragma: no cover
83+
ssl = None # type: ignore[assignment]
84+
SSLContext = object # type: ignore[misc,assignment]
8185

8286

8387
__all__ = ("ClientRequest", "ClientResponse", "RequestInfo", "Fingerprint")

aiohttp/connector.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@
6060
)
6161
from .resolver import DefaultResolver
6262

63-
try:
63+
if TYPE_CHECKING:
6464
import ssl
6565

6666
SSLContext = ssl.SSLContext
67-
except ImportError: # pragma: no cover
68-
ssl = None # type: ignore[assignment]
69-
SSLContext = object # type: ignore[misc,assignment]
70-
67+
else:
68+
try:
69+
import ssl
70+
71+
SSLContext = ssl.SSLContext
72+
except ImportError: # pragma: no cover
73+
ssl = None # type: ignore[assignment]
74+
SSLContext = object # type: ignore[misc,assignment]
7175

7276
EMPTY_SCHEMA_SET = frozenset({""})
7377
HTTP_SCHEMA_SET = frozenset({"http", "https"})

aiohttp/web.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from contextlib import suppress
1010
from importlib import import_module
1111
from typing import (
12+
TYPE_CHECKING,
1213
Any,
1314
Awaitable,
1415
Callable,
@@ -287,10 +288,13 @@
287288
)
288289

289290

290-
try:
291+
if TYPE_CHECKING:
291292
from ssl import SSLContext
292-
except ImportError: # pragma: no cover
293-
SSLContext = Any # type: ignore[misc,assignment]
293+
else:
294+
try:
295+
from ssl import SSLContext
296+
except ImportError: # pragma: no cover
297+
SSLContext = object # type: ignore[misc,assignment]
294298

295299
# Only display warning when using -Wdefault, -We, -X dev or similar.
296300
warnings.filterwarnings("ignore", category=NotAppKeyWarning, append=True)

aiohttp/web_runner.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
import socket
44
import warnings
55
from abc import ABC, abstractmethod
6-
from typing import Any, List, Optional, Set
6+
from typing import TYPE_CHECKING, Any, List, Optional, Set
77

88
from yarl import URL
99

1010
from .typedefs import PathLike
1111
from .web_app import Application
1212
from .web_server import Server
1313

14-
try:
14+
if TYPE_CHECKING:
1515
from ssl import SSLContext
16-
except ImportError:
17-
SSLContext = object # type: ignore[misc,assignment]
18-
16+
else:
17+
try:
18+
from ssl import SSLContext
19+
except ImportError: # pragma: no cover
20+
SSLContext = object # type: ignore[misc,assignment]
1921

2022
__all__ = (
2123
"BaseSite",

aiohttp/worker.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import signal
77
import sys
88
from types import FrameType
9-
from typing import Any, Awaitable, Callable, Optional, Union # noqa
9+
from typing import TYPE_CHECKING, Any, Optional
1010

1111
from gunicorn.config import AccessLogFormat as GunicornAccessLogFormat
1212
from gunicorn.workers import base
@@ -17,13 +17,18 @@
1717
from .web_app import Application
1818
from .web_log import AccessLogger
1919

20-
try:
20+
if TYPE_CHECKING:
2121
import ssl
2222

2323
SSLContext = ssl.SSLContext
24-
except ImportError: # pragma: no cover
25-
ssl = None # type: ignore[assignment]
26-
SSLContext = object # type: ignore[misc,assignment]
24+
else:
25+
try:
26+
import ssl
27+
28+
SSLContext = ssl.SSLContext
29+
except ImportError: # pragma: no cover
30+
ssl = None # type: ignore[assignment]
31+
SSLContext = object # type: ignore[misc,assignment]
2732

2833

2934
__all__ = ("GunicornWebWorker", "GunicornUVLoopWebWorker")

docs/spelling_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ py
245245
pydantic
246246
pyenv
247247
pyflakes
248+
pyright
248249
pytest
249250
Pytest
250251
Quickstart

0 commit comments

Comments
 (0)