Fixers¶
Fixers come in two types: Default and Opt-in. Default fixers should not break code except for corner cases, and are idempotent (applying them more than once to given source code will make no changes after the first application). Opt-in fixers are allowed to break these rules.
Python 2 code from Python 2.6 and older will be upgraded to code that is compatible with Python 2.6, 2.7, and Python 3.
If code is using a feature unique to Python 2.7, it will not be downgraded to
work with Python 2.6. For example, dict.viewitems() usage will not be
removed to make the code compatible with Python 2.6.
Some fixers rely on the latest release of the six project to work
(see Fixers requiring six).
If you wish to turn off these fixers to avoid an external dependency on six,
then use the --no-six flag.
Fixers use the API defined by fissix. For details of how this works, and how to
implement your own fixers, see Creating a fixer, at python3porting.com.
python -m modernize will try to load fixers whose full dotted-path is specified
as a -f argument, but will fail if they are not found. By default, fixers
will not be found in the current directory; use --fixers-here to make
python -m modernize look for them there, or see the Python tutorial on
modules (in particular,
the parts on the search path
and packages)
for more info on how Python finds modules.
Default¶
A default fixer will be enabled when:
Either no
-f/--fixoptions are used, or-f default/--fix=defaultis used, or the fixer is listed explicitly in an-f/--fixoption; andThe fixer is not listed in an
-x/--nofixoption; andFor fixers that are dependent on the six project,
--no-sixis not specified (see Fixers requiring six).
The -x/--nofix and --no-six options always override fixers specified
using -f/--fix. The --six-unicode and --future-unicode options
also disable fixers that are not applicable for those options.
Fixers requiring six¶
The six project provides the six module which contains various tidbits in
helping to support Python 2/3 code. All six-related fixers assume the latest
version of six is installed.
-
basestring¶ Replaces all references to
basestring()withsix.string_types.New in version 0.4.
-
dict_six¶ Fixes various methods on the
dicttype for getting all keys, values, or items. E.g.:x.values() x.itervalues() x.viewvalues()
becomes:
list(x.values()) six.itervalues(x) six.viewvalues(x)
Care is taken to only call
list()when not in an iterating context (e.g. not the iterable for aforloop).
-
filter¶ When a call to
filteris discovered,from six.moves import filteris added to the module. Wrapping the use in a call tolist()is done when necessary.
-
imports_six¶ Uses
six.movesto fix various renamed modules, e.g.:import ConfigParser ConfigParser.ConfigParser()
becomes:
import six.moves.configparser six.moves.configparser.ConfigParser()
The modules in Python 2 whose renaming in Python 3 is supported are:
__builtin___winregBaseHTTPServerCGIHTTPServerConfigParsercopy_regCookiecookielibcPickleDialogdummy_threadFileDialoggdbmhtmlentitydefsHTMLParserhttplibQueuereprrobotparserScrolledTextSimpleDialogSimpleHTTPServerSimpleXMLRPCServerSocketServerthreadTixtkColorChoosertkCommonDialogTkconstantsTkdndtkFileDialogtkFontTkintertkMessageBoxtkSimpleDialogttkxmlrpclib
New in version 0.4.
-
input_six¶ Changes:
input(x) raw_input(x)
to:
from six.moves import input eval(input(x)) input(x)
New in version 0.4.
-
int_long_tuple¶ Changes
(int, long)or(long, int)tosix.integer_types.New in version 0.4.
-
map¶ If a call to
mapis discovered,from six.moves import mapis added to the module. Wrapping the use in a call tolist()is done when necessary.
-
metaclass¶ Changes:
class Foo: __metaclass__ = Meta
to:
import six class Foo(six.with_metaclass(Meta)): pass
See also
-
raise_six¶ Changes
raise E, V, Ttosix.reraise(E, V, T).
-
unicode_type¶ Changes all reference of
unicodetosix.text_type.
-
urllib_six¶ Changes:
from urllib import quote_plus quote_plus('hello world')
to:
from six.moves.urllib.parse import quote_plus quote_plus('hello world')
-
xrange_six¶ Changes:
w = xrange(x) y = range(z)
to:
from six.moves import range w = range(x) y = list(range(z))
Care is taken not to call
list()whenrange()is used in an iterating context.
fissix fixers¶
Some fixers from fissix in Python’s standard library are run by default unmodified as their transformations are Python 2 compatible.
Fixers with no dependencies¶
-
import¶ Changes implicit relative imports to explicit relative imports and adds
from __future__ import absolute_import.New in version 0.4.
-
next¶ Changes all method calls from
x.next()tonext(x).
-
print¶ Changes all usage of the
printstatement to use theprint()function and addsfrom __future__ import print_function.
-
raise¶ Changes comma-based
raisestatements from:raise E, V raise (((E, E1), E2), E3), V
to:
raise E(V) raise E(V)
Opt-in¶
To specify an opt-in fixer while also running all the default fixers, make sure
to specify the -f default or --fix=default option, e.g.:
python -m modernize -f default -f libmodernize.fixes.fix_open
-
classic_division¶ When a use of the division operator –
/– is found, addfrom __future__ import divisionand change the operator to//. Iffrom __future__ import divisionis already present, this fixer is skipped.This is intended for use in programs where
/is conventionally only used for integer division, or where it is intended to do a manual pass after runningpython -m modernizeto look for cases that should not have been changed to//. The results of division on non-integers may differ after running this fixer: for example,3.5 / 2 == 1.75, but3.5 // 2 == 1.0.Some objects may override the
__div__method for a use other than division, and thus would break when changed to use a__floordiv__method instead.This fixer is opt-in because it may change the meaning of code as described above.
New in version 1.0.