Skip to content

Commit

Permalink
Fix type of SSLContext for some static type checkers (#10099) (#10144)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6200513)

Co-authored-by: AlanBogarin <[email protected]>
  • Loading branch information
Dreamsorcerer and AlanBogarin authored Dec 8, 2024
1 parent f52f60a commit eb42db8
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 @@ -8,13 +8,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 @@ -72,12 +72,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 @@ -60,14 +60,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 @@ -287,10 +288,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 @@ -3,19 +3,21 @@
import socket
import warnings
from abc import ABC, abstractmethod
from typing import Any, List, Optional, Set
from typing import TYPE_CHECKING, Any, List, Optional, Set

from yarl import URL

from .typedefs import PathLike
from .web_app import Application
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 eb42db8

Please sign in to comment.