Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

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

Closed
tchakravarty opened this issue Jun 3, 2015 · 6 comments
Closed

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

tchakravarty opened this issue Jun 3, 2015 · 6 comments

Comments

@tchakravarty
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

@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
Copy link
Contributor

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.

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

No branches or pull requests

3 participants