Skip to content

Commit 6200513

Browse files
authored
Fix type of SSLContext for some static type checkers (#10099)
1 parent dbd77ad commit 6200513

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
@@ -7,13 +7,17 @@
77

88
from .typedefs import StrOrURL
99

10-
try:
10+
if TYPE_CHECKING:
1111
import ssl
1212

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

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

1822
if TYPE_CHECKING:
1923
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
@@ -74,12 +74,16 @@
7474
RawHeaders,
7575
)
7676

77-
try:
77+
if TYPE_CHECKING:
7878
import ssl
7979
from ssl import SSLContext
80-
except ImportError: # pragma: no cover
81-
ssl = None # type: ignore[assignment]
82-
SSLContext = object # type: ignore[misc,assignment]
80+
else:
81+
try:
82+
import ssl
83+
from ssl import SSLContext
84+
except ImportError: # pragma: no cover
85+
ssl = None # type: ignore[assignment]
86+
SSLContext = object # type: ignore[misc,assignment]
8387

8488

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

aiohttp/connector.py

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

64-
try:
64+
if TYPE_CHECKING:
6565
import ssl
6666

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

7377
EMPTY_SCHEMA_SET = frozenset({""})
7478
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,
@@ -267,10 +268,13 @@
267268
)
268269

269270

270-
try:
271+
if TYPE_CHECKING:
271272
from ssl import SSLContext
272-
except ImportError: # pragma: no cover
273-
SSLContext = Any # type: ignore[misc,assignment]
273+
else:
274+
try:
275+
from ssl import SSLContext
276+
except ImportError: # pragma: no cover
277+
SSLContext = object # type: ignore[misc,assignment]
274278

275279
# Only display warning when using -Wdefault, -We, -X dev or similar.
276280
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
@@ -2,7 +2,7 @@
22
import signal
33
import socket
44
from abc import ABC, abstractmethod
5-
from typing import Any, Generic, List, Optional, Set, Type, TypeVar
5+
from typing import TYPE_CHECKING, Any, Generic, List, Optional, Set, Type, TypeVar
66

77
from yarl import URL
88

@@ -16,11 +16,13 @@
1616
from .web_request import BaseRequest, Request
1717
from .web_server import Server
1818

19-
try:
19+
if TYPE_CHECKING:
2020
from ssl import SSLContext
21-
except ImportError:
22-
SSLContext = object # type: ignore[misc,assignment]
23-
21+
else:
22+
try:
23+
from ssl import SSLContext
24+
except ImportError: # pragma: no cover
25+
SSLContext = object # type: ignore[misc,assignment]
2426

2527
__all__ = (
2628
"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)