discogs / discogs_client

DEPRECATED - Official Python Client for the Discogs API
http://www.discogs.com/developers
Other
479 stars 132 forks source link

TypeError: __repr__ returned non-string (type bytes) #55

Closed tchakravarty closed 9 years ago

tchakravarty commented 9 years ago

Here is a minimal example of my attempts to interact with the Discogs API:

from SensitiveInformation.discogs_application_info import provide_discogs_auth, provide_verifier
import discogs_client

discogs_consumer_key, discogs_consumer_secret = provide_discogs_auth()
discogs = discogs_client.Client(user_agent="ThoughfulMachineLearning",
                                consumer_key=discogs_consumer_key,
                          consumer_secret=discogs_consumer_secret)
discogs_auth_url = discogs.get_authorize_url()
discogs.get_access_token(verifier=provide_verifier())
discogs.identity()

The functions provide_discogs_auth and provide_verifier simply return the consumer key & secret and the verifier from user authorization. get_access_token returns the access key and secret as expected.

However, on the last line, when I make an API call, I get:

Out[38]: In[39]: discogs.identity()
Traceback (most recent call last):
Out[39]:   File "/usr/local/lib/python3.4/dist-packages/IPython/core/formatters.py", line 219, in catch_format_error
    r = method(self, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/IPython/core/formatters.py", line 690, in __call__
    printer.pretty(obj)
  File "/usr/local/lib/python3.4/dist-packages/IPython/lib/pretty.py", line 407, in pretty
    return _default_pprint(obj, self, cycle)
  File "/usr/local/lib/python3.4/dist-packages/IPython/lib/pretty.py", line 527, in _default_pprint
    _repr_pprint(obj, p, cycle)
  File "/usr/local/lib/python3.4/dist-packages/IPython/lib/pretty.py", line 709, in _repr_pprint
    output = repr(obj)
TypeError: __repr__ returned non-string (type bytes)

Not sure if this is related to IPython or the client library, but would appreciate help either way. Thanks.

rodneykeeling commented 9 years ago

Hi @tchakravarty,

I haven't seen this issue before. I use IPython quite a lot myself and haven't seen this. Are you using an IPython shell (i.e., ipython -i) or IPython Notebook? My suggestion would be to put a break point (import ipdb; ipdb.set_trace()) before the line output = repr(obj) and inspect the obj variable. That appears to be the wrong type, so perhaps inspecting it a bit could provide some clues.

Let me know if you find anything.

Thanks, Rodney

tchakravarty commented 9 years ago

Hi @rodneykeeling, I am using the IPython console from within PyCharm. But please note that Martijn Pieters is claiming that this is a bug over on SO, and he is not often wrong... :-)

rodneykeeling commented 9 years ago

Ah, I see. I am still primarily using Python 2.7.9.

I added those .encode('utf-8') bits in there because some characters in Python 2 were having encoding issues. I'll see if I can find a nice middleground to get both Python 2 & 3 working.

Thanks, Rodney

takluyver commented 9 years ago

Not very elegant, but something like this should work:

r = ... # create something with unicode
if sys.version_info[0] < 3:
    return r.encode('utf-8')
return r

UTF-8 will not always be the right encoding, especially on Windows, but if it's mostly ascii characters you can get away with it.

rodneykeeling commented 9 years ago

@takluyver Yeah, creating a method that does that in one of the parent classes seems like the least intrusive option since there are so many __repr__() methods.

rodneykeeling commented 9 years ago

Got a PR up here #56. Would love it if someone could test it. I tried it using Python 2.7.9 and 3.4.0 and it seems to work fine.