Skip to content

Commit

Permalink
Fix type of SSLContext for some static type checkers (#10099)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanBogarin authored Dec 8, 2024
1 parent dbd77ad commit 6200513
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGES/10099.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed type of ``SSLContext`` for some static type checkers (e.g. pyright).
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Adam Mills
Adrian Krupa
Adrián Chaves
Ahmed Tahri
Alan Bogarin
Alan Tse
Alec Hanefeld
Alejandro Gómez
Expand Down
10 changes: 7 additions & 3 deletions aiohttp/client_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

from .typedefs import StrOrURL

try:
if TYPE_CHECKING:
import ssl

SSLContext = ssl.SSLContext
except ImportError: # pragma: no cover
ssl = SSLContext = None # type: ignore[assignment]
else:
try:
import ssl

SSLContext = ssl.SSLContext
except ImportError: # pragma: no cover
ssl = SSLContext = None # type: ignore[assignment]

if TYPE_CHECKING:
from .client_reqrep import ClientResponse, ConnectionKey, Fingerprint, RequestInfo
Expand Down
12 changes: 8 additions & 4 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@
RawHeaders,
)

try:
if TYPE_CHECKING:
import ssl
from ssl import SSLContext
except ImportError: # pragma: no cover
ssl = None # type: ignore[assignment]
SSLContext = object # type: ignore[misc,assignment]
else:
try:
import ssl
from ssl import SSLContext
except ImportError: # pragma: no cover
ssl = None # type: ignore[assignment]
SSLContext = object # type: ignore[misc,assignment]


__all__ = ("ClientRequest", "ClientResponse", "RequestInfo", "Fingerprint")
Expand Down
14 changes: 9 additions & 5 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@
)
from .resolver import DefaultResolver

try:
if TYPE_CHECKING:
import ssl

SSLContext = ssl.SSLContext
except ImportError: # pragma: no cover
ssl = None # type: ignore[assignment]
SSLContext = object # type: ignore[misc,assignment]

else:
try:
import ssl

SSLContext = ssl.SSLContext
except ImportError: # pragma: no cover
ssl = None # type: ignore[assignment]
SSLContext = object # type: ignore[misc,assignment]

EMPTY_SCHEMA_SET = frozenset({""})
HTTP_SCHEMA_SET = frozenset({"http", "https"})
Expand Down
10 changes: 7 additions & 3 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from contextlib import suppress
from importlib import import_module
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Expand Down Expand Up @@ -267,10 +268,13 @@
)


try:
if TYPE_CHECKING:
from ssl import SSLContext
except ImportError: # pragma: no cover
SSLContext = Any # type: ignore[misc,assignment]
else:
try:
from ssl import SSLContext
except ImportError: # pragma: no cover
SSLContext = object # type: ignore[misc,assignment]

# Only display warning when using -Wdefault, -We, -X dev or similar.
warnings.filterwarnings("ignore", category=NotAppKeyWarning, append=True)
Expand Down
12 changes: 7 additions & 5 deletions aiohttp/web_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import signal
import socket
from abc import ABC, abstractmethod
from typing import Any, Generic, List, Optional, Set, Type, TypeVar
from typing import TYPE_CHECKING, Any, Generic, List, Optional, Set, Type, TypeVar

from yarl import URL

Expand All @@ -16,11 +16,13 @@
from .web_request import BaseRequest, Request
from .web_server import Server

try:
if TYPE_CHECKING:
from ssl import SSLContext
except ImportError:
SSLContext = object # type: ignore[misc,assignment]

else:
try:
from ssl import SSLContext
except ImportError: # pragma: no cover
SSLContext = object # type: ignore[misc,assignment]

__all__ = (
"BaseSite",
Expand Down
15 changes: 10 additions & 5 deletions aiohttp/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import signal
import sys
from types import FrameType
from typing import Any, Awaitable, Callable, Optional, Union # noqa
from typing import TYPE_CHECKING, Any, Optional

from gunicorn.config import AccessLogFormat as GunicornAccessLogFormat
from gunicorn.workers import base
Expand All @@ -17,13 +17,18 @@
from .web_app import Application
from .web_log import AccessLogger

try:
if TYPE_CHECKING:
import ssl

SSLContext = ssl.SSLContext
except ImportError: # pragma: no cover
ssl = None # type: ignore[assignment]
SSLContext = object # type: ignore[misc,assignment]
else:
try:
import ssl

SSLContext = ssl.SSLContext
except ImportError: # pragma: no cover
ssl = None # type: ignore[assignment]
SSLContext = object # type: ignore[misc,assignment]


__all__ = ("GunicornWebWorker", "GunicornUVLoopWebWorker")
Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ py
pydantic
pyenv
pyflakes
pyright
pytest
Pytest
Quickstart
Expand Down

0 comments on commit 6200513

Please sign in to comment.