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
8import typing as typ
10from . import common
12# NOTE (mb 2018-06-24): Version info pulled from:
13# https://docs.python.org/3/library/__future__.html
16class FixerBase:
18 version_info : common.VersionInfo
19 required_imports : typ.Set[common.ImportDecl]
20 module_declarations: typ.Set[str]
22 def __init__(self) -> None:
23 self.required_imports = set()
24 self.module_declarations = set()
26 def __call__(self, ctx: common.BuildContext, tree: ast.Module) -> ast.Module:
27 try:
28 return self.apply_fix(ctx, tree)
29 except common.FixerError as ex:
30 if ex.parent is None:
31 ex.parent = tree
32 if ex.filepath is None:
33 ex.filepath = ctx.filepath
34 raise
36 def apply_fix(self, ctx: common.BuildContext, tree: ast.Module) -> ast.Module:
37 raise NotImplementedError()
40class TransformerFixerBase(FixerBase, ast.NodeTransformer):
41 def apply_fix(self, ctx: common.BuildContext, tree: ast.Module) -> ast.Module:
42 new_tree = self.visit(tree)
43 return typ.cast(ast.Module, new_tree)