/
typing_pep604.py
186 lines (157 loc) · 5.48 KB
/
typing_pep604.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import annotations
import ast
import functools
import sys
from typing import Iterable
from tokenize_rt import NON_CODING_TOKENS
from tokenize_rt import Offset
from tokenize_rt import Token
from pyupgrade._ast_helpers import ast_to_offset
from pyupgrade._ast_helpers import is_name_attr
from pyupgrade._data import register
from pyupgrade._data import State
from pyupgrade._data import TokenFunc
from pyupgrade._token_helpers import CLOSING
from pyupgrade._token_helpers import find_closing_bracket
from pyupgrade._token_helpers import find_token
from pyupgrade._token_helpers import OPENING
def _fix_optional(i: int, tokens: list[Token]) -> None:
j = find_token(tokens, i, '[')
k = find_closing_bracket(tokens, j)
if tokens[j].line == tokens[k].line:
tokens[k] = Token('CODE', ' | None')
del tokens[i:j + 1]
else:
tokens[j] = tokens[j]._replace(src='(')
tokens[k] = tokens[k]._replace(src=')')
tokens[i:j] = [Token('CODE', 'None | ')]
def _fix_union(
i: int,
tokens: list[Token],
*,
arg_count: int,
) -> None:
depth = 1
parens_done = []
open_parens = []
commas = []
coding_depth = None
j = find_token(tokens, i, '[')
k = j + 1
while depth:
# it's possible our first coding token is a close paren
# so make sure this is separate from the if chain below
if (
tokens[k].name not in NON_CODING_TOKENS and
tokens[k].src != '(' and
coding_depth is None
):
if tokens[k].src == ')': # the coding token was an empty tuple
coding_depth = depth - 1
else:
coding_depth = depth
if tokens[k].src in OPENING:
if tokens[k].src == '(':
open_parens.append((depth, k))
depth += 1
elif tokens[k].src in CLOSING:
if tokens[k].src == ')':
paren_depth, open_paren = open_parens.pop()
parens_done.append((paren_depth, (open_paren, k)))
depth -= 1
elif tokens[k].src == ',':
commas.append((depth, k))
k += 1
k -= 1
assert coding_depth is not None
assert not open_parens, open_parens
comma_depth = min((depth for depth, _ in commas), default=sys.maxsize)
min_depth = min(comma_depth, coding_depth)
to_delete = [
paren
for depth, positions in parens_done
if depth < min_depth
for paren in positions
]
if comma_depth <= coding_depth:
comma_positions = [k for depth, k in commas if depth == comma_depth]
if len(comma_positions) == arg_count:
to_delete.append(comma_positions.pop())
else:
comma_positions = []
to_delete.sort()
if tokens[j].line == tokens[k].line:
del tokens[k]
for comma in comma_positions:
tokens[comma] = Token('CODE', ' |')
for paren in reversed(to_delete):
del tokens[paren]
del tokens[i:j + 1]
else:
tokens[j] = tokens[j]._replace(src='(')
tokens[k] = tokens[k]._replace(src=')')
for comma in comma_positions:
tokens[comma] = Token('CODE', ' |')
for paren in reversed(to_delete):
del tokens[paren]
del tokens[i:j]
def _supported_version(state: State) -> bool:
return (
state.in_annotation and (
state.settings.min_version >= (3, 10) or (
not state.settings.keep_runtime_typing and
'annotations' in state.from_imports['__future__']
)
)
)
def _any_arg_is_str(node_slice: ast.expr) -> bool:
return (
isinstance(node_slice, ast.Str) or (
isinstance(node_slice, ast.Tuple) and
any(isinstance(elt, ast.Str) for elt in node_slice.elts)
)
)
@register(ast.Subscript)
def visit_Subscript(
state: State,
node: ast.Subscript,
parent: ast.AST,
) -> Iterable[tuple[Offset, TokenFunc]]:
if not _supported_version(state):
return
# don't rewrite forward annotations (unless we know they will be dequoted)
if 'annotations' not in state.from_imports['__future__']:
if (
(sys.version_info >= (3, 9) and _any_arg_is_str(node.slice)) or
(
sys.version_info < (3, 9) and
isinstance(node.slice, ast.Index) and
_any_arg_is_str(node.slice.value)
)
):
return
if is_name_attr(
node.value,
state.from_imports,
('typing',),
('Optional',),
):
yield ast_to_offset(node), _fix_optional
elif is_name_attr(node.value, state.from_imports, ('typing',), ('Union',)):
if sys.version_info >= (3, 9): # pragma: >=3.9 cover
node_slice = node.slice
elif isinstance(node.slice, ast.Index): # pragma: <3.9 cover
node_slice: ast.AST = node.slice.value
else: # pragma: <3.9 cover
node_slice = node.slice # unexpected slice type
if isinstance(node_slice, ast.Slice): # not a valid annotation
return
if isinstance(node_slice, ast.Tuple):
if node_slice.elts:
arg_count = len(node_slice.elts)
else:
return # empty Union
else:
arg_count = 1
func = functools.partial(_fix_union, arg_count=arg_count)
yield ast_to_offset(node), func