question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't convert '__proxy__' object to str implicitly

See original GitHub issue

When using the Bootstrap3 inline form field template using lazily translated field labels in the current dev branch, an error is raised on:

{% crispy_field field 'placeholder' field.label %}

The error comes down to:

File "..../django-crispy-forms/crispy_forms/templatetags/crispy_forms_field.py" in render
  134.                     widget.attrs[attribute_name] += " " + template.Variable(attribute).resolve(context)

Exception Type: TypeError at ...
Exception Value: Can't convert '__proxy__' object to str implicitly

This simple str call resolves the issue:

widget.attrs[attribute_name] += " " + str(template.Variable(attribute).resolve(context))

Using Django 1.7c2 on Python 3.4

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
bayersglasseycommented, May 24, 2017

I’m not very familiar with the internals of Django’s lazy objects, but it seems to me like this could be fixed by defining __radd__ for the values returned by ugettext_lazy.

Minimal case of the error:

from django.utils.translation import ugettext_lazy as _
lazy_text = _("abc")
lazy_text + "123"  # works fine
"123" + lazy_text  # TypeError: Can't convert '__proxy__' object to str implicitly

…So if lazy_text.__radd__ was defined, I think this bug would be solved.

I did a quick test to make sure defining __radd__ lets us add to strings:

class A:
    def __radd__(self, x):
        return x + " A"
    def __add__(self, x):
        return "A " + x

a = A()
a+"123"  # "A 123"
"123"+a  # "123 A"   ...success!
0reactions
carltongibsoncommented, May 24, 2017

@bayersglassey Nice example. This might be worth suggesting as a fix on Django.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python gettext error: Can't convert '__proxy__' object to str ...
This can be fixed by wrapping the second call to ugettext_lazy() in str() (i.e. the code becomes str( _('link!') ) . Doing this...
Read more >
TypeError: Can't convert 'int' object to str implicitly
This error message Can't convert 'int' object to str implicitly is clear, when concatenating strings with integers - you can't directly stick together...
Read more >
Proxy objects, symbols, and string conversions - Matt Zeunert
Doing an explicit conversion with p. toString() + " world!" works fine! So what happens when Chrome tries to convert an object to...
Read more >
proxies: stratifying toString, valueOf - ES Discuss
Allowing proxies to trap implicit calls to base-level methods but not explicit calls seems weird. If a Dict should not pollute its pseudo-property...
Read more >
Python error TypeError Can t convert int object to str implicitly
This error message Can't convert 'int' object to str implicitly is clear, when concatenating strings with integers - you can't directly ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found