-
Notifications
You must be signed in to change notification settings - Fork 8
/
no_implicit_optional.py
282 lines (230 loc) · 11.4 KB
/
no_implicit_optional.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import argparse
import os
import re
import sys
from typing import Tuple
import libcst as cst
from libcst.codemod import (
CodemodContext,
TransformSuccess,
VisitorBasedCodemodCommand,
gather_files,
parallel_exec_transform_with_prettyprint,
transform_module,
)
from libcst.codemod.visitors import AddImportsVisitor
def is_typing_optional(expr: cst.BaseExpression) -> bool:
if isinstance(expr, cst.Name):
return expr.value == "Optional"
if isinstance(expr, cst.Attribute):
return (
expr.attr.value == "Optional"
and isinstance(expr.value, cst.Name)
and expr.value.value in ("typing", "t", "T")
)
if isinstance(expr, cst.Subscript):
return is_typing_optional(expr.value)
return False
def is_typing_union_with_none(expr: cst.BaseExpression) -> bool:
if isinstance(expr, cst.Subscript) and expr.slice:
has_union_base = (isinstance(expr.value, cst.Name) and expr.value.value == "Union") or (
isinstance(expr.value, cst.Attribute)
and expr.value.attr.value == "Union"
and isinstance(expr.value.value, cst.Name)
and expr.value.value.value in ("typing", "t", "T")
)
if has_union_base:
return any(
isinstance(el.slice, cst.Index)
and isinstance(el.slice.value, cst.Name)
and el.slice.value.value == "None"
for el in expr.slice
)
return False
def is_pep_604_union_with_none(expr: cst.BaseExpression) -> bool:
if not isinstance(expr, cst.BinaryOperation) or not isinstance(expr.operator, cst.BitOr):
return False
return (
(isinstance(expr.right, cst.Name) and expr.right.value == "None")
or (isinstance(expr.left, cst.Name) and expr.left.value == "None")
or (is_pep_604_union_with_none(expr.left) or is_pep_604_union_with_none(expr.right))
)
def is_literal_with_none(expr: cst.BaseExpression) -> bool:
if isinstance(expr, cst.Name) and expr.value == "None":
return True
if isinstance(expr, cst.Subscript) and expr.slice:
has_literal_base = (isinstance(expr.value, cst.Name) and expr.value.value == "Literal") or (
isinstance(expr.value, cst.Attribute)
and expr.value.attr.value == "Literal"
and isinstance(expr.value.value, cst.Name)
and expr.value.value.value in ("typing", "t", "T")
)
if has_literal_base:
return any(
isinstance(el.slice, cst.Index)
and isinstance(el.slice.value, cst.Name)
and el.slice.value.value == "None"
for el in expr.slice
)
return False
def is_optional_sounding_alias(expr: cst.BaseExpression) -> bool:
return isinstance(expr, cst.Name) and bool(
expr.value.endswith("Opt") or re.match(r"_?(Opt[A-Z]|Optional|None[A-Z])", expr.value)
)
def is_typing_annotated(expr: cst.BaseExpression) -> bool:
if isinstance(expr, cst.Name):
return expr.value == "Annotated"
if isinstance(expr, cst.Attribute):
return (
expr.attr.value == "Annotated"
and isinstance(expr.value, cst.Name)
and expr.value.value in ("typing", "t", "T")
)
if isinstance(expr, cst.Subscript):
return is_typing_annotated(expr.value)
return False
def type_hint_explicitly_allows_none_with_expr(
expr: cst.BaseExpression,
) -> Tuple[bool, cst.BaseExpression]:
if is_typing_annotated(expr):
assert isinstance(expr, cst.Subscript)
assert isinstance(expr.slice[0].slice, cst.Index)
return type_hint_explicitly_allows_none_with_expr(expr.slice[0].slice.value)
return (
is_typing_optional(expr)
or is_typing_union_with_none(expr)
or is_pep_604_union_with_none(expr)
or is_literal_with_none(expr)
or is_optional_sounding_alias(expr)
), expr
def type_hint_explicitly_allows_none(expr: cst.BaseExpression) -> bool:
allows_none, _ = type_hint_explicitly_allows_none_with_expr(expr)
return allows_none
class NoImplicitOptionalCommand(VisitorBasedCodemodCommand):
def __init__(self, context: CodemodContext, use_union_or: bool) -> None:
super().__init__(context)
self.use_union_or = use_union_or
def leave_Param(self, original_node: cst.Param, updated_node: cst.Param) -> cst.Param:
if (
original_node.annotation is not None
and original_node.default is not None
and isinstance(original_node.default, cst.Name)
and original_node.default.value == "None"
):
top_level_expr = original_node.annotation.annotation
allows_none, expr = type_hint_explicitly_allows_none_with_expr(top_level_expr)
if not allows_none:
new_expr: cst.BaseExpression
if self.use_union_or:
new_expr = cst.BinaryOperation(
left=expr, operator=cst.BitOr(), right=cst.Name(value="None")
)
else:
new_expr = cst.Subscript(
value=cst.Name(value="Optional"),
slice=[cst.SubscriptElement(cst.Index(value=expr))],
)
AddImportsVisitor.add_needed_import(self.context, "typing", "Optional")
if expr is not top_level_expr: # happens with Annotated
new_expr = top_level_expr.deep_replace(expr, new_expr)
new_annotation = cst.Annotation(new_expr)
return updated_node.with_changes(annotation=new_annotation)
return updated_node
def main() -> int:
test()
parser = argparse.ArgumentParser()
parser.add_argument(
"--use-union-or",
action="store_true",
help="Whether to use PEP 604 `X | None` syntax instead of `Optional[X]`",
)
parser.add_argument("path", nargs="+")
args = parser.parse_args()
bases = list(map(os.path.abspath, args.path))
root = os.path.commonpath(bases)
root = os.path.dirname(root) if os.path.isfile(root) else root
files = gather_files(bases, include_stubs=True)
try:
result = parallel_exec_transform_with_prettyprint(
NoImplicitOptionalCommand(CodemodContext(), use_union_or=args.use_union_or),
files,
repo_root=root,
)
except KeyboardInterrupt:
print("Interrupted!", file=sys.stderr)
return 2
print(
f"Finished codemodding {result.successes + result.skips + result.failures} files!",
file=sys.stderr,
)
print(f" - Transformed {result.successes} files successfully.", file=sys.stderr)
print(f" - Skipped {result.skips} files.", file=sys.stderr)
print(f" - Failed to codemod {result.failures} files.", file=sys.stderr)
print(f" - {result.warnings} warnings were generated.", file=sys.stderr)
return 1 if result.failures > 0 else 0
def test() -> None:
assert not type_hint_explicitly_allows_none(cst.parse_expression("int"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("str"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("List[str]"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("typing.Tuple[str]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("Optional"))
assert type_hint_explicitly_allows_none(cst.parse_expression("Optional[int]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("t.Optional[int]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("T.Optional[int]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("typing.Optional[int]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("Union[None, int]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("Union[int, None]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("t.Union[int, None]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("T.Union[int, None]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("typing.Union[int, None]"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("Union"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("Union[int, str]"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("t.Union[int, str]"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("T.Union[int, str]"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("typing.Union[int, str]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("int | None"))
assert type_hint_explicitly_allows_none(cst.parse_expression("None | int"))
assert type_hint_explicitly_allows_none(
cst.parse_expression("int | str | None | float | bytes")
)
assert not type_hint_explicitly_allows_none(cst.parse_expression("int | str"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("int | str | float | bytes"))
assert type_hint_explicitly_allows_none(cst.parse_expression("None"))
assert type_hint_explicitly_allows_none(cst.parse_expression("Literal[1, 2, None, 3]"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("Literal[1, 2]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("OptWhatever"))
assert type_hint_explicitly_allows_none(cst.parse_expression("_OptWhatever"))
assert type_hint_explicitly_allows_none(cst.parse_expression("WhateverOpt"))
assert not type_hint_explicitly_allows_none(cst.parse_expression("Annotated[int, ...]"))
assert type_hint_explicitly_allows_none(cst.parse_expression("Annotated[Optional[int], ...]"))
assert type_hint_explicitly_allows_none(
cst.parse_expression("typing.Annotated[Optional[int], ...]")
)
assert not type_hint_explicitly_allows_none(cst.parse_expression("t.Annotated[int, ...]"))
cmd = NoImplicitOptionalCommand(CodemodContext(), use_union_or=False)
result = transform_module(cmd, "def foo(x: int = None): pass")
assert isinstance(result, TransformSuccess)
assert result.code == "from typing import Optional\n\ndef foo(x: Optional[int] = None): pass"
result = transform_module(cmd, "def foo(x: list[int] = None): pass")
assert isinstance(result, TransformSuccess)
assert (
result.code == "from typing import Optional\n\ndef foo(x: Optional[list[int]] = None): pass"
)
result = transform_module(cmd, "def foo(x: Annotated[int, str.isdigit] = None): pass")
assert isinstance(result, TransformSuccess)
assert (
result.code
== "from typing import Optional\n\ndef foo(x: Annotated[Optional[int], str.isdigit] = None): pass"
)
cmd = NoImplicitOptionalCommand(CodemodContext(), use_union_or=True)
result = transform_module(cmd, "def foo(x: int = None): pass")
assert isinstance(result, TransformSuccess)
assert result.code == "def foo(x: int | None = None): pass"
result = transform_module(cmd, "def foo(x: list[int] = None): pass")
assert isinstance(result, TransformSuccess)
assert result.code == "def foo(x: list[int] | None = None): pass"
result = transform_module(cmd, "def foo(x: Annotated[int, str.isdigit] = None): pass")
assert isinstance(result, TransformSuccess)
assert result.code == "def foo(x: Annotated[int | None, str.isdigit] = None): pass"
if __name__ == "__main__":
sys.exit(main())