-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Avoid using get on a defaultdict #5766
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
Conversation
Pull Request Test Coverage Report for Build 1789962783
π - Coveralls |
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.
But the proposed change would also result in a KeyError crash, right ?
@@ -1659,7 +1659,7 @@ def _get_messages_to_set( | |||
else: | |||
category_id_formatted = category_id | |||
if category_id_formatted is not None: | |||
for _msgid in self.msgs_store._msgs_by_category.get(category_id_formatted): | |||
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]: |
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.
for _msgid in self.msgs_store._msgs_by_category[category_id_formatted]: | |
for _msgid in self.msgs_store._msgs_by_category.get(category_id_formatted, []): |
It won't, as it's a defaultdict. If the key doesn't exist it'll make one by the default factory ( |
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 missed that it was a defaultdict, thank you for the fix !
Type of Changes
Description
I was poking around Pylint internals, trying to add new issue categories to the codebase, and stumbled upon this bug.
Since
MessageDefinitionStore._msgs_by_category
is adefaultdict(list)
, using.get()
here can break the code on bad input, because it will returnNone
, which is not iterable.