Python-future: Fixer to remove __future__/future imports from futurized code

Created on 28 May 2019  ·  5Comments  ·  Source: PythonCharmers/python-future

I was looking for this feature, but didn't see it in the documentation so I'm opening a feature request.

I'd love to see a fixer which takes Python 2/3 compatible code and removes the Python 2 support as best as possible.

This would:

  • Remove unnecessary __future__ imports (print_function for example)
  • Remove from future import standard_library and standard_library.install_aliases() lines
  • Remove from builtins import * lines
  • Remove from future.utils import python_2_unicode_compatible

If such a fixer already exists, I'd love to know about it.

A number of my clients are using future to upgrade their code to Python 3 and then dropping Python 2 support shortly afterward and such a fixer would be very helpful for them.

futurize script help wanted unscheduled wontfix / cantfix

Most helpful comment

I made some transformations myself and bundled them up into a script. It's currently available here: https://github.com/purplediane/drop2

All 5 comments

If it exists, then it's not documented well. I had to manually remove all that. Use of a fixture that auto-ran maybe with a flag such as -3 to mean only refactor for 3 compliance and ignore python2 compatibility would be great.

I made some transformations myself and bundled them up into a script. It's currently available here: https://github.com/purplediane/drop2

A related tool, with some overlap (namely __future__ imports):

asottile/pyupgrade: A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language.

Other notable things:

  • Removes u prefix
  • Removes (object) from class definitions
  • Updates super() calls
  • Updates mock imports

That said, I was able to use sed for standard_library:

sed -i '' '/.*standard_library.*/d' **/*.py
-from future import standard_library
-standard_library.install_aliases()
 import json
 import logging
 import urllib.parse

I'm planning to do the same for:

  • .*python_2_unicode_compatible.*
  • .*implements_iterator.*
  • from builtins.*

I'm already aware of pyupgrade and have used it. It is very helpful.

pyupgrade (plus the pre-commit hook) is a great tool for this purpose. Can we close this FR since the alternative is available?

Was this page helpful?
0 / 5 - 0 ratings