#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 by , 11 years ago
comment:4 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 11 years ago
Resolution: | → invalid |
---|---|
Status: | assigned → 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 by , 11 years ago
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 by , 11 years ago
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.
https://github.com/django/django/pull/1543