Reducing amount of warnings during test run (#5162)

* put flake8 options into separate file to remove pytest warnings

* remove ResourceLeaked warning in pypy

* suppress warnings from twisted

* ignore deprecation warnings here

* ignore deprecation warning in tests of deprecated methods

* ignore deprecation warnings here

* update test classes

* don`t use deprecated method call

* ignore deprecation warnings here

* proper warning class

* more selective ignoring

* Revert "don`t use deprecated method call"

This reverts commit 59216ab560.
This commit is contained in:
Vostretsov Nikita 2021-05-28 09:45:06 +00:00 committed by GitHub
parent ee682af3b0
commit 23cfdb058e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 66 additions and 44 deletions

19
.flake8 Normal file
View File

@ -0,0 +1,19 @@
[flake8]
max-line-length = 119
ignore = W503
exclude =
# Exclude files that are meant to provide top-level imports
# E402: Module level import not at top of file
# F401: Module imported but unused
scrapy/__init__.py E402
scrapy/core/downloader/handlers/http.py F401
scrapy/http/__init__.py F401
scrapy/linkextractors/__init__.py E402 F401
scrapy/selector/__init__.py F401
scrapy/spiders/__init__.py E402 F401
# Issues pending a review:
scrapy/utils/url.py F403 F405
tests/test_loader.py E741

2
.gitignore vendored
View File

@ -14,6 +14,8 @@ htmlcov/
.coverage .coverage
.pytest_cache/ .pytest_cache/
.coverage.* .coverage.*
coverage.*
test-output.*
.cache/ .cache/
.mypy_cache/ .mypy_cache/
/tests/keys/localhost.crt /tests/keys/localhost.crt

View File

@ -21,10 +21,11 @@ collect_ignore = [
*_py_files("tests/CrawlerRunner"), *_py_files("tests/CrawlerRunner"),
] ]
for line in open('tests/ignores.txt'): with open('tests/ignores.txt') as reader:
file_path = line.strip() for line in reader:
if file_path and file_path[0] != '#': file_path = line.strip()
collect_ignore.append(file_path) if file_path and file_path[0] != '#':
collect_ignore.append(file_path)
if not H2_ENABLED: if not H2_ENABLED:
collect_ignore.extend( collect_ignore.extend(

View File

@ -20,20 +20,5 @@ addopts =
--ignore=docs/utils --ignore=docs/utils
markers = markers =
only_asyncio: marks tests as only enabled when --reactor=asyncio is passed only_asyncio: marks tests as only enabled when --reactor=asyncio is passed
flake8-max-line-length = 119 filterwarnings=
flake8-ignore = ignore::DeprecationWarning:twisted.web.test.test_webclient
W503
# Exclude files that are meant to provide top-level imports
# E402: Module level import not at top of file
# F401: Module imported but unused
scrapy/__init__.py E402
scrapy/core/downloader/handlers/http.py F401
scrapy/http/__init__.py F401
scrapy/linkextractors/__init__.py E402 F401
scrapy/selector/__init__.py F401
scrapy/spiders/__init__.py E402 F401
# Issues pending a review:
scrapy/utils/url.py F403 F405
tests/test_loader.py E741

View File

@ -6,12 +6,14 @@ import tempfile
import unittest import unittest
from io import BytesIO from io import BytesIO
from datetime import datetime from datetime import datetime
from warnings import catch_warnings, filterwarnings
import lxml.etree import lxml.etree
from itemadapter import ItemAdapter from itemadapter import ItemAdapter
from scrapy.item import Item, Field from scrapy.item import Item, Field
from scrapy.utils.python import to_unicode from scrapy.utils.python import to_unicode
from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.exporters import ( from scrapy.exporters import (
BaseItemExporter, PprintItemExporter, PickleItemExporter, CsvItemExporter, BaseItemExporter, PprintItemExporter, PickleItemExporter, CsvItemExporter,
XmlItemExporter, JsonLinesItemExporter, JsonItemExporter, XmlItemExporter, JsonLinesItemExporter, JsonItemExporter,
@ -172,10 +174,12 @@ class PythonItemExporterTest(BaseItemExporterTest):
self.assertEqual(type(exported['age'][0]['age'][0]), dict) self.assertEqual(type(exported['age'][0]['age'][0]), dict)
def test_export_binary(self): def test_export_binary(self):
exporter = PythonItemExporter(binary=True) with catch_warnings():
value = self.item_class(name='John\xa3', age='22') filterwarnings('ignore', category=ScrapyDeprecationWarning)
expected = {b'name': b'John\xc2\xa3', b'age': b'22'} exporter = PythonItemExporter(binary=True)
self.assertEqual(expected, exporter.export_item(value)) value = self.item_class(name='John\xa3', age='22')
expected = {b'name': b'John\xc2\xa3', b'age': b'22'}
self.assertEqual(expected, exporter.export_item(value))
def test_nonstring_types_item(self): def test_nonstring_types_item(self):
item = self._get_nonstring_types_item() item = self._get_nonstring_types_item()

View File

@ -515,7 +515,7 @@ class FromCrawlerFileFeedStorage(FileFeedStorage, FromCrawlerMixin):
class DummyBlockingFeedStorage(BlockingFeedStorage): class DummyBlockingFeedStorage(BlockingFeedStorage):
def __init__(self, uri): def __init__(self, uri, *args, feed_options=None):
self.path = file_uri_to_path(uri) self.path = file_uri_to_path(uri)
def _store_in_thread(self, file): def _store_in_thread(self, file):
@ -541,7 +541,7 @@ class LogOnStoreFileStorage:
It can be used to make sure `store` method is invoked. It can be used to make sure `store` method is invoked.
""" """
def __init__(self, uri): def __init__(self, uri, feed_options=None):
self.path = file_uri_to_path(uri) self.path = file_uri_to_path(uri)
self.logger = getLogger() self.logger = getLogger()

View File

@ -1,6 +1,6 @@
import unittest import unittest
from unittest import mock from unittest import mock
from warnings import catch_warnings from warnings import catch_warnings, filterwarnings
from w3lib.encoding import resolve_encoding from w3lib.encoding import resolve_encoding
@ -134,7 +134,9 @@ class BaseResponseTest(unittest.TestCase):
assert isinstance(response.text, str) assert isinstance(response.text, str)
self._assert_response_encoding(response, encoding) self._assert_response_encoding(response, encoding)
self.assertEqual(response.body, body_bytes) self.assertEqual(response.body, body_bytes)
self.assertEqual(response.body_as_unicode(), body_unicode) with catch_warnings():
filterwarnings("ignore", category=ScrapyDeprecationWarning)
self.assertEqual(response.body_as_unicode(), body_unicode)
self.assertEqual(response.text, body_unicode) self.assertEqual(response.text, body_unicode)
def _assert_response_encoding(self, response, encoding): def _assert_response_encoding(self, response, encoding):
@ -345,8 +347,10 @@ class TextResponseTest(BaseResponseTest):
r1 = self.response_class('http://www.example.com', body=original_string, encoding='cp1251') r1 = self.response_class('http://www.example.com', body=original_string, encoding='cp1251')
# check body_as_unicode # check body_as_unicode
self.assertTrue(isinstance(r1.body_as_unicode(), str)) with catch_warnings():
self.assertEqual(r1.body_as_unicode(), unicode_string) filterwarnings("ignore", category=ScrapyDeprecationWarning)
self.assertTrue(isinstance(r1.body_as_unicode(), str))
self.assertEqual(r1.body_as_unicode(), unicode_string)
# check response.text # check response.text
self.assertTrue(isinstance(r1.text, str)) self.assertTrue(isinstance(r1.text, str))

View File

@ -1,6 +1,6 @@
import unittest import unittest
from unittest import mock from unittest import mock
from warnings import catch_warnings from warnings import catch_warnings, filterwarnings
from scrapy.exceptions import ScrapyDeprecationWarning from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.item import ABCMeta, _BaseItem, BaseItem, DictItem, Field, Item, ItemMeta from scrapy.item import ABCMeta, _BaseItem, BaseItem, DictItem, Field, Item, ItemMeta
@ -328,16 +328,18 @@ class BaseItemTest(unittest.TestCase):
class SubclassedItem(Item): class SubclassedItem(Item):
pass pass
self.assertTrue(isinstance(BaseItem(), BaseItem)) with catch_warnings():
self.assertTrue(isinstance(SubclassedBaseItem(), BaseItem)) filterwarnings("ignore", category=ScrapyDeprecationWarning)
self.assertTrue(isinstance(Item(), BaseItem)) self.assertTrue(isinstance(BaseItem(), BaseItem))
self.assertTrue(isinstance(SubclassedItem(), BaseItem)) self.assertTrue(isinstance(SubclassedBaseItem(), BaseItem))
self.assertTrue(isinstance(Item(), BaseItem))
self.assertTrue(isinstance(SubclassedItem(), BaseItem))
# make sure internal checks using private _BaseItem class succeed # make sure internal checks using private _BaseItem class succeed
self.assertTrue(isinstance(BaseItem(), _BaseItem)) self.assertTrue(isinstance(BaseItem(), _BaseItem))
self.assertTrue(isinstance(SubclassedBaseItem(), _BaseItem)) self.assertTrue(isinstance(SubclassedBaseItem(), _BaseItem))
self.assertTrue(isinstance(Item(), _BaseItem)) self.assertTrue(isinstance(Item(), _BaseItem))
self.assertTrue(isinstance(SubclassedItem(), _BaseItem)) self.assertTrue(isinstance(SubclassedItem(), _BaseItem))
def test_deprecation_warning(self): def test_deprecation_warning(self):
""" """

View File

@ -108,7 +108,7 @@ class WarnWhenSubclassedTest(unittest.TestCase):
# ignore subclassing warnings # ignore subclassing warnings
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter('ignore', ScrapyDeprecationWarning) warnings.simplefilter('ignore', MyWarning)
class UserClass(Deprecated): class UserClass(Deprecated):
pass pass

View File

@ -5,8 +5,9 @@ import platform
import unittest import unittest
from datetime import datetime from datetime import datetime
from itertools import count from itertools import count
from warnings import catch_warnings from warnings import catch_warnings, filterwarnings
from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.utils.python import ( from scrapy.utils.python import (
memoizemethod_noargs, binary_is_text, equal_attributes, memoizemethod_noargs, binary_is_text, equal_attributes,
WeakKeyCache, get_func_args, to_bytes, to_unicode, WeakKeyCache, get_func_args, to_bytes, to_unicode,
@ -160,7 +161,11 @@ class UtilsPythonTestCase(unittest.TestCase):
pass pass
_values = count() _values = count()
wk = WeakKeyCache(lambda k: next(_values))
with catch_warnings():
filterwarnings("ignore", category=ScrapyDeprecationWarning)
wk = WeakKeyCache(lambda k: next(_values))
k = _Weakme() k = _Weakme()
v = wk[k] v = wk[k]
self.assertEqual(v, wk[k]) self.assertEqual(v, wk[k])