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

Changed cache_handler.py to utilize Python's Context Management Protocol #991

Merged
merged 34 commits into from
Jan 22, 2025

Conversation

508chris
Copy link
Contributor

I changed two methods within the CacheHandler class, get_cached_token and save_token_to_cache. Instead of opening and closing files manually, I updated these methods to use Python's "with" statement to keep up with good practice in Python programming.
This will also alleviate any errors that occur before the file is closed which would result in allocated resources.
Using the with statement, it simplifies the code as well as provides more reliability to the code base.

I also added an except clause to the get_cached_token method to handle any json decode errors.

Let me know if you have any questions at all.

508chris added 2 commits June 12, 2023 14:11
… to utilize pythons context management protocol to open and close files, instead of opening and closing manually. More information specified in the pull request.
@508chris
Copy link
Contributor Author

updated change log

Copy link
Member

@stephanebruckert stephanebruckert left a comment

Choose a reason for hiding this comment

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

Fixed conflicts but something is wrong with the tests

TypeError: 'Mock' object does not support the context manager protocol

I know this is from a long time ago but any chance you can have another look? Thanks!

with open(self.cache_path) as f:
token_info_string = f.read()
token_info = json.loads(token_info_string)
except IOError as error:
Copy link
Member

Choose a reason for hiding this comment

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

Let's try to also use OSError as it's more general and can catch more issues.

The master branch is currently using OSError https://github.com/spotipy-dev/spotipy/blob/2.25.0/spotipy/cache_handler.py#L84 since #1065

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey! I see the issue with the mock object not supporting the context manager. I'll also swap back to OSError.

Working on the Mock updates now and will test locally. I’ll push the changes once everything is working.

Thank you!

s2t2 and others added 4 commits January 13, 2025 14:21
* Update docs

Add maximum value for limit parameter. See: https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks

* Update CHANGELOG.md

---------

Co-authored-by: Stéphane Bruckert <[email protected]>
… to utilize pythons context management protocol to open and close files, instead of opening and closing manually. More information specified in the pull request.
@508chris
Copy link
Contributor Author

Made some updates to the unit tests. The main issue was incorrect handling of the open function, which didn’t support the context manager protocol when mocked.
I used mock_open with a custom side_effect to properly simulate file handling behavior.

Let me know what you think and/or if there is anything else needed from my end.
Thank you!

@stephanebruckert
Copy link
Member

Thanks for the quick updates! Tests ran but seem to be failing, please can you check again? 🙏

I've now relaxed the rule that prevented to run the github action workflow for first-time contributors, so if you push, you will be able to see if it passes directly.

@508chris
Copy link
Contributor Author

Hi! Sorry about that. Thank you for looking into it. Yes I am checking again and will ensure the tests pass.
Also thanks for making the making the rule more lenient.

508chris and others added 9 commits January 18, 2025 15:02
… to utilize pythons context management protocol to open and close files, instead of opening and closing manually. More information specified in the pull request.
* Update docs

Add maximum value for limit parameter. See: https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks

* Update CHANGELOG.md

---------

Co-authored-by: Stéphane Bruckert <[email protected]>
… to utilize pythons context management protocol to open and close files, instead of opening and closing manually. More information specified in the pull request.
@stephanebruckert
Copy link
Member

Tests still not passing (I modified the workflows again so we can see the results directly, hopeful they always run now).

I also got a version working for the file context manager and mocking, feel free to copy everything from it master...context-manager-mock

Copy link
Member

@stephanebruckert stephanebruckert left a comment

Choose a reason for hiding this comment

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

Approved by mistake as checks were green but they didn't include all tests

@508chris
Copy link
Contributor Author

Apologies for the confusion — I was pushing and syncing changes to my forked version while testing the workflow, but I haven't finalized all the fixes yet. I’ll review the updates in your version to see how they compare with my current progress.

Thank you for staying on top of this and for the helpful pointers. I’ll finalize my changes by tomorrow or the day after and push everything again for review.

@stephanebruckert
Copy link
Member

np, thanks!

@508chris
Copy link
Contributor Author

Hey, I am testing out your context-manager-mock branch and getting fails/errors locally. The failures are due to (I believe) hitting spotify api limits. Also seeing failures for SSL errors. Have you experienced anything similar?

Updated integration tests to mock `_get` and `_post` methods in the Spotify class
@508chris
Copy link
Contributor Author

Actually - all tests passed on my fork when I pushed some changes. I believe the tests are failing on the upstream repository side as I do not have access to configure the required secrets for the workflow. Let me know what you think. Thank you!

@stephanebruckert stephanebruckert merged commit 1dbbbf6 into spotipy-dev:master Jan 22, 2025
7 of 12 checks passed
@stephanebruckert
Copy link
Member

I believe the tests are failing on the upstream repository side as I do not have access to configure the required secrets for the workflow

That's right! I've merged it and it all passed https://github.com/spotipy-dev/spotipy/actions/runs/12919090082

Awesome stuff thank you for jumping on this again after a year!

@508chris
Copy link
Contributor Author

I only added those mocks as I was getting errors on test_add_to_queue tests due to "missing 1 required positional argument: 'mock_post'". Might've been missing something sorry bout that.

Thank you for looking into this as well as working with me on it. Learned a lot and very appreciative of the opportunity to contribute.

Thanks again!
-Chris

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

Successfully merging this pull request may close these issues.

4 participants