Repository navigation
Stop calling the deprecated Headers.to_unicode_dict() - #405
Open
viren-ramani wants to merge 1 commit into
Open
viren-ramani wants to merge 1 commit into
viren-ramani wants to merge 1 commit into
Conversation
Scrapy 2.20 deprecated Headers.to_unicode_dict() in favour of to_tuple_list(), so use_scrapy_headers() logged a ScrapyDeprecationWarning for every request. On Scrapy 2.15+, where to_tuple_list() exists, build the same case-insensitive dict from it; older versions keep using to_unicode_dict(), which does not warn there. Behaviour is unchanged: repeated headers are still joined with ",", headers without values (e.g. User-Agent: None) are still kept as empty strings, and header order is preserved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scrapy 2.20 deprecated
Headers.to_unicode_dict()in favour ofto_tuple_list().use_scrapy_headers()calls it for every request, so with Scrapy 2.20+ each request logs:to_tuple_list()was added in Scrapy 2.15, so it is used from 2.15 on, and older versions keep callingto_unicode_dict(), which does not warn there.to_tuple_list()is not a drop-in replacement, so the dict is rebuilt to match whatto_unicode_dict()returned:,.user-agent,refererandhost, while Scrapy storesUser-Agentand so on. Scrapy'sCaseInsensitiveDictcannot be reused becausescrapy.utils.datatypesis deprecated in 2.20 too, soheaders.pyhas a small private equivalent.to_tuple_list()skips them, which would change what is sent forheaders={"User-Agent": None}: Chromium currently gets an emptyUser-Agent, and would get the browser's one instead.The new tests call
use_scrapy_headers()directly.test_no_deprecation_warningturnsScrapyDeprecationWarninginto an error and fails without the change, with the warning above. The others cover merging, case-insensitive lookups, headers without values and header order, and pass both before and after the change.Verified locally with Python 3.11 on macOS arm64:
tox -e py(Scrapy 2.20): 158 passed, 10 skipped, 2 failed. The 2 failures (TestBodyEncoding,'gbk' == 'gb18030'with w3lib 2.6.0) also fail onmain.tox -e py-pinned(Scrapy 2.7): the new tests pass, and the other results matchmain.test_extensions.py:75E1101, whichmainhas too.