-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Conversation
Codecov Report
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
|
Please use pre-commit locally and run tests locally, and fix the errors produced by them. |
I think my commits are okay to rerun the workflow. @wRAR |
scrapy/utils/log.py
Outdated
|
||
level = logkws.get("level", logging.INFO) | ||
message = logkws.get("format", logkws.get("msg")) | ||
message = str(logkws.get("msg")) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I came up with three possible solutions:
message = logkws.get("msg", logkws.get("msg"))
message = logkws.get("msg", "")
message = str(logkws.get("msg"))
Do you have any suggestions or preferences among these three solutions?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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}
.
There was a problem hiding this comment.
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.
- key
msg
is missing in the dictionary - 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
…dictionary contains 'msg': None
scrapy/utils/log.py
Outdated
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 |
There was a problem hiding this comment.
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 thinklogkws["msg"]
instead oflogkws.get("msg", "")
makes sense now that the warning is gone. - There was no warning if
logkws["msg"]
wasNone
, so we should assume that as a possibility since this dict seems to be user-provided. So we cannotassert
, we have to fall back:message = logkws["msg"] or ""
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"]
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ""
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
Thanks everyone! |
commits aabc3db and cc2ad92 resolved #6111
resolves #6109