[PR #964] [MERGED] Ignore Attribute error in Spotify.__del__ #1098

Closed
opened 2026-02-28 00:03:36 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/spotipy-dev/spotipy/pull/964
Author: @johncheng2011
Created: 4/12/2023
Status: Merged
Merged: 5/15/2024
Merged by: @dieser-niko

Base: masterHead: bug/inheriting-exceptions-in-init


📝 Commits (5)

  • 6cc817a ignore if _session does not exist
  • 2063df0 Merge branch 'spotipy-dev:master' into bug/inheriting-exceptions-in-init
  • f647ca7 update changelog
  • 17893a6 Update CHANGELOG.md
  • 6d8d9d1 Merge branch 'master' into bug/inheriting-exceptions-in-init

📊 Changes

2 files changed (+7 additions, -2 deletions)

View changed files

📝 CHANGELOG.md (+2 -0)
📝 spotipy/client.py (+5 -2)

📄 Description

I was trying to create a class that Inherits spotipy.Spotify. But found this interaction where if an exception is raised in the child class's __init__ it will try to run Spotify.__del__ where self._session hasn't been defined yet. So it raises an Attribute Error instead of the Exception from the child class.

For example

from spotipy import Spotify


class Spotify2(Spotify):
    def __init__(self):
        raise Exception
    

spotify_instance = Spotify2()

Will result in this error ignoring the Exception raised in the child class's __init__

Traceback (most recent call last):
  File "/Users/Me/Desktop/test.py", line 22, in <module>
    spotify_instance = Spotify2()
  File "/Users/Me/Desktop/test.py", line 19, in __init__
    raise Exception
Exception
Exception ignored in: <function Spotify.__del__ at 0x101b7df70>
Traceback (most recent call last):
  File "/Users/Me/.pyenv/versions/3.9.15/lib/python3.9/site-packages/spotipy/client.py", line 214, in __del__
    if isinstance(self._session, requests.Session):
AttributeError: 'Spotify2' object has no attribute '_session'

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/spotipy-dev/spotipy/pull/964 **Author:** [@johncheng2011](https://github.com/johncheng2011) **Created:** 4/12/2023 **Status:** ✅ Merged **Merged:** 5/15/2024 **Merged by:** [@dieser-niko](https://github.com/dieser-niko) **Base:** `master` ← **Head:** `bug/inheriting-exceptions-in-init` --- ### 📝 Commits (5) - [`6cc817a`](https://github.com/spotipy-dev/spotipy/commit/6cc817af85c167c5c0099fdd78635e6411c58266) ignore if _session does not exist - [`2063df0`](https://github.com/spotipy-dev/spotipy/commit/2063df0fa546a3b446b640a36f83d6b8cd12c2cc) Merge branch 'spotipy-dev:master' into bug/inheriting-exceptions-in-init - [`f647ca7`](https://github.com/spotipy-dev/spotipy/commit/f647ca75c2a1cde12a2c06703e117e7b7dab0857) update changelog - [`17893a6`](https://github.com/spotipy-dev/spotipy/commit/17893a611539cb23306a4aeeb96b876c90819b98) Update CHANGELOG.md - [`6d8d9d1`](https://github.com/spotipy-dev/spotipy/commit/6d8d9d1f9ba578ff3e515adb305d0dabb9c4eb07) Merge branch 'master' into bug/inheriting-exceptions-in-init ### 📊 Changes **2 files changed** (+7 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+2 -0) 📝 `spotipy/client.py` (+5 -2) </details> ### 📄 Description I was trying to create a class that Inherits spotipy.Spotify. But found this interaction where if an exception is raised in the child class's `__init__` it will try to run `Spotify.__del__` where `self._session` hasn't been defined yet. So it raises an Attribute Error instead of the Exception from the child class. For example ``` from spotipy import Spotify class Spotify2(Spotify): def __init__(self): raise Exception spotify_instance = Spotify2() ``` Will result in this error ignoring the Exception raised in the child class's `__init__` ``` Traceback (most recent call last): File "/Users/Me/Desktop/test.py", line 22, in <module> spotify_instance = Spotify2() File "/Users/Me/Desktop/test.py", line 19, in __init__ raise Exception Exception Exception ignored in: <function Spotify.__del__ at 0x101b7df70> Traceback (most recent call last): File "/Users/Me/.pyenv/versions/3.9.15/lib/python3.9/site-packages/spotipy/client.py", line 214, in __del__ if isinstance(self._session, requests.Session): AttributeError: 'Spotify2' object has no attribute '_session' ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-28 00:03:36 +03:00
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#1098
No description provided.