From 6200513d8fd34a820a4d10d238ca92d9f73ce7ee Mon Sep 17 00:00:00 2001 From: AlanBogarin Date: Sun, 8 Dec 2024 12:09:00 -0300 Subject: [PATCH] Fix type of SSLContext for some static type checkers (#10099) --- CHANGES/10099.bugfix.rst | 1 + CONTRIBUTORS.txt | 1 + aiohttp/client_exceptions.py | 10 +++++++--- aiohttp/client_reqrep.py | 12 ++++++++---- aiohttp/connector.py | 14 +++++++++----- aiohttp/web.py | 10 +++++++--- aiohttp/web_runner.py | 12 +++++++----- aiohttp/worker.py | 15 ++++++++++----- docs/spelling_wordlist.txt | 1 + 9 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 CHANGES/10099.bugfix.rst 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 66c706cde4f..40fa2cb2b1a 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 9f1c7751da7..da159d0ae7d 100644 --- a/aiohttp/client_exceptions.py +++ b/aiohttp/client_exceptions.py @@ -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 diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index f2db7e85e50..8c927a49bf0 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -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") diff --git a/aiohttp/connector.py b/aiohttp/connector.py index a23c25a5b32..1b7610c2831 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -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"}) diff --git a/aiohttp/web.py b/aiohttp/web.py index 50df1d10db2..3de39fc6e9d 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, @@ -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) diff --git a/aiohttp/web_runner.py b/aiohttp/web_runner.py index f36fcb1ea56..55dbdcc61c3 100644 --- a/aiohttp/web_runner.py +++ b/aiohttp/web_runner.py @@ -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 @@ -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", diff --git a/aiohttp/worker.py b/aiohttp/worker.py index 1c734ea56c4..4674388e8ba 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 556646ba8db..33d6c7ecf2c 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -245,6 +245,7 @@ py pydantic pyenv pyflakes +pyright pytest Pytest Quickstart