[GH-ISSUE #918] TypeError in oauth2 #550

Closed
opened 2026-02-27 23:23:17 +03:00 by kerem · 16 comments
Owner

Originally created by @shibli-mueed on GitHub (Dec 10, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/918

In file spotipy/oauth2.py in Class SpotifyAuthBase method __del__. Both arguments were of different types.

File "C:\Storage\Python Programs\project_vid_ed\.vEnv\lib\site-packages\spotipy\oauth2.py", line 156, in __del__
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

Before the code was:

def __del__(self):
        """Make sure the connection (pool) gets closed"""
        if isinstance(self._session,requests.Session):
            self._session.close()

After fixing:

def __del__(self):
        """Make sure the connection (pool) gets closed"""
        # print(type(self._session))
        if isinstance(type(self._session),type(requests.Session)):
            self._session.close()

Environment:

  • OS: Windows 11
  • Python version: 3.10.4
  • spotipy version: 2.21.0
  • IDE: Visual Studio Code
Originally created by @shibli-mueed on GitHub (Dec 10, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/918 In file *spotipy/oauth2.py* in Class `SpotifyAuthBase` method `__del__`. Both arguments were of different types. ``` File "C:\Storage\Python Programs\project_vid_ed\.vEnv\lib\site-packages\spotipy\oauth2.py", line 156, in __del__ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union ``` **Before the code was:** ``` def __del__(self): """Make sure the connection (pool) gets closed""" if isinstance(self._session,requests.Session): self._session.close() ``` **After fixing:** ``` def __del__(self): """Make sure the connection (pool) gets closed""" # print(type(self._session)) if isinstance(type(self._session),type(requests.Session)): self._session.close() ``` **Environment:** - OS: Windows 11 - Python version: 3.10.4 - spotipy version: 2.21.0 - IDE: Visual Studio Code
kerem 2026-02-27 23:23:17 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@dieser-niko commented on GitHub (Jan 3, 2023):

I'm not quite sure how you got this error, but the code seems to work without type().
Also I'm not sure if you know how isinstance or type even work. Look at this example:

>>> import requests
>>> type(requests.Session)
<class 'type'>

As you can see, it returns class type which doesn't directly relate to our session we want to check.

This would also further break the code:

>>> import spotipy
>>> import requests
>>> session = requests.Session()
>>> oauth = spotipy.oauth2.SpotifyAuthBase(requests_session=session)

>>> isinstance(type(oauth._session), type(requests.Session))
True  # Yes, it returned True, but also this:
>>> oauth._session = 5  # or any other non session object
>>> isinstance(type(oauth._session), type(requests.Session))
True

So just to make sure that you understand the purpose of the __del__ function, here a small explanation:

If the class gets destroyed (for example by stopping the python script), it will call this function so it can properly disconnect itself from the server. But before closing the connection, the function has to check if self._session is actually a session. If so, then it can close the connection. But if the self._session value got changed to an unfamiliar object, then there's a chance that the self._session doesn't have a close() function as an attribute. And without that check it would throw an AttributeError.

I hope you understand that this is not a bug, but an intended feature.

(Python 3.10.5)

<!-- gh-comment-id:1369815625 --> @dieser-niko commented on GitHub (Jan 3, 2023): I'm not quite sure how you got this error, but the code seems to work without `type()`. Also I'm not sure if you know how `isinstance` or `type` even work. Look at this example: ```python3 >>> import requests >>> type(requests.Session) <class 'type'> ``` As you can see, it returns class `type` which doesn't directly relate to our session we want to check. This would also further break the code: ```python3 >>> import spotipy >>> import requests >>> session = requests.Session() >>> oauth = spotipy.oauth2.SpotifyAuthBase(requests_session=session) >>> isinstance(type(oauth._session), type(requests.Session)) True # Yes, it returned True, but also this: >>> oauth._session = 5 # or any other non session object >>> isinstance(type(oauth._session), type(requests.Session)) True ``` So just to make sure that you understand the purpose of the `__del__` function, here a small explanation: If the class gets destroyed (for example by stopping the python script), it will call this function so it can properly disconnect itself from the server. But before closing the connection, the function has to check if `self._session` is actually a session. If so, then it can close the connection. But if the `self._session` value got changed to an unfamiliar object, then there's a chance that the `self._session` doesn't have a `close()` function as an attribute. And without that check it would throw an AttributeError. I hope you understand that this is not a bug, but an intended feature. (Python 3.10.5)
Author
Owner

@buanzo commented on GitHub (Apr 14, 2023):

Just to add that I am getting the same error in oauth2.py:156, but also in client.py line 214.

Exception ignored in: <function SpotifyAuthBase.__del__ at 0x7f2477666b90>
Traceback (most recent call last):
  File "/home/buanzo/.local/lib/python3.10/site-packages/spotipy/oauth2.py", line 156, in __del__
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

Exception ignored in: <function Spotify.__del__ at 0x7f2477647e20>
Traceback (most recent call last):
  File "/home/buanzo/.local/lib/python3.10/site-packages/spotipy/client.py", line 214, in __del__
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
<!-- gh-comment-id:1509383199 --> @buanzo commented on GitHub (Apr 14, 2023): Just to add that I am getting the same error in oauth2.py:156, but also in client.py line 214. ``` Exception ignored in: <function SpotifyAuthBase.__del__ at 0x7f2477666b90> Traceback (most recent call last): File "/home/buanzo/.local/lib/python3.10/site-packages/spotipy/oauth2.py", line 156, in __del__ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union Exception ignored in: <function Spotify.__del__ at 0x7f2477647e20> Traceback (most recent call last): File "/home/buanzo/.local/lib/python3.10/site-packages/spotipy/client.py", line 214, in __del__ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union ```
Author
Owner

@dieser-niko commented on GitHub (Apr 15, 2023):

Can you give me a code snippet that would trigger the error?

<!-- gh-comment-id:1509719400 --> @dieser-niko commented on GitHub (Apr 15, 2023): Can you give me a code snippet that would trigger the error?
Author
Owner

@buanzo commented on GitHub (Apr 15, 2023):

There is nothing strange in the code, but I can tell you it only happens when using flask, and the script is reloaded because of a change:

 * Detected change in '/home/buanzo/git/spotifyapp/main.py', reloading
Exception ignored in: <function SpotifyAuthBase.__del__ at 0x7f3ffdc42b90>
Traceback (most recent call last):
  File "/home/buanzo/.local/lib/python3.10/site-packages/spotipy/oauth2.py", line 156, in __del__
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
Exception ignored in: <function Spotify.__del__ at 0x7f3ffdc1fe20>
Traceback (most recent call last):
  File "/home/buanzo/.local/lib/python3.10/site-packages/spotipy/client.py", line 214, in __del__
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

I used the app for a solid one minute (it has 3 flask routes only, heh, I just started it yesterday). Then had the euroka moment of forcing a reload. And immediatelly, the error appeared.

Hope this helps a bit. I can email you the full script if you want, but I assure you, it's a skeleton app written off gpt.

<!-- gh-comment-id:1509747915 --> @buanzo commented on GitHub (Apr 15, 2023): There is nothing strange in the code, but I can tell you it only happens when using flask, and the script is reloaded because of a change: ``` * Detected change in '/home/buanzo/git/spotifyapp/main.py', reloading Exception ignored in: <function SpotifyAuthBase.__del__ at 0x7f3ffdc42b90> Traceback (most recent call last): File "/home/buanzo/.local/lib/python3.10/site-packages/spotipy/oauth2.py", line 156, in __del__ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union Exception ignored in: <function Spotify.__del__ at 0x7f3ffdc1fe20> Traceback (most recent call last): File "/home/buanzo/.local/lib/python3.10/site-packages/spotipy/client.py", line 214, in __del__ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union ``` I used the app for a solid one minute (it has 3 flask routes only, heh, I *just* started it yesterday). Then had the euroka moment of forcing a reload. And immediatelly, the error appeared. Hope this helps a bit. I can email you the full script if you want, but I assure you, it's a skeleton app written off gpt.
Author
Owner

@buanzo commented on GitHub (Apr 15, 2023):

At the time of the reload, type(request.Session) is NoneType in /home/buanzo/.local/lib/python3.10/site-packages/spotipy/oauth2.py:156

<!-- gh-comment-id:1509764255 --> @buanzo commented on GitHub (Apr 15, 2023): At the time of the reload, type(request.Session) is NoneType in /home/buanzo/.local/lib/python3.10/site-packages/spotipy/oauth2.py:156
Author
Owner

@dieser-niko commented on GitHub (Apr 15, 2023):

I can email you the full script if you want, but I assure you, it's a skeleton app written off gpt.

That would be great, because I just created my own little flask app with spotipy and it works as expected.
It would be nice if you could send it here, I don't really like having my email address out in the open.

<!-- gh-comment-id:1509827298 --> @dieser-niko commented on GitHub (Apr 15, 2023): > I can email you the full script if you want, but I assure you, it's a skeleton app written off gpt. That would be great, because I just created my own little flask app with spotipy and it works as expected. It would be nice if you could send it here, I don't really like having my email address out in the open.
Author
Owner

@buanzo commented on GitHub (Apr 15, 2023):

Mine is known. @.***

you mail me, and i will not disclose it.

cheers

On Sat, Apr 15, 2023, 10:31 Niko @.***> wrote:

I can email you the full script if you want, but I assure you, it's a
skeleton app written off gpt.

That would be great, because I just created my own little flask app with
spotipy and it works as expected.
It would be nice if you could send it here, I don't really like having my
email address out in the open.


Reply to this email directly, view it on GitHub
https://github.com/spotipy-dev/spotipy/issues/918#issuecomment-1509827298,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAEHBTY6V34BXJUK4HIRMZTXBKPKJANCNFSM6AAAAAASZ6Z7GA
.
You are receiving this because you commented.Message ID:
@.***>

<!-- gh-comment-id:1509834619 --> @buanzo commented on GitHub (Apr 15, 2023): Mine is known. ***@***.*** you mail me, and i will not disclose it. cheers On Sat, Apr 15, 2023, 10:31 Niko ***@***.***> wrote: > I can email you the full script if you want, but I assure you, it's a > skeleton app written off gpt. > > That would be great, because I just created my own little flask app with > spotipy and it works as expected. > It would be nice if you could send it here, I don't really like having my > email address out in the open. > > — > Reply to this email directly, view it on GitHub > <https://github.com/spotipy-dev/spotipy/issues/918#issuecomment-1509827298>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/AAEHBTY6V34BXJUK4HIRMZTXBKPKJANCNFSM6AAAAAASZ6Z7GA> > . > You are receiving this because you commented.Message ID: > ***@***.***> >
Author
Owner

@dieser-niko commented on GitHub (Apr 15, 2023):

It got censored. Try to add the address to your github profile and set it to public

<!-- gh-comment-id:1509845025 --> @dieser-niko commented on GitHub (Apr 15, 2023): It got censored. Try to add the address to your github profile and set it to public
Author
Owner

@buanzo commented on GitHub (Apr 15, 2023):

Google my alias, buanzo. find me anywhere. cheers.

On Sat, Apr 15, 2023, 11:01 Niko @.***> wrote:

It got censored. Try to add the address to your github profile and set it
to public


Reply to this email directly, view it on GitHub
https://github.com/spotipy-dev/spotipy/issues/918#issuecomment-1509845025,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAEHBTY6QIKAAWI6NGRCORLXBKS27ANCNFSM6AAAAAASZ6Z7GA
.
You are receiving this because you commented.Message ID:
@.***>

<!-- gh-comment-id:1509846039 --> @buanzo commented on GitHub (Apr 15, 2023): Google my alias, buanzo. find me anywhere. cheers. On Sat, Apr 15, 2023, 11:01 Niko ***@***.***> wrote: > It got censored. Try to add the address to your github profile and set it > to public > > — > Reply to this email directly, view it on GitHub > <https://github.com/spotipy-dev/spotipy/issues/918#issuecomment-1509845025>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/AAEHBTY6QIKAAWI6NGRCORLXBKS27ANCNFSM6AAAAAASZ6Z7GA> > . > You are receiving this because you commented.Message ID: > ***@***.***> >
Author
Owner

@stephanebruckert commented on GitHub (Apr 16, 2023):

@buanzo why not paste a Minimal reproducible example here? It allows anyone with time to easily try it.

<!-- gh-comment-id:1510293547 --> @stephanebruckert commented on GitHub (Apr 16, 2023): @buanzo why not paste a [Minimal reproducible example](https://en.wikipedia.org/wiki/Minimal_reproducible_example) here? It allows anyone with time to easily try it.
Author
Owner

@timhagel commented on GitHub (Jul 19, 2023):

On my Flask app I was starting the program with python (file) instead of flask run and it would give me this error

<!-- gh-comment-id:1642587908 --> @timhagel commented on GitHub (Jul 19, 2023): On my Flask app I was starting the program with `python (file)` instead of `flask run` and it would give me this error
Author
Owner

@dieser-niko commented on GitHub (Jul 20, 2023):

Can you provide an example code so that I can reproduce the issue

<!-- gh-comment-id:1643308450 --> @dieser-niko commented on GitHub (Jul 20, 2023): Can you provide an example code so that I can reproduce the issue
Author
Owner

@timhagel commented on GitHub (Jul 20, 2023):

@dieser-niko Yeah here's a really basic Flask and Spotipy app

app.py

from flask import Flask, jsonify, request, render_template
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy

app = Flask(__name__)

sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())


@app.after_request
def add_header(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers',
                         'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods',
                         'GET,PUT,POST,DELETE,OPTIONS')
    return response


@app.route('/song', methods=['POST'])
def hello():

    print('Incoming..')
    data = request.form.get('data')
    print(data)
    print("Finding song")
    songresult = sp.search(data)
    print(songresult['tracks']['items'][0]['uri'])
    return (songresult['tracks']['items'][0]['uri']), 200

If you run this with python app.py

Exception ignored in: <function Spotify.__del__ at 0x7fb5326f6320>
Traceback (most recent call last):
  File "/home/timot/anaconda3/lib/python3.10/site-packages/spotipy/client.py", line 214, in __del__
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
Exception ignored in: <function SpotifyAuthBase.__del__ at 0x7fb5323e9000>
Traceback (most recent call last):
  File "/home/timot/anaconda3/lib/python3.10/site-packages/spotipy/oauth2.py", line 156, in __del__
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

And if you run with flask run it should start without the error

Hope this helps

<!-- gh-comment-id:1644560863 --> @timhagel commented on GitHub (Jul 20, 2023): @dieser-niko Yeah here's a really basic Flask and Spotipy app ### app.py ``` from flask import Flask, jsonify, request, render_template from spotipy.oauth2 import SpotifyClientCredentials import spotipy app = Flask(__name__) sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials()) @app.after_request def add_header(response): response.headers.add('Access-Control-Allow-Origin', '*') response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization') response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS') return response @app.route('/song', methods=['POST']) def hello(): print('Incoming..') data = request.form.get('data') print(data) print("Finding song") songresult = sp.search(data) print(songresult['tracks']['items'][0]['uri']) return (songresult['tracks']['items'][0]['uri']), 200 ``` If you run this with `python app.py` ``` Exception ignored in: <function Spotify.__del__ at 0x7fb5326f6320> Traceback (most recent call last): File "/home/timot/anaconda3/lib/python3.10/site-packages/spotipy/client.py", line 214, in __del__ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union Exception ignored in: <function SpotifyAuthBase.__del__ at 0x7fb5323e9000> Traceback (most recent call last): File "/home/timot/anaconda3/lib/python3.10/site-packages/spotipy/oauth2.py", line 156, in __del__ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union ``` And if you run with `flask run` it should start without the error Hope this helps
Author
Owner

@dieser-niko commented on GitHub (Jul 21, 2023):

Finally an example. Also I can't believe I'm saying this, but it works on my machine..
My guess is that it might be because of anaconda. Can you try running it in native python instead?

Also about that python app.py, you need to put app.run() at the end of the script to make it actually run.

<!-- gh-comment-id:1645260860 --> @dieser-niko commented on GitHub (Jul 21, 2023): Finally an example. Also I can't believe I'm saying this, but it works on my machine.. My guess is that it might be because of anaconda. Can you try running it in native python instead? Also about that `python app.py`, you need to put `app.run()` at the end of the script to make it actually run.
Author
Owner

@buanzo commented on GitHub (Jul 21, 2023):

Sorry Niko, I totally changed my code in the meantime and the problem went
away, hence my lack of example -offering.

On Fri, Jul 21, 2023, 06:10 Niko @.***> wrote:

Finally an example. Also I can't believe I'm saying this, but it works on
my machine..
My guess is that it might be because of anaconda. Can you try running it
in native python instead?

Also about that python app.py, you need to put app.run() at the end of
the script to make it actually run.


Reply to this email directly, view it on GitHub
https://github.com/spotipy-dev/spotipy/issues/918#issuecomment-1645260860,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAEHBT6TF573QQBPQTM26NTXRJBPNANCNFSM6AAAAAASZ6Z7GA
.
You are receiving this because you were mentioned.Message ID:
@.***>

<!-- gh-comment-id:1645411758 --> @buanzo commented on GitHub (Jul 21, 2023): Sorry Niko, I totally changed my code in the meantime and the problem went away, hence my lack of example -offering. On Fri, Jul 21, 2023, 06:10 Niko ***@***.***> wrote: > Finally an example. Also I can't believe I'm saying this, but it works on > my machine.. > My guess is that it might be because of anaconda. Can you try running it > in native python instead? > > Also about that python app.py, you need to put app.run() at the end of > the script to make it actually run. > > — > Reply to this email directly, view it on GitHub > <https://github.com/spotipy-dev/spotipy/issues/918#issuecomment-1645260860>, > or unsubscribe > <https://github.com/notifications/unsubscribe-auth/AAEHBT6TF573QQBPQTM26NTXRJBPNANCNFSM6AAAAAASZ6Z7GA> > . > You are receiving this because you were mentioned.Message ID: > ***@***.***> >
Author
Owner

@dieser-niko commented on GitHub (Mar 3, 2025):

Closing in favour of #621 as it seems to be related (garbage collection)

<!-- gh-comment-id:2693675579 --> @dieser-niko commented on GitHub (Mar 3, 2025): Closing in favour of #621 as it seems to be related (garbage collection)
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#550
No description provided.