How to tell futurize (python-future) to not touch a specific line?
I'm trying to convert Python 2.7 codebase to 3.x using python-future
. Sometimes I need to keep a small bit of Python-2-specific code intact, until the migration is complete. For example,
import sys
def f(x):
if sys.version_info[0] == 2:
if type(x) == unicode: # futurize: please don't change this line.
call_some_py2_specific_logic()
the_rest_of_logic()
However, if I run this code through futurize
, it will replace unicode
by str
. Is there any way to mark the line so that futurize
"understands" the comment I wrote above?
Not yet (as of January 2022), although running futurize with the -x libfuturize.fixers.fix_unicode_keep_u
parameter will skip the unicode replacement fixer, which might be useful if you are running this as part of a CI/CD pipeline to prevent regressions.
The other option is to wrap the output in a script that checks for the presence of your magic comment in the output of futurize, and ignores it if present (which you might want to write up into an answer, since you wrote one!)