votes up 2

Expected bytes, unicode, or None; got %r

Package:
tornado
github stars 20166
Exception Class:
TypeError

Raise code

""" 

    If the argument is already a byte string or None, it is returned unchanged.
    Otherwise it must be a unicode string and is encoded as utf8.
    """
    if isinstance(value, _UTF8_TYPES):
        return value
    if not isinstance(value, unicode_type):
        raise TypeError("Expected bytes, unicode, or None; got %r" % type(value))
    return value.encode("utf-8")


_TO_UNICODE_TYPES = (unicode_type, type(None))


@typing.overload

Ways to fix

votes up 1 votes down

Summary: This function expects to receive either a string, bytes, or None. If anything else is provided as an argument, it will throw this exception.

Code to Reproduce the Error (WRONG):

from tornado.escape import utf8
r = utf8(3)

Working Version (Fixed):

from tornado.escape import utf8
r = utf8('Hello')
Jul 08, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix