-
-
Notifications
You must be signed in to change notification settings - Fork 510
/
annotated_unions.py
186 lines (149 loc) 路 5.77 KB
/
annotated_unions.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
from typing import Optional, Sequence
import libcst as cst
import libcst.matchers as m
from libcst._nodes.expression import BaseExpression, Call # noqa: TCH002
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
from libcst.codemod.visitors import AddImportsVisitor, RemoveImportsVisitor
def _find_named_argument(args: Sequence[cst.Arg], name: str) -> cst.Arg | None:
return next(
(arg for arg in args if arg.keyword and arg.keyword.value == name),
None,
)
def _find_positional_argument(
args: Sequence[cst.Arg], search_index: int
) -> cst.Arg | None:
for index, arg in enumerate(args):
if index > search_index:
return None
if index == search_index and arg.keyword is None:
return arg
return None
class ConvertUnionToAnnotatedUnion(VisitorBasedCodemodCommand):
DESCRIPTION: str = (
"Converts strawberry.union(..., types=(...)) to "
"Annotated[Union[...], strawberry.union(...)]"
)
def __init__(
self,
context: CodemodContext,
use_pipe_syntax: bool = True,
use_typing_extensions: bool = False,
) -> None:
self._is_using_named_import = False
self.use_pipe_syntax = use_pipe_syntax
self.use_typing_extensions = use_typing_extensions
super().__init__(context)
def visit_Module(self, node: cst.Module) -> Optional[bool]:
self._is_using_named_import = False
return super().visit_Module(node)
@m.visit(
m.ImportFrom(
m.Name("strawberry"),
[
m.ZeroOrMore(),
m.ImportAlias(m.Name("union")),
m.ZeroOrMore(),
],
)
)
def visit_import_from(self, original_node: cst.ImportFrom) -> None:
self._is_using_named_import = True
@m.leave(
m.Call(
func=m.Attribute(value=m.Name("strawberry"), attr=m.Name("union"))
| m.Name("union")
)
)
def leave_union_call(
self, original_node: Call, updated_node: Call
) -> BaseExpression:
if not self._is_using_named_import and isinstance(original_node.func, cst.Name):
return original_node
types = _find_named_argument(original_node.args, "types")
union_name = _find_named_argument(original_node.args, "name")
if types is None:
types = _find_positional_argument(original_node.args, 1)
# this is probably a strawberry.union(name="...") so we skip the conversion
# as it is going to be used in the new way already 馃槉
if types is None:
return original_node
AddImportsVisitor.add_needed_import(
self.context,
"typing_extensions" if self.use_typing_extensions else "typing",
"Annotated",
)
RemoveImportsVisitor.remove_unused_import(self.context, "strawberry", "union")
if union_name is None:
union_name = _find_positional_argument(original_node.args, 0)
assert union_name
assert isinstance(types.value, (cst.Tuple, cst.List))
types = types.value.elements # type: ignore
union_name = union_name.value # type: ignore
description = _find_named_argument(original_node.args, "description")
directives = _find_named_argument(original_node.args, "directives")
if self.use_pipe_syntax:
union_node = self._create_union_node_with_pipe_syntax(types) # type: ignore
else:
AddImportsVisitor.add_needed_import(self.context, "typing", "Union")
union_node = cst.Subscript(
value=cst.Name(value="Union"),
slice=[
cst.SubscriptElement(slice=cst.Index(value=t.value))
for t in types # type: ignore
],
)
union_call_args = [
cst.Arg(
value=union_name, # type: ignore
keyword=cst.Name(value="name"),
equal=cst.AssignEqual(
whitespace_before=cst.SimpleWhitespace(""),
whitespace_after=cst.SimpleWhitespace(""),
),
)
]
additional_args = {"description": description, "directives": directives}
union_call_args.extend(
cst.Arg(
value=arg.value,
keyword=cst.Name(name),
equal=cst.AssignEqual(
whitespace_before=cst.SimpleWhitespace(""),
whitespace_after=cst.SimpleWhitespace(""),
),
)
for name, arg in additional_args.items()
if arg is not None
)
union_call_node = cst.Call(
func=cst.Attribute(
value=cst.Name(value="strawberry"),
attr=cst.Name(value="union"),
),
args=union_call_args,
)
return cst.Subscript(
value=cst.Name(value="Annotated"),
slice=[
cst.SubscriptElement(
slice=cst.Index(
value=union_node,
),
),
cst.SubscriptElement(
slice=cst.Index(
value=union_call_node,
),
),
],
)
@classmethod
def _create_union_node_with_pipe_syntax(
cls, types: Sequence[cst.BaseElement]
) -> cst.BaseExpression:
type_names = [t.value for t in types]
if not all(isinstance(t, cst.Name) for t in type_names):
raise ValueError("Only names are supported for now")
expression = " | ".join(name.value for name in type_names) # type: ignore
return cst.parse_expression(expression)