Coverage for src/lib3to6/fixers_builtin_rename.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of the lib3to6 project
2# https://github.com/mbarkhau/lib3to6
3#
4# Copyright (c) 2019-2021 Manuel Barkhau (mbarkhau@gmail.com) - MIT License
5# SPDX-License-Identifier: MIT
7import ast
9from . import common
10from . import fixer_base as fb
13class BuiltinsRenameFixerBase(fb.FixerBase):
15 new_name: str
16 old_name: str
18 def apply_fix(self, ctx: common.BuildContext, tree: ast.Module) -> ast.Module:
19 for node in ast.walk(tree):
20 is_access_to_builtin = (
21 isinstance(node, ast.Name)
22 and isinstance(node.ctx, ast.Load)
23 and node.id == self.new_name
24 )
26 if is_access_to_builtin:
27 self.required_imports.add(common.ImportDecl("builtins", None, "__builtin__"))
28 builtin_renmae_decl_str = f"""
29 {self.new_name} = getattr(builtins, '{self.old_name}', {self.new_name})
30 """
31 self.module_declarations.add(builtin_renmae_decl_str.strip())
33 return tree
36class XrangeToRangeFixer(BuiltinsRenameFixerBase):
38 version_info = common.VersionInfo(apply_until="2.7")
40 new_name = "range"
41 old_name = "xrange"
44class UnicodeToStrFixer(BuiltinsRenameFixerBase):
46 version_info = common.VersionInfo(apply_until="2.7")
48 new_name = "str"
49 old_name = "unicode"
52class UnichrToChrFixer(BuiltinsRenameFixerBase):
54 version_info = common.VersionInfo(apply_until="2.7")
56 new_name = "chr"
57 old_name = "unichr"
60class RawInputToInputFixer(BuiltinsRenameFixerBase):
62 version_info = common.VersionInfo(apply_until="2.7")
64 new_name = "input"
65 old_name = "raw_input"