You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
2to3 should remove from future_builtins import * #63310
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-10-20.22:46:00.334>created_at=<Date2013-09-28.14:52:33.751>labels= ['type-bug', 'expert-2to3']
title='2to3 should remove from future_builtins import *'updated_at=<Date2021-10-20.22:46:00.333>user='https://github.com/peterjc'
after running the zip fixer which respects the meaning of this command (issue 217). It does not, which results in an ImportError when trying to use the converted code under Python 3.
Similarly for lines like this after running the map fixer etc:
fromfuture_builtinsimportmap
Background:
Python 2.6 and 2.7 have an iterator-style zip function in future_builtins (and other things):
$ python2.6
Python 2.6.8 (unknown, Sep 28 2013, 12:09:28)
[GCC 4.6.3] on linux3
Type "help", "copyright", "credits" or "license" for more information.
>>> import future_builtins
>>> dir(future_builtins)
['__doc__', '__file__', '__name__', '__package__', 'ascii', 'filter', 'hex', 'map', 'oct', 'zip']
>>> quit()
$ python2.7
Python 2.7.1 (r271:86832, Dec 26 2010, 19:03:20)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import future_builtins
>>> dir(future_builtins)
['__doc__', '__file__', '__name__', '__package__', 'ascii', 'filter', 'hex', 'map', 'oct', 'zip']
>>> quit()
The future_builtins module does not exist under Python 3, in particular its zip and map functions do not exist.
$ python3.3
Python 3.3.2 (default, Sep 28 2013, 12:00:20)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import future_builtins
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'future_builtins'
Sample script using zip which works under Python 2.6 and 2.7,
$ more zip_2to3_bug.py
from future_builtins import zip
assert next(zip("abc", [1, 2, 3])) == ("a", 1)
print "Done"
$ python2.6 zip_2to3_bug.py
Done
$ python2.7 zip_2to3_bug.py
Done
$ more zip_2to3_bug.py
from future_builtins import zip
assert next(zip("abc", [1, 2, 3])) == ("a", 1)
print("Done")
This breaks:
$ python3.3 zip_2to3_bug.py
Traceback (most recent call last):
File "zip_2to3_bug.py", line 1, in <module>from future_builtins importzipImportError: No module named 'future_builtins'
Expected output should respect the fact I am using "from future_builtins import zip" therefore zip as an iterator already (this is working), and then remove the line "from future_builtins import zip":
$ more map_2to3_bug.py
from future_builtins import map
x = [-2, -1, 0, 1, 2, 3]
y = next(map(abs, x))
assert y == 2
print("Done")
This breaks:
$ python3.3 map_2to3_bug.py
Traceback (most recent call last):
File "map_2to3_bug.py", line 1, in <module>from future_builtins importmapImportError: No module named 'future_builtins'
Expected output should respect the fact I am using "from future_builtins import map" and therefore treat map according (this is working), and then remove the line "from future_builtins import map":
$ more zip_2to3_bug.py
x = [-2, -1, 0, 1, 2, 3]
y = next(map(abs, x))
assert y == 2
print("Done")
Thinking about this, perhaps the bug is that Python 3 doesn't have a future_builtins module? Consider:
$ python2.6
Python 2.6.8 (unknown, Sep 28 2013, 12:09:28)
[GCC 4.6.3] on linux3
Type "help", "copyright", "credits" or "license" for more information.
>>> from__future__import with_statement
>>> from__future__import print_function
>>> from future_builtins importmap, zip
>>> quit()
versus:
$ python3.3
Python 3.3.2 (default, Sep 28 2013, 12:00:20)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from__future__import with_statement
>>> from__future__import print_function
>>> from future_builtins importmap, zip
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'future_builtins'
>>> quit()
The expectation from the __future__ imports is that once a feature is part of Python, the import is a harmless no-op. You could expect the same from future_builtins as well.
marfire commented on Sep 22, 2014
marfiremannequin
Mannequin
More actions
I'm interested in working on this, but it's not clear to me if this has been accepted as a bug. I converted a large project recently and was shocked to find that all 100+ files were broken because of the needless "from future_builtins..." line. Perversely, this most hurts those who made the most effort to make their code forward-compatible with Python 3.
One complication is that you can't simply substitute a blank line, since that could produce invalid code if the import is inside a try/except block. (This doesn't affect "from __future__..." since that can only be at the start of a file.) Substituting a "pass" should work fine though. That would be a bit unsightly, but then again so is the blank line at the start of the file produced by removing "from __future__...". More important is to produce code that actually works under Python 3.
Another issue is that it's possible to simply "import future_builtins" and then use, for example, "future_builtins.zip(...)". So trying to fix that usage as well would be (I assume) a lot more complicated. Personally it seems fine with me to just fix the "from __future_builtins__..." usage.
zvyn commented on Jun 24, 2015
zvynmannequin
Mannequin
More actions
Here is a simple patch that would solve this issue.
The new fixer 'future_builtins' removes from future_builtins import foo statements if they aren't nested in other constructs (try-except, classes, ...) and replaces them with pass otherwise.
MojoVampire commented on Jun 25, 2015
MojoVampiremannequin
Mannequin
More actions
Shouldn't it also disable the relevant fixers for map, filter, zip, etc. as appropriate? Otherwise a whole bunch of calls will be wrapper in list() or the like to mimic a behavior the code never had in Py2 in the first place.
zvyn commented on Jun 25, 2015
zvynmannequin
Mannequin
More actions
The other fixers you mentioned are deactivated independently of this patch if an import from future_builtins exists. I gave it the same run_order as in the fix for __future__ imports which prevents 2to3 from deleting the calls before other fixers check for their existence.
Activity
peterjc commentedon Sep 28, 2013
The 2to3 script should remove lines like this:
after running the zip fixer which respects the meaning of this command (issue 217). It does not, which results in an ImportError when trying to use the converted code under Python 3.
Similarly for lines like this after running the map fixer etc:
Background:
Python 2.6 and 2.7 have an iterator-style zip function in future_builtins (and other things):
The future_builtins module does not exist under Python 3, in particular its zip and map functions do not exist.
$ python3.3 Python 3.3.2 (default, Sep 28 2013, 12:00:20) [GCC 4.6.3] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import future_builtins Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named 'future_builtins'Sample script using zip which works under Python 2.6 and 2.7,
Now let's use the 2to3 script (in place):
Here's the (broken) output:
This breaks:
Expected output should respect the fact I am using "from future_builtins import zip" therefore zip as an iterator already (this is working), and then remove the line "from future_builtins import zip":
Sample script using zip which works under Python 2.6 and 2.7,
$ more map_2to3_bug.py from future_builtins import map x = [-2, -1, 0, 1, 2, 3] y = next(map(abs, x)) assert y == 2 print "Done" $ python2.6 map_2to3_bug.py Done $ python2.7 map_2to3_bug.py DoneNow let's use the 2to3 script (in place):
Here's the (broken) output:
$ more map_2to3_bug.py from future_builtins import map x = [-2, -1, 0, 1, 2, 3] y = next(map(abs, x)) assert y == 2 print("Done")This breaks:
Expected output should respect the fact I am using "from future_builtins import map" and therefore treat map according (this is working), and then remove the line "from future_builtins import map":
$ more zip_2to3_bug.py x = [-2, -1, 0, 1, 2, 3] y = next(map(abs, x)) assert y == 2 print("Done")peterjc commentedon Sep 29, 2013
Thinking about this, perhaps the bug is that Python 3 doesn't have a future_builtins module? Consider:
versus:
The expectation from the __future__ imports is that once a feature is part of Python, the import is a harmless no-op. You could expect the same from future_builtins as well.
marfire commentedon Sep 22, 2014
I'm interested in working on this, but it's not clear to me if this has been accepted as a bug. I converted a large project recently and was shocked to find that all 100+ files were broken because of the needless "from future_builtins..." line. Perversely, this most hurts those who made the most effort to make their code forward-compatible with Python 3.
One complication is that you can't simply substitute a blank line, since that could produce invalid code if the import is inside a try/except block. (This doesn't affect "from __future__..." since that can only be at the start of a file.) Substituting a "pass" should work fine though. That would be a bit unsightly, but then again so is the blank line at the start of the file produced by removing "from __future__...". More important is to produce code that actually works under Python 3.
Another issue is that it's possible to simply "import future_builtins" and then use, for example, "future_builtins.zip(...)". So trying to fix that usage as well would be (I assume) a lot more complicated. Personally it seems fine with me to just fix the "from __future_builtins__..." usage.
zvyn commentedon Jun 24, 2015
Here is a simple patch that would solve this issue.
The new fixer 'future_builtins' removes
from future_builtins import foostatements if they aren't nested in other constructs (try-except, classes, ...) and replaces them withpassotherwise.MojoVampire commentedon Jun 25, 2015
Shouldn't it also disable the relevant fixers for map, filter, zip, etc. as appropriate? Otherwise a whole bunch of calls will be wrapper in list() or the like to mimic a behavior the code never had in Py2 in the first place.
zvyn commentedon Jun 25, 2015
The other fixers you mentioned are deactivated independently of this patch if an import from future_builtins exists. I gave it the same run_order as in the fix for __future__ imports which prevents 2to3 from deleting the calls before other fixers check for their existence.
MojoVampire commentedon Jun 25, 2015
Ah, my mistake. Apologies.