Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#16261 closed Bug (worksforme)

Model.__repr__() should never return unicode

Reported by: Luc Saffre Owned by: nobody
Component: Core (Other) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Unlike stated in ticket #13831,
and according to
this post on slashdot,
it seems that __repr__() should never return unicode in Python 2.

The current version does:

    def __repr__(self):
            u = unicode(self)
        except (UnicodeEncodeError, UnicodeDecodeError):
            u = '[Bad Unicode data]'
        return smart_str(u'<%s: %s>' % (self.__class__.__name__, u))

My suggestion would be to change this to:

    def __repr__(self):
            u = unicode(self)
        except (UnicodeEncodeError, UnicodeDecodeError):
            u = '[Bad Unicode data]'
        return '<%s: %r>' % (self.__class__.__name__, u)

Change History (2)

comment:1 Changed 5 years ago by Aymeric Augustin

Component: UncategorizedCore (Other)
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: worksforme
Status: newclosed
Type: UncategorizedBug

Since smart_str returns a bytestring version of its first argument, the current implementation of __repr__ always returns a bytestring.

So I don't understand why you suggest this change.

Also, it is backwards incompatible for people relying on a certain format of __repr__ in their tests.

On the general topic of __repr__ and unicode

The Python docs say that __repr__ should return a "string representation". Your argument is that "string representation" means "instance of str" and not "instance of basestr", but I'm not convinced.

This bug ( says that __repr__ can return unicode, which should be automatically converted as needed. Apparently, this feature was broken between 2.4 and 2.5.

Even if __repr__ actually returned unicode, I'm not sure we would change it.

comment:2 Changed 5 years ago by Aymeric Augustin

#13831 is not related: it discusses non-ascii characters in __repr__, not unicode.

Note: See TracTickets for help on using tickets.
Back to Top