Silence the CertificateOptions method warning. (#7410)

* Silence the CertificateOptions method warning.

* Fix typing.
This commit is contained in:
Andrey Rakhmatullin 2026-04-08 18:54:13 +05:00 committed by GitHub
parent b9be5ce053
commit 2b174e348d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 16 deletions

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import warnings import warnings
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, cast from typing import TYPE_CHECKING, Any, cast
from OpenSSL import SSL from OpenSSL import SSL
@ -25,6 +26,8 @@ from scrapy.utils.deprecate import create_deprecated_class
from scrapy.utils.misc import build_from_crawler, load_object from scrapy.utils.misc import build_from_crawler, load_object
if TYPE_CHECKING: if TYPE_CHECKING:
from collections.abc import Generator
from twisted.internet._sslverify import ClientTLSOptions from twisted.internet._sslverify import ClientTLSOptions
# typing.Self requires Python 3.11 # typing.Self requires Python 3.11
@ -34,6 +37,18 @@ if TYPE_CHECKING:
from scrapy.settings import BaseSettings from scrapy.settings import BaseSettings
@contextmanager
def _filter_method_warning() -> Generator[None]:
with warnings.catch_warnings():
# Twisted deprecation, https://github.com/scrapy/scrapy/issues/3288
warnings.filterwarnings(
"ignore",
message=r"Passing method to twisted\.internet\.ssl\.CertificateOptions",
category=DeprecationWarning,
)
yield
@implementer(IPolicyForHTTPS) @implementer(IPolicyForHTTPS)
class _ScrapyClientContextFactory(BrowserLikePolicyForHTTPS): class _ScrapyClientContextFactory(BrowserLikePolicyForHTTPS):
"""Non-peer-certificate verifying HTTPS context factory. """Non-peer-certificate verifying HTTPS context factory.
@ -63,11 +78,12 @@ class _ScrapyClientContextFactory(BrowserLikePolicyForHTTPS):
self.tls_ciphers = AcceptableCiphers.fromOpenSSLCipherString(tls_ciphers) self.tls_ciphers = AcceptableCiphers.fromOpenSSLCipherString(tls_ciphers)
else: else:
self.tls_ciphers = DEFAULT_CIPHERS self.tls_ciphers = DEFAULT_CIPHERS
self._certificate_options = CertificateOptions( with _filter_method_warning():
method=self._ssl_method, self._certificate_options = CertificateOptions(
fixBrokenPeers=True, method=self._ssl_method,
acceptableCiphers=self.tls_ciphers, fixBrokenPeers=True,
) acceptableCiphers=self.tls_ciphers,
)
self._ctx = self._get_context() self._ctx = self._get_context()
self._verify_certificates = verify_certificates self._verify_certificates = verify_certificates
@ -110,13 +126,14 @@ class _ScrapyClientContextFactory(BrowserLikePolicyForHTTPS):
if not self._verify_certificates: if not self._verify_certificates:
return _ScrapyClientTLSOptions(hostname.decode("ascii"), self._ctx) # type: ignore[no-untyped-call] return _ScrapyClientTLSOptions(hostname.decode("ascii"), self._ctx) # type: ignore[no-untyped-call]
# Note that this doesn't use self._ctx # Note that this doesn't use self._ctx
return optionsForClientTLS( with _filter_method_warning():
hostname=hostname.decode("ascii"), return optionsForClientTLS(
extraCertificateOptions={ hostname=hostname.decode("ascii"),
"method": self._ssl_method, extraCertificateOptions={
"acceptableCiphers": self.tls_ciphers, "method": self._ssl_method,
}, "acceptableCiphers": self.tls_ciphers,
) },
)
ScrapyClientContextFactory = create_deprecated_class( ScrapyClientContextFactory = create_deprecated_class(
@ -160,10 +177,11 @@ class BrowserLikeContextFactory(_ScrapyClientContextFactory):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def creatorForNetloc(self, hostname: bytes, port: int) -> ClientTLSOptions: def creatorForNetloc(self, hostname: bytes, port: int) -> ClientTLSOptions:
return optionsForClientTLS( with _filter_method_warning():
hostname=hostname.decode("ascii"), return optionsForClientTLS(
extraCertificateOptions={"method": self._ssl_method}, hostname=hostname.decode("ascii"),
) extraCertificateOptions={"method": self._ssl_method},
)
@implementer(IPolicyForHTTPS) @implementer(IPolicyForHTTPS)