-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
data.py
134 lines (101 loc) · 3.74 KB
/
data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from __future__ import annotations
import ast
import pkgutil
from collections import defaultdict
from typing import TYPE_CHECKING, Any, Callable, Iterable, List, Tuple, TypeVar
from tokenize_rt import Offset, Token
from django_upgrade import fixers
class Settings:
def __init__(self, target_version: tuple[int, int]) -> None:
self.target_version = target_version
class State:
def __init__(
self,
settings: Settings,
filename: str,
from_imports: dict[str, set[str]],
) -> None:
self.settings = settings
self.filename = filename
self.from_imports = from_imports
AST_T = TypeVar("AST_T", bound=ast.AST)
TokenFunc = Callable[[int, List[Token]], None]
ASTFunc = Callable[[State, AST_T, ast.AST], Iterable[Tuple[Offset, TokenFunc]]]
if TYPE_CHECKING: # pragma: no cover
from typing import Protocol
else:
Protocol = object
class ASTCallbackMapping(Protocol):
def __getitem__(self, tp: type[AST_T]) -> list[ASTFunc[AST_T]]: # pragma: no cover
...
def items(self) -> Iterable[tuple[Any, Any]]: # pragma: no cover
...
def visit(
tree: ast.Module,
settings: Settings,
filename: str,
) -> dict[Offset, list[TokenFunc]]:
ast_funcs = get_ast_funcs(settings.target_version)
initial_state = State(
settings=settings,
filename=filename,
from_imports=defaultdict(set),
)
nodes: list[tuple[State, ast.AST, ast.AST]] = [(initial_state, tree, tree)]
ret = defaultdict(list)
while nodes:
state, node, parent = nodes.pop()
for ast_func in ast_funcs[type(node)]:
for offset, token_func in ast_func(state, node, parent):
ret[offset].append(token_func)
if (
isinstance(node, ast.ImportFrom)
and node.level == 0
and (
node.module is not None
and (node.module.startswith("django.") or node.module == "django")
)
):
state.from_imports[node.module].update(
name.name
for name in node.names
if name.asname is None and name.name != "*"
)
for name in reversed(node._fields):
value = getattr(node, name)
next_state = state
if isinstance(value, ast.AST):
nodes.append((next_state, value, node))
elif isinstance(value, list):
for subvalue in reversed(value):
if isinstance(subvalue, ast.AST):
nodes.append((next_state, subvalue, node))
return ret
class Fixer:
def __init__(self, name: str, min_version: tuple[int, int]) -> None:
self.name = name
self.min_version = min_version
self.ast_funcs: ASTCallbackMapping = defaultdict(list)
FIXERS.append(self)
def register(
self, type_: type[AST_T]
) -> Callable[[ASTFunc[AST_T]], ASTFunc[AST_T]]:
def decorator(func: ASTFunc[AST_T]) -> ASTFunc[AST_T]:
self.ast_funcs[type_].append(func)
return func
return decorator
FIXERS: list[Fixer] = []
def _import_fixers() -> None:
# https://github.com/python/mypy/issues/1422
fixers_path: str = fixers.__path__ # type: ignore
mod_infos = pkgutil.walk_packages(fixers_path, f"{fixers.__name__}.")
for _, name, _ in mod_infos:
__import__(name, fromlist=["_trash"])
_import_fixers()
def get_ast_funcs(target_version: tuple[int, int]) -> ASTCallbackMapping:
ast_funcs: ASTCallbackMapping = defaultdict(list)
for fixer in FIXERS:
if target_version >= fixer.min_version:
for type_, type_funcs in fixer.ast_funcs.items():
ast_funcs[type_].extend(type_funcs)
return ast_funcs