-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Fix #1781: compact_items expects dict but sometimes passed list #1862
Conversation
Hey HA music wizards, just wondering if I can do anything on this. Totally get you're probably busy but let me know! It would be cool to get this enabled on the public release. |
custom_components/mass/services.py
Outdated
@@ -59,14 +59,16 @@ def compact_item(item: dict[str, Any]) -> dict[str, Any]: | |||
item[key] = compact_item(value) | |||
elif isinstance(value, list): | |||
for subitem in value: | |||
compact_item(subitem) | |||
if isinstance(subitem, dict): |
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.
It's always better to guard and breakout early. So in this case:
if not isinstance(subitem, dict):
continue
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.
btw: maybe we also want to catch lists here ?
Thanks, happy to make that update, and I'll think about how to capture lists as well. I'll have time to push some changes on Sunday probably. Appreciate the review! |
Pushed the guard clauses and verified working. I don't think anything is needed for lists here, they should just work I think? If required, can we do in separate merge? |
Thanks! |
This pull request fixes #1781 by implementing a simple type check before invoking
compact_items
. I wasn't able to find any docs on preferences for contributing, so please shoot this back to me if any changes are needed!Summary:
With Spotify connected, running the
mass.search
service causes an error to occur iftrack
is passed as amedia_type
. This occurs because thecompact_items
function inmass/services.py
expects and is type hinted to exclusively accept a dictionary, but when called fromhandle_search
or recursively from withincompact_items
, no check is performed to ensure the correct type is passed. This usually does not cause errors, but in some circumstances lists are passed, in the case of the below example for theexternal_ids
field.Recreation YAML:
Error:
The error
TypeError: pop expected at most 1 argument, got 2
is thrown becauselist.pop
expects a single index whereasdict.pop
expects a key and optionally a default value. This causes improper arguments to be passed when a list is given tocompact_items
.Working Local Demonstration: