-
Notifications
You must be signed in to change notification settings - Fork 89
/
types.py
85 lines (63 loc) · 2.06 KB
/
types.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
#!/usr/bin/env python3
#
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from typing import Any, Callable, Dict, List, NewType, Optional, Type, Union
from attr import Factory, dataclass
from fissix.fixer_base import BaseFix
from fissix.pgen2 import token
from fissix.pygram import python_symbols
from fissix.pytree import Leaf, Node
class Passthrough:
def __init__(self, target: Any) -> None:
self._target = target
def __getattr__(self, name: str) -> Any:
return getattr(self._target, name)
TOKEN = Passthrough(token)
SYMBOL = Passthrough(python_symbols)
SENTINEL = object()
START = object()
DROP = object()
STARS = {TOKEN.STAR, TOKEN.DOUBLESTAR}
ARG_END = {TOKEN.RPAR, TOKEN.COMMA}
ARG_LISTS = {SYMBOL.typedargslist, SYMBOL.arglist} # function def, function call
ARG_ELEMS = {
TOKEN.NAME, # single argument
SYMBOL.tname, # type annotated
SYMBOL.argument, # keyword argument
SYMBOL.star_expr, # vararg
} | STARS
LN = Union[Leaf, Node]
Stringish = Union[str, object]
Filename = NewType("Filename", str)
FilenameMatcher = Callable[[Filename], bool]
Capture = Dict[str, Any]
Callback = Callable[[Node, Capture, Filename], Optional[LN]]
Filter = Callable[[Node, Capture, Filename], bool]
Fixers = List[Type[BaseFix]]
Hunk = List[str]
Processor = Callable[[Filename, Hunk], bool]
@dataclass
class Transform:
selector: str = ""
kwargs: Dict[str, Any] = Factory(dict)
filters: List[Filter] = Factory(list)
callbacks: List[Callback] = Factory(list)
fixer: Optional[Type[BaseFix]] = None
class BowlerException(Exception):
def __init__(
self, message: str = "", *, filename: str = "", hunks: List[Hunk] = None
) -> None:
super().__init__(message)
self.filename = filename
self.hunks = hunks
class BowlerQuit(BowlerException):
pass
class IMRError(BowlerException):
pass
class RetryFile(BowlerException):
pass
class BadTransform(BowlerException):
pass