[GH-ISSUE #571] UnboundLocalError: local variable 'response' referenced before assignment #342

Closed
opened 2026-02-27 23:22:05 +03:00 by kerem · 1 comment
Owner

Originally created by @peterbe on GitHub (Sep 5, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/571

Describe the bug
If something goes wrong in self._session.request() the variable response will never be set. Leading to a
UnboundLocalError: local variable 'response' referenced before assignment

Your code
Sorry.
Error from my Rollbar:
Screen Shot 2020-09-05 at 9 51 41 AM

Expected behavior
Requests fail and that's fine. Judging from skimming the code, it's trying to package a message to go with raising a SpotifyException but it's failing to do so.

Output
Paste and format errors (with complete stacktrace) or logs. Make sure to remove sensitive information.

Environment:

  • spotipy version: spotipy==2.13.0

Additional context
Relying on UnboundLocalError from a caller isn't nice. If it was SpotifyException I could do something like this:

from spotipy.exceptions import SpotifyException

try:
    something_that_calls_spotipy(...)
except SpotifyException as err:
    logger.warning("Oh no. Spotify exception...")
Originally created by @peterbe on GitHub (Sep 5, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/571 **Describe the bug** If something goes wrong in `self._session.request()` the variable `response` will never be set. Leading to a `UnboundLocalError: local variable 'response' referenced before assignment` **Your code** Sorry. Error from my Rollbar: <img width="1159" alt="Screen Shot 2020-09-05 at 9 51 41 AM" src="https://user-images.githubusercontent.com/26739/92306493-71e43680-ef5d-11ea-93a5-54f275cbc12d.png"> **Expected behavior** Requests fail and that's fine. Judging from skimming the code, it's trying to package a message to go with raising a `SpotifyException` but it's failing to do so. **Output** Paste and format errors (with complete stacktrace) or logs. Make sure to remove sensitive information. **Environment:** - spotipy version: `spotipy==2.13.0` **Additional context** Relying on `UnboundLocalError` from a caller isn't nice. If it was `SpotifyException` I could do something like this: ```python from spotipy.exceptions import SpotifyException try: something_that_calls_spotipy(...) except SpotifyException as err: logger.warning("Oh no. Spotify exception...") ```
kerem 2026-02-27 23:22:05 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@stephanebruckert commented on GitHub (Oct 2, 2020):

Should be fixed by https://github.com/plamere/spotipy/pull/580

<!-- gh-comment-id:702965777 --> @stephanebruckert commented on GitHub (Oct 2, 2020): Should be fixed by https://github.com/plamere/spotipy/pull/580
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#342
No description provided.