Cogniac / cogniac-sdk-py

Python SDK for Cogniac Public API
Apache License 2.0
5 stars 6 forks source link

Subjects __repr__ and __str__ methods cause TypeError #89

Closed CogniacViSean closed 3 years ago

CogniacViSean commented 3 years ago

Found this because calling CogniacConnection.get_all_subjects() causes:

TypeError: __repr__ returned non-string (type bytes)

I believe it's these lines, the Application object doesn't have this problem, and returns the string directly: https://github.com/Cogniac/cogniac-sdk-py/blob/11f985a07bbaf5616315abbb0a56c75bc7928472/cogniac/subject.py#L216 https://github.com/Cogniac/cogniac-sdk-py/blob/11f985a07bbaf5616315abbb0a56c75bc7928472/cogniac/subject.py#L220

Perhaps related to Python 2 to 3 compatibility?

sergii-bond commented 3 years ago

https://portingguide.readthedocs.io/en/latest/strings.html

sergii-bond commented 3 years ago

test case to reproduce:

# -*- coding: utf-8 -*-

import sys 

class Test(object):
    def __repr__(self):
        s = "%s" % u"сок"
        return s.encode(sys.stdout.encoding)

    def __str__(self):
        s = "%s" % u"сок"
        return s.encode(sys.stdout.encoding)

x = Test()
print(x)

python2 runs ok, python3 returns TypeError: __str__ returned non-string (type bytes)

CogniacViSean commented 3 years ago

This six library has a utility decorator that can handle this, worth adding to the project? It may have other tools for python2/3 compatibility that may be useful elsewhere:

https://six.readthedocs.io/#six.python_2_unicode_compatible

wskish commented 3 years ago

six is currently the # 2 pypi package so it seems like an okay dependency to introduce