I am converting code from python2 to python3 for newstyle classes using future. My project is in Django 1.11
I have a class in forms.py as:
class Address:
...rest of code...
class AddressForm(Address, forms.ModelForm):
...rest of code...
in Python 2
which is converted to :
from buitlins import object
class Address(object):
...rest of code...
class AddressForm(Address, forms.ModelForm):
...rest of code...
in Python 3
I have a selenium test that fails when this Form is invoked after it is converted to Python3 with the following error:
File "<path_to_venv>/local/lib/python2.7/site-packages/django/utils/six.py", line 842, in <lambda>
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
File "<path_to_venv>/local/lib/python2.7/site-packages/future/types/newobject.py", line 78, in __unicode__
s = type(self).__str__(self)
RuntimeError: maximum recursion depth exceeded
However, when I remove the import from buitlins import object the test passes.
But as I have added a future check, I get a future difference error & thus every class has to be converted to newstyle. I want it to work in both Python2 and Python3.
Is there a way this module builtins module import can affect just one class and not others in the forms.py file. Or is there some other method to handle this?
The problem you're running up against seems to be from two different Python 2 modernization tools fighting. You seem to be using the
python_2_unicode_compatibledecorator fromdjango.utils.sixand inheriting from
newobject, which has this__unicode__methodAnd because the two have slightly different strategies for providing both
__unicode__and__str__methods, they ed up calling each other infinitely, which leads to your recursion error.The module that provides builtins.object provides its own
python_2_unicode_compatibledecorator. Have you tried using that over the one fromdjango.utils.six?