Opened 22 months ago

Closed 22 months ago

Last modified 22 months ago

#21019 closed Bug (invalid)

force_text fails on class object with __unicode__ instance method

Reported by: drtyrsa Owned by: drtyrsa
Component: Utilities Version: 1.5
Severity: Normal Keywords: force_text
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

In [10]: class A(object):
            def __unicode__(self):
                pass
   ....:     

In [11]: force_text(A)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-f9169705858c> in <module>()
----> 1 force_text(A)

.../django/utils/encoding.pyc in force_text(s, encoding, strings_only, errors)
     97         if not isinstance(s, six.string_types):
     98             if hasattr(s, '__unicode__'):
---> 99                 s = s.__unicode__()
    100             else:
    101                 if six.PY3:

TypeError: unbound method __unicode__() must be called with A instance as first argument (got nothing instead)

Change History (7)

comment:1 Changed 22 months ago by drtyrsa

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 22 months ago by drtyrsa

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:3 Changed 22 months ago by drtyrsa

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:4 Changed 22 months ago by drtyrsa

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to drtyrsa
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:5 Changed 22 months ago by claudep

  • Resolution set to invalid
  • Status changed from assigned to closed

I think that calling force_text on the class itself is not a valid use case, unless you can demonstrate the opposite.

comment:6 Changed 22 months ago by drtyrsa

What are valid use cases for smart_text then? As I understand it takes some object and returns its str (unicode) representation. Why can not this object be class itself?

I am using it to log the args of the function (some args can be models classes). Isn't it valid use case?

comment:7 Changed 22 months ago by claudep

Currently, force_text/smart_text are meant to receive instances, not classes. I'd say that for your specific use case, you may test your argument before passing it to force_text, for example with the inspect.isclass method.

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