Debian Codegraph @

test-data/unit/exportjson.test

raw
1 -- Test cases for exporting mypy cache files to JSON (mypy.exportjson).
2 --
3 -- The tool is maintained on a best effort basis so we don't attempt to have
4 -- full test coverage.
5 --
6 -- Some tests only ensure that *some* JSON is generated successfully. These
7 -- have <not checked> as [out].
8
9 [case testExportVar]
10 x = 0
11 [out]
12 {
13 ".class": "MypyFile",
14 "_fullname": "main",
15 "names": {
16 ".class": "SymbolTable",
17 "x": {
18 ".class": "SymbolTableNode",
19 "kind": "Gdef",
20 "node": {
21 ".class": "Var",
22 "name": "x",
23 "fullname": "main.x",
24 "type": "builtins.int",
25 "setter_type": null,
26 "flags": [
27 "is_ready",
28 "is_inferred",
29 "has_explicit_value"
30 ]
31 }
32 }
33 },
34 "is_stub": false,
35 "path": ...,
36 "is_partial_stub_package": false,
37 "future_import_flags": []
38 }
39
40 [case testExportClass]
41 class C:
42 x: int
43 [out]
44 {
45 ".class": "MypyFile",
46 "_fullname": "main",
47 "names": {
48 ".class": "SymbolTable",
49 "C": {
50 ".class": "SymbolTableNode",
51 "kind": "Gdef",
52 "node": {
53 ".class": "TypeInfo",
54 "module_name": "main",
55 "fullname": "main.C",
56 "names": {
57 ".class": "SymbolTable",
58 "x": {
59 ".class": "SymbolTableNode",
60 "kind": "Mdef",
61 "node": {
62 ".class": "Var",
63 "name": "x",
64 "fullname": "main.C.x",
65 "type": "builtins.int",
66 "setter_type": null,
67 "flags": [
68 "is_initialized_in_class",
69 "is_ready"
70 ]
71 }
72 }
73 },
74 "defn": {
75 ".class": "ClassDef",
76 "name": "C",
77 "fullname": "main.C",
78 "type_vars": []
79 },
80 "abstract_attributes": [],
81 "type_vars": [],
82 "has_param_spec_type": false,
83 "bases": [
84 "builtins.object"
85 ],
86 "mro": [
87 "main.C",
88 "builtins.object"
89 ],
90 "_promote": [],
91 "alt_promote": null,
92 "declared_metaclass": null,
93 "metaclass_type": null,
94 "tuple_type": null,
95 "typeddict_type": null,
96 "flags": [],
97 "metadata": {},
98 "slots": null,
99 "deletable_attributes": [],
100 "self_type": null,
101 "dataclass_transform_spec": null,
102 "deprecated": null
103 }
104 }
105 },
106 "is_stub": false,
107 "path": ...,
108 "is_partial_stub_package": false,
109 "future_import_flags": []
110 }
111
112 [case testExportCrossRef]
113 from typing import Any
114 [out]
115 {
116 ".class": "MypyFile",
117 "_fullname": "main",
118 "names": {
119 ".class": "SymbolTable",
120 "Any": {
121 ".class": "SymbolTableNode",
122 "kind": "Gdef",
123 "cross_ref": "typing.Any"
124 }
125 },
126 "is_stub": false,
127 "path": ...,
128 "is_partial_stub_package": false,
129 "future_import_flags": []
130 }
131
132 [case testExportFuncDef]
133 def foo(a: int) -> None: ...
134 [out]
135 {
136 ".class": "MypyFile",
137 "_fullname": "main",
138 "names": {
139 ".class": "SymbolTable",
140 "foo": {
141 ".class": "SymbolTableNode",
142 "kind": "Gdef",
143 "node": {
144 ".class": "FuncDef",
145 "name": "foo",
146 "fullname": "main.foo",
147 "arg_names": [
148 "a"
149 ],
150 "arg_kinds": [
151 0
152 ],
153 "type": {
154 ".class": "CallableType",
155 "arg_types": [
156 "builtins.int"
157 ],
158 "arg_kinds": [
159 0
160 ],
161 "arg_names": [
162 "a"
163 ],
164 "ret_type": {
165 ".class": "NoneType"
166 },
167 "fallback": "builtins.function",
168 "name": "foo",
169 "variables": [],
170 "is_ellipsis_args": false,
171 "implicit": false,
172 "is_bound": false,
173 "type_guard": null,
174 "type_is": null,
175 "from_concatenate": false,
176 "imprecise_arg_kinds": false,
177 "unpack_kwargs": false
178 },
179 "flags": [],
180 "abstract_status": 0,
181 "dataclass_transform_spec": null,
182 "deprecated": null,
183 "original_first_arg": "a"
184 }
185 }
186 },
187 "is_stub": false,
188 "path": ...,
189 "is_partial_stub_package": false,
190 "future_import_flags": []
191 }
192
193 [case testExportDifferentTypes]
194 from __future__ import annotations
195
196 from typing import Callable, Any, Literal, NoReturn, TypedDict, NamedTuple
197
198 list_ann: list[int]
199 any_ann: Any
200 tuple_ann: tuple[int, str]
201 union_ann: int | None
202 callable_ann: Callable[[int], str]
203 type_type_ann: type[int]
204 literal_ann: Literal['x', 5, False]
205
206 def f() -> NoReturn:
207 assert False
208
209 BadType = 1
210 x: BadType # type: ignore
211
212 class TD(TypedDict):
213 x: int
214
215 td = TD(x=1)
216
217 NT = NamedTuple("NT", [("x", int)])
218
219 nt = NT(x=1)
220
221 [builtins fixtures/tuple.pyi]
222 [typing fixtures/typing-medium.pyi]
223 [out]
224 <not checked>
225
226 [case testExportGenericTypes]
227 from __future__ import annotations
228
229 from typing import TypeVar, Callable
230 from typing_extensions import TypeVarTuple, ParamSpec, Unpack, Concatenate
231
232 T = TypeVar("T")
233
234 def ident(x: T) -> T:
235 return x
236
237 Ts = TypeVarTuple("Ts")
238
239 def ts(t: tuple[Unpack[Ts]]) -> tuple[Unpack[Ts]]:
240 return t
241
242 P = ParamSpec("P")
243
244 def pspec(f: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None:
245 f(*args, **kwargs)
246
247 def concat(f: Callable[Concatenate[int, P], None], *args: P.args, **kwargs: P.kwargs) -> None:
248 f(1, *args, **kwargs)
249
250 [builtins fixtures/tuple.pyi]
251 [out]
252 <not checked>
253
254 [case testExportDifferentNodes]
255 from __future__ import annotations
256
257 import typing
258
259 from typing import overload, TypeVar
260
261 @overload
262 def f(x: int) -> int: ...
263 @overload
264 def f(x: str) -> str: ...
265 def f(x: int | str) -> int | str: ...
266
267 T = TypeVar("T")
268
269 def deco(f: T) -> T:
270 return f
271
272 @deco
273 def foo(x: int) -> int: ...
274
275 X = int
276 x: X = 2
277
278 [builtins fixtures/tuple.pyi]
279 [out]
280 <not checked>
281
282 [case testExportMetaBasic_meta]
283 import typing
284 from typing_extensions import Final
285 [builtins fixtures/tuple.pyi]
286 [out]
287 {
288 "id": "main",
289 "path": ...,
290 "mtime": ...,
291 "size": 49,
292 "hash": "db2252f953c889e6b78dde8e30bd241a0c86b2d9",
293 "data_mtime": ...,
294 "dependencies": [
295 "typing",
296 "typing_extensions",
297 "builtins"
298 ],
299 "suppressed": [],
300 "options": {
301 "other_options": "<hash>",
302 "platform": ...
303 },
304 "dep_prios": [
305 10,
306 5,
307 5
308 ],
309 "dep_lines": [
310 1,
311 2,
312 1
313 ],
314 "dep_hashes": [
315 "<hash>",
316 "<hash>",
317 "<hash>"
318 ],
319 "interface_hash": "<hash>",
320 "version_id": ...,
321 "ignore_all": false,
322 "plugin_data": null
323 }
324 {
325 "id": "types",
326 "path": ...,
327 "mtime": ...,
328 "size": 372,
329 "hash": "7542ae4787693f5598d0e8fc9efe396bd6f9af0c",
330 "data_mtime": ...,
331 "dependencies": [
332 "typing",
333 "builtins"
334 ],
335 "suppressed": [],
336 "options": {
337 "other_options": "<hash>",
338 "platform": ...
339 },
340 "dep_prios": [
341 5,
342 5
343 ],
344 "dep_lines": [
345 1,
346 1
347 ],
348 "dep_hashes": [
349 "<hash>",
350 "<hash>"
351 ],
352 "interface_hash": "<hash>",
353 "version_id": ...,
354 "ignore_all": true,
355 "plugin_data": null
356 }