Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed some deprecated functions and supports #6116

Merged
merged 9 commits into from
Oct 30, 2023

Conversation

Chenwei-Niu
Copy link
Contributor

@Chenwei-Niu Chenwei-Niu commented Oct 18, 2023

commits aabc3db and cc2ad92 resolved #6111
resolves #6109

@Chenwei-Niu Chenwei-Niu changed the title removed Deprecated function scrapy.utils.response.response_httprepr removed some deprecated functions and supports Oct 18, 2023
@codecov
Copy link

codecov bot commented Oct 18, 2023

Codecov Report

Merging #6116 (e5fee58) into master (9b06f6b) will decrease coverage by 0.05%.
The diff coverage is 100.00%.

❗ Current head e5fee58 differs from pull request most recent head 61dfa12. Consider uploading reports for the commit 61dfa12 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6116      +/-   ##
==========================================
- Coverage   88.84%   88.80%   -0.05%     
==========================================
  Files         160      160              
  Lines       11425    11411      -14     
  Branches     1863     1859       -4     
==========================================
- Hits        10151    10133      -18     
- Misses        962      972      +10     
+ Partials      312      306       -6     
Files Coverage Δ
scrapy/utils/log.py 91.83% <100.00%> (+3.37%) ⬆️
scrapy/utils/response.py 88.37% <ø> (-1.83%) ⬇️

... and 3 files with indirect coverage changes

@wRAR
Copy link
Member

wRAR commented Oct 18, 2023

Please use pre-commit locally and run tests locally, and fix the errors produced by them.

scrapy/utils/log.py Outdated Show resolved Hide resolved
@Chenwei-Niu
Copy link
Contributor Author

Chenwei-Niu commented Oct 18, 2023

7afb4ddad370a3187da5297f977b2e7 7afb4ddad370a3187da5297f977b2e7

I think my commits are okay to rerun the workflow. @wRAR

@Chenwei-Niu Chenwei-Niu requested a review from wRAR October 18, 2023 11:34

level = logkws.get("level", logging.INFO)
message = logkws.get("format", logkws.get("msg"))
message = str(logkws.get("msg"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why str()? It wasn't needed before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, wRAR,
The problem here is that logkws.get("msg") returns Any | None instead of Any. The previous code works well since it sets a default value in get() and returns Any.
If we don't take measures here, the type of variable message is Any | None. And an incompatible return value error arises when conducting tests.
c0a2e8ee4fb9c2b8c01599fad954a21
f105114ba8921466f0a84ab5fa76967

I came up with three possible solutions:

  1. message = logkws.get("msg", logkws.get("msg"))
  2. message = logkws.get("msg", "")
  3. message = str(logkws.get("msg"))

Do you have any suggestions or preferences among these three solutions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logkws.get("msg", "") is fine I think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logkws.get("msg", "") is fine I think.

Only if msg is missing from the dict, not if it exists as None, which mypy probably considers a possibility.

If it could exist as None (e.g. it the user made a mistake), logkws.get("msg") or "" should work. Otherwise, what @wRAR said, and assert message is not None after the line should silence mypy about the impossible scenario of logkws containing {"msg": None}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I‘ve already tested, the code below could handle both two scenarios you mentioned.

  1. key msg is missing in the dictionary
  2. Dictionary contains "msg": None
    message = logkws.get("msg", "")
    assert message is not None

If there is no problem, I will modify and commit it.
@Gallaecio @wRAR

Comment on lines 235 to 236
if not {"level", "msg", "args"} <= set(logkws):
warnings.warn("Missing keys in LogFormatter method", ScrapyDeprecationWarning)

if "format" in logkws:
warnings.warn(
"`format` key in LogFormatter methods has been "
"deprecated, use `msg` instead",
ScrapyDeprecationWarning,
)

level = logkws.get("level", logging.INFO)
message = logkws.get("format", logkws.get("msg"))
message = logkws.get("msg", "")
assert message is not None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, now that I look at the warning, I see 2 things:

  • The warning was about msg being a required key, so I think logkws["msg"] instead of logkws.get("msg", "") makes sense now that the warning is gone.
  • There was no warning if logkws["msg"] was None, so we should assume that as a possibility since this dict seems to be user-provided. So we cannot assert, we have to fall back: message = logkws["msg"] or "".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Gallarcio,
I don't really understand why we have to fall back: message = logkws["msg"] or "".

Firstly ,this part of the warnings has been removed according to @wRAR 's comments #6116 (comment)

if not {"level", "msg", "args"} <= set(logkws):
        warnings.warn("Missing keys in LogFormatter method", ScrapyDeprecationWarning)

Secondly:

Gallaecio: The warning was about msg being a required key, so I think logkws["msg"] instead of logkws.get("msg", "") makes sense now that the warning is gone.

Although msg is a requried key, but it is only mentioned and constrained in comments in class LogFormatter, there is no actual code constraints. The user can still provide a dictionary without the msg key. Hence, if key msg is missing, simply using logkws["msg"] would arise a KeyError.

And I agree the point we cannot assert message is not None.
In fact, our current logging system could cope with the case {"msg": None}. So maybe only message = logkws.get("msg", "") should be fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think raising KeyError is fine. I think it is the natural evolution from the warning: first msg was probably not necessary, then it became necessary but it worked without it causing a warning, now it will raise KeyError if missing.

If "msg": None is OK, then I agree the fall back is not needed, and we can simply use logkws["msg"].

Copy link
Member

@Laerte Laerte Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using or operator in case we have the key msg but the value is null we fallback to empty string, since the default empty string value using get() is only when the dict don’t have the key not for the key being null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the best solution for this is would be:

message = logkws.get("msg") or ""

Copy link
Member

@Laerte Laerte Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless like @Gallaecio said we want to raise a exception so the user can fix their code, then we don’t need to get at all only the or operator to handle cases where key exist but is null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Laerte
I finally edited to message = logkws.get("msg") or "" as your suggestion.
Could you please review and merge my pull request?

Thank you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t have write access, we need to wait for @wRAR / @Gallaecio

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @wRAR @Gallaecio , could you please review and merge this pull request?
Thanks!

@Gallaecio Gallaecio merged commit 1f797d0 into scrapy:master Oct 30, 2023
25 checks passed
@Gallaecio
Copy link
Member

Thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants