PythonCharmers / python-future

Easy, clean, reliable Python 2/3 compatibility
http://python-future.org
MIT License
1.17k stars 291 forks source link

Futurize to add __future__ imports breaks pylint directives #588

Open amethystmarie opened 2 years ago

amethystmarie commented 2 years ago

When running the futurize fixer libfuturize.fixes.fix_unicode_literals_import on a file with a pylint directive, the import is added prior to the pylint directive, which causes them to be ignored. This is true of all directives that don't match the specific regexes of encoding, docstrings and shebang that the future_import method checks for.

https://github.com/PythonCharmers/python-future/blob/4657ad23de79a541ebcc7a06f1b9ad60172ad3c4/src/libfuturize/fixer_util.py#L230

Before:

# pylint: disable=import-error,invalid-name

import path
import system

Run: futurize --write --fix=libfuturize.fixes.fix_unicode_literals_import

After:

from __future__ import unicode_literals

# pylint: disable=import-error,invalid-name

import path
import system

If it skipped to the first non-commented, non-docstring line before inserting anything, it would fix this and any other directives that haven't been accounted for.