More docs for the is_asyncio_reactor_installed() behavior change. (#6866)

This commit is contained in:
Andrey Rakhmatullin 2025-06-06 15:59:49 +05:00 committed by GitHub
parent d99234a33f
commit 405d9bc8a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 1 deletions

View File

@ -126,6 +126,15 @@ Backward-incompatible changes
also enforced for start requests.
(:issue:`6777`)
- Calling :func:`scrapy.utils.reactor.is_asyncio_reactor_installed` without
an installed reactor now raises an exception instead of installing a
reactor. This shouldn't affect normal Scrapy use cases, but it may affect
3rd-party test suites that use Scrapy internals such as
:class:`~scrapy.crawler.Crawler` and don't install a reactor explicitly. If
you are affected by this change, you most likely need to install the
reactor before running Scrapy code that expects it to be installed.
(:issue:`6732`, :issue:`6735`)
- The ``from_settings()`` method of
:class:`~scrapy.spidermiddlewares.urllength.UrlLengthMiddleware`,
deprecated in Scrapy 2.12.0, is removed earlier than the usual deprecation

View File

@ -202,6 +202,11 @@ def is_asyncio_reactor_installed() -> bool:
"""Check whether the installed reactor is :class:`~twisted.internet.asyncioreactor.AsyncioSelectorReactor`.
Raise a :exc:`RuntimeError` if no reactor is installed.
.. versionchanged:: 2.13
In earlier Scrapy versions this function silently installed the default
reactor if there was no reactor installed. Now it raises an exception to
prevent silent problems in this case.
"""
if not is_reactor_installed():
raise RuntimeError(

View File

@ -18,7 +18,7 @@ from twisted.trial.unittest import SkipTest
from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.utils.boto import is_botocore_available
from scrapy.utils.deprecate import create_deprecated_class
from scrapy.utils.reactor import is_asyncio_reactor_installed
from scrapy.utils.reactor import is_asyncio_reactor_installed, is_reactor_installed
from scrapy.utils.spider import DefaultSpider
if TYPE_CHECKING:
@ -117,6 +117,11 @@ def get_reactor_settings() -> dict[str, Any]:
settings, so tests that run the crawler in the current process may need to
pass a correct ``"TWISTED_REACTOR"`` setting value when creating it.
"""
if not is_reactor_installed():
raise RuntimeError(
"get_reactor_settings() called without an installed reactor,"
" you may need to install a reactor explicitly when running your tests."
)
settings: dict[str, Any] = {}
if not is_asyncio_reactor_installed():
settings["TWISTED_REACTOR"] = None