diff --git a/CHANGES/10099.bugfix.rst b/CHANGES/10099.bugfix.rst new file mode 100644 index 00000000000..718420a6ad5 --- /dev/null +++ b/CHANGES/10099.bugfix.rst @@ -0,0 +1 @@ +Fixed type of ``SSLContext`` for some static type checkers (e.g. pyright). diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 6adb3b97fb1..ded6c463e40 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -9,6 +9,7 @@ Adam Mills Adrian Krupa Adrián Chaves Ahmed Tahri +Alan Bogarin Alan Tse Alec Hanefeld Alejandro Gómez diff --git a/aiohttp/client_exceptions.py b/aiohttp/client_exceptions.py index 667da8d5084..1d298e9a8cf 100644 --- a/aiohttp/client_exceptions.py +++ b/aiohttp/client_exceptions.py @@ -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 diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index e97c40ce0e5..43b48063c6e 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -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") diff --git a/aiohttp/connector.py b/aiohttp/connector.py index 93bc2513b20..a9123f82bc0 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -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"}) diff --git a/aiohttp/web.py b/aiohttp/web.py index f975b665331..d6ab6f6fad4 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -9,6 +9,7 @@ from contextlib import suppress from importlib import import_module from typing import ( + TYPE_CHECKING, Any, Awaitable, Callable, @@ -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) diff --git a/aiohttp/web_runner.py b/aiohttp/web_runner.py index f8933383435..bcfec727c84 100644 --- a/aiohttp/web_runner.py +++ b/aiohttp/web_runner.py @@ -3,7 +3,7 @@ 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 @@ -11,11 +11,13 @@ 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", diff --git a/aiohttp/worker.py b/aiohttp/worker.py index 9b307697336..8ed121ac955 100644 --- a/aiohttp/worker.py +++ b/aiohttp/worker.py @@ -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 @@ -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") diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt index a1f3d944584..c4e10b44987 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -245,6 +245,7 @@ py pydantic pyenv pyflakes +pyright pytest Pytest Quickstart