Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type of SSLContext for some static type checkers (#10099) #10145

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading