Skip to content

Commit

Permalink
Merge pull request #145 from rowillia/add_unichr_fixer
Browse files Browse the repository at this point in the history
Add a fixer for instances of `unichr`
  • Loading branch information
takluyver committed Oct 31, 2016
2 parents c00f480 + 54c9983 commit 94ae50b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Unreleased

* Added the opt-in classic_division fixer.
* Updated the ``dict_six`` fixer to support ``six.viewitems()`` and friends.
* New fixer for ``unichr``, changed to ``six.unichr``.
* Documentation corrections.


Expand Down
5 changes: 5 additions & 0 deletions docs/fixers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ version of ``six`` is installed.
from six.moves.urllib.parse import quote_plus
quote_plus('hello world')
.. 2to3fixer:: unichr
Changes all reference of :func:`unichr <python2:unichr>` to
:data:`six.unichr`.
.. 2to3fixer:: xrange_six
Changes::
Expand Down
1 change: 1 addition & 0 deletions libmodernize/fixes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'libmodernize.fixes.fix_unicode',
'libmodernize.fixes.fix_unicode_type',
'libmodernize.fixes.fix_urllib_six',
'libmodernize.fixes.fix_unichr',
'libmodernize.fixes.fix_xrange_six',
'libmodernize.fixes.fix_zip',
])
Expand Down
19 changes: 19 additions & 0 deletions libmodernize/fixes/fix_unichr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import absolute_import

from lib2to3 import fixer_base
from lib2to3.fixer_util import is_probably_builtin

import libmodernize


class FixUnichr(fixer_base.ConditionalFix):
BM_compatible = True

skip_on = 'six.moves.unichr'
PATTERN = """'unichr'"""

def transform(self, node, results):
if self.should_skip(node):
return
if is_probably_builtin(node):
libmodernize.touch_import(u'six', u'unichr', node)
38 changes: 38 additions & 0 deletions tests/test_fix_unichr_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from __future__ import absolute_import

from utils import check_on_input


UNICHR_METHOD_REF = ("""\
converter = unichr
""", """\
from __future__ import absolute_import
from six import unichr
converter = unichr
""")

UNICHR_METHOD_CALL = ("""\
unichr(42)
""", """\
from __future__ import absolute_import
from six import unichr
unichr(42)
""")

UNICHR_USER_CALL = ("""\
foobar.unichr(42)
""", """\
foobar.unichr(42)
""")


def test_unichr_method_ref():
check_on_input(*UNICHR_METHOD_REF)


def test_unichr_method_call():
check_on_input(*UNICHR_METHOD_CALL)


def test_unichr_user_call():
check_on_input(*UNICHR_USER_CALL)

0 comments on commit 94ae50b

Please sign in to comment.