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

TypeError in oauth2 #918

Open
shibli-mueed opened this issue Dec 10, 2022 · 15 comments
Open

TypeError in oauth2 #918

shibli-mueed opened this issue Dec 10, 2022 · 15 comments
Labels

Comments

@shibli-mueed
Copy link

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
@dieser-niko
Copy link

dieser-niko commented 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)

@buanzo
Copy link

buanzo commented 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

@dieser-niko
Copy link

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

@buanzo
Copy link

buanzo commented 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.

@buanzo
Copy link

buanzo commented 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

@dieser-niko
Copy link

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.

@buanzo
Copy link

buanzo commented Apr 15, 2023 via email

@dieser-niko
Copy link

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

@buanzo
Copy link

buanzo commented Apr 15, 2023 via email

@stephanebruckert
Copy link
Member

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

@timhagel
Copy link

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

@dieser-niko
Copy link

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

@timhagel
Copy link

@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

@dieser-niko
Copy link

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.

@buanzo
Copy link

buanzo commented Jul 21, 2023 via email

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

No branches or pull requests

5 participants