Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Deprecate `scrapy.twisted_version` (#6512)

Laerte Pereirad2bdbad8c8cc
diff --git a/scrapy/__init__.py b/scrapy/__init__.py
index 1c1a5c2cc..921296502 100644
--- a/scrapy/__init__.py
+++ b/scrapy/__init__.py
@@ -6,8 +6,6 @@ import pkgutil
import sys
import warnings
-from twisted import version as _txv
-
# Declare top-level shortcuts
from scrapy.http import FormRequest, Request
from scrapy.item import Field, Item
@@ -17,7 +15,6 @@ from scrapy.spiders import Spider
__all__ = [
"__version__",
"version_info",
- "twisted_version",
"Spider",
"Request",
"FormRequest",
@@ -30,7 +27,23 @@ __all__ = [
# Scrapy and Twisted versions
__version__ = (pkgutil.get_data(__package__, "VERSION") or b"").decode("ascii").strip()
version_info = tuple(int(v) if v.isdigit() else v for v in __version__.split("."))
-twisted_version = (_txv.major, _txv.minor, _txv.micro)
+
+
+def __getattr__(name: str):
+ if name == "twisted_version":
+ import warnings
+
+ from twisted import version as _txv
+
+ from scrapy.exceptions import ScrapyDeprecationWarning
+
+ warnings.warn(
+ "The scrapy.twisted_version attribute is deprecated, use twisted.version instead",
+ ScrapyDeprecationWarning,
+ )
+ return _txv.major, _txv.minor, _txv.micro
+
+ raise AttributeError
# Ignore noisy twisted deprecation warnings
diff --git a/tests/test_scrapy__getattr__.py b/tests/test_scrapy__getattr__.py
new file mode 100644
index 000000000..979c42267
--- /dev/null
+++ b/tests/test_scrapy__getattr__.py
@@ -0,0 +1,13 @@
+import warnings
+
+
+def test_deprecated_twisted_version():
+ with warnings.catch_warnings(record=True) as warns:
+ from scrapy import twisted_version
+
+ assert twisted_version is not None
+ assert isinstance(twisted_version, tuple)
+ assert (
+ "The scrapy.twisted_version attribute is deprecated, use twisted.version instead"
+ in warns[0].message.args
+ )