Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 5d199d2

Browse files
Denys Smirnovdennwc
authored andcommitted
handle with return types other than identifiers; fixes #187
Allow types other than BoxedName as a type of return. Also make sure to include the Init to all returns - Python has implicit returns. Signed-off-by: Denys Smirnov <[email protected]>
1 parent d64d05a commit 5d199d2

File tree

50 files changed

+2313
-170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2313
-170
lines changed

driver/normalizer/normalizer.go

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,25 @@ func funcDefMap(typ string, async bool) Mapping {
4242
// just that there is no annotation (but the function could still return some
4343
// other type!).
4444
{Name: "returns", Optional: "ret_opt", Op: Cases("ret_case",
45+
// case 1: no type annotations for the return
4546
Is(nil),
46-
Obj{
47-
uast.KeyType: String("BoxedName"),
48-
"boxed_value": Var("ret_type"),
47+
// case 2: boxed identifier
48+
Fields{
49+
{Name: uast.KeyType, Op: String("BoxedName")},
50+
{Name: "boxed_value", Op: Var("ret_type")},
4951
// No problem dropping this one, it's used by an internal interpreter optimization/cache
5052
// without semantic meaning
51-
"ctx": Any(),
52-
}),
53-
},
53+
{Name: "ctx", Op: Any()},
54+
// FIXME: change this once we've a way to store other nodes on semantic objects
55+
// See: https://github.com/bblfsh/sdk/issues/361
56+
// See: https://github.com/bblfsh/python-driver/issues/178
57+
{Name: "noops_previous", Drop: true, Op: Any()},
58+
{Name: "noops_sameline", Drop: true, Op: Any()},
59+
},
60+
// case 3: everything else
61+
// TODO: not reversible
62+
Var("ret_type"),
63+
)},
5464
{Name: "decorator_list", Op: Var("func_decorators")},
5565
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
5666
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
@@ -68,22 +78,44 @@ func funcDefMap(typ string, async bool) Mapping {
6878
},
6979
UASTType(uast.Alias{}, Obj{
7080
// FIXME: can't call identifierWithPos because it would take the position of the
71-
// function node that is not exactly the same as the position of the function name
81+
// function node that is not exactly the same as the position of the function name
7282
"Name": UASTType(uast.Identifier{}, Obj{
7383
"Name": Var("name"),
7484
}),
7585
"Node": UASTType(uast.Function{}, Obj{
7686
"Type": UASTType(uast.FunctionType{}, Fields{
7787
{Name: "Arguments", Op: Var("arguments")},
7888
{Name: "Returns", Optional: "ret_opt", Op: Cases("ret_case",
79-
// Dont add None here as default, read the comment on the upper
80-
// side of the annotation
81-
Is(nil),
89+
// Python always adds an implicit return of None if function
90+
// returns nothing, so we will set Init on our returns to None.
91+
92+
// case 1: no type annotations for the return
93+
Arr(UASTType(uast.Argument{},
94+
Obj{
95+
"Init": UASTType(uast.Identifier{},
96+
Obj{"Name": String("None")},
97+
),
98+
},
99+
)),
100+
// case 2: boxed identifier
101+
Arr(UASTType(uast.Argument{},
102+
Obj{
103+
"Type": Var("ret_type"),
104+
"Init": UASTType(uast.Identifier{},
105+
Obj{"Name": String("None")},
106+
),
107+
},
108+
)),
109+
// case 3: everything else
82110
Arr(UASTType(uast.Argument{},
83111
Obj{
84112
"Type": Var("ret_type"),
113+
"Init": UASTType(uast.Identifier{},
114+
Obj{"Name": String("None")},
115+
),
85116
},
86-
)))},
117+
)),
118+
)},
87119
}),
88120
"Body": UASTType(uast.Block{}, Obj{
89121
"Statements": Var("body"),
@@ -220,8 +252,8 @@ var Normalizers = []Mapping{
220252
// FIXME: change this once we've a way to store other nodes on semantic objects
221253
// See: https://github.com/bblfsh/sdk/issues/361
222254
// See: https://github.com/bblfsh/python-driver/issues/178
223-
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
224-
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
255+
{Name: "noops_previous", Drop: true, Op: Any()},
256+
{Name: "noops_sameline", Drop: true, Op: Any()},
225257
},
226258
Fields{
227259
{Name: "arg",
@@ -241,8 +273,8 @@ var Normalizers = []Mapping{
241273
// FIXME: change this once we've a way to store other nodes on semantic objects
242274
// See: https://github.com/bblfsh/sdk/issues/361
243275
// See: https://github.com/bblfsh/python-driver/issues/178
244-
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
245-
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
276+
{Name: "noops_previous", Drop: true, Op: Any()},
277+
{Name: "noops_sameline", Drop: true, Op: Any()},
246278
// This one is pesky - they're ignored by the runtime, could have typing from
247279
// mypy, or could have anything else, so we can assign to the semantic type
248280
{Name: "annotation", Optional: "ann_opt", Op: Any()},
@@ -260,8 +292,8 @@ var Normalizers = []Mapping{
260292
// FIXME: change this once we've a way to store other nodes on semantic objects
261293
// See: https://github.com/bblfsh/sdk/issues/361
262294
// See: https://github.com/bblfsh/python-driver/issues/178
263-
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
264-
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
295+
{Name: "noops_previous", Drop: true, Op: Any()},
296+
{Name: "noops_sameline", Drop: true, Op: Any()},
265297
// This one is pesky - they're ignored by the runtime, could have typing from
266298
// mypy, or could have anything else, so we can assign to the semantic type
267299
{Name: "annotation", Op: Any()},
@@ -278,8 +310,8 @@ var Normalizers = []Mapping{
278310
// FIXME: change this once we've a way to store other nodes on semantic objects
279311
// See: https://github.com/bblfsh/sdk/issues/361
280312
// See: https://github.com/bblfsh/python-driver/issues/178
281-
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
282-
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
313+
{Name: "noops_previous", Drop: true, Op: Any()},
314+
{Name: "noops_sameline", Drop: true, Op: Any()},
283315
// This one is pesky - they're ignored by the runtime, could have typing from
284316
// mypy, or could have anything else, so we can assign to the semantic type
285317
{Name: "annotation", Op: Any()},
@@ -296,8 +328,8 @@ var Normalizers = []Mapping{
296328
// FIXME: change this once we've a way to store other nodes on semantic objects
297329
// See: https://github.com/bblfsh/sdk/issues/361
298330
// See: https://github.com/bblfsh/python-driver/issues/178
299-
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
300-
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
331+
{Name: "noops_previous", Drop: true, Op: Any()},
332+
{Name: "noops_sameline", Drop: true, Op: Any()},
301333
// This one is pesky - they're ignored by the runtime, could have typing from
302334
// mypy, or could have anything else, so we can assign to the semantic type
303335
{Name: "annotation", Op: Any()},
@@ -368,8 +400,8 @@ var Normalizers = []Mapping{
368400
// FIXME: change this once we've a way to store other nodes on semantic objects
369401
// See: https://github.com/bblfsh/sdk/issues/361
370402
// See: https://github.com/bblfsh/python-driver/issues/178
371-
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
372-
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
403+
{Name: "noops_previous", Drop: true, Op: Any()},
404+
{Name: "noops_sameline", Drop: true, Op: Any()},
373405
},
374406
Obj{
375407
"All": Bool(true),
@@ -393,8 +425,8 @@ var Normalizers = []Mapping{
393425
// FIXME: change this once we've a way to store other nodes on semantic objects
394426
// See: https://github.com/bblfsh/sdk/issues/361
395427
// See: https://github.com/bblfsh/python-driver/issues/178
396-
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
397-
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
428+
{Name: "noops_previous", Drop: true, Op: Any()},
429+
{Name: "noops_sameline", Drop: true, Op: Any()},
398430
},
399431
Obj{
400432
"Names": Var("names"),

fixtures/annotations.py.sem.uast

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@
249249
],
250250
Returns: [
251251
{ '@type': "uast:Argument",
252-
Init: ~,
252+
Init: { '@type': "uast:Identifier",
253+
Name: "None",
254+
},
253255
MapVariadic: false,
254256
Name: ~,
255257
Receiver: false,

fixtures/bench_accumulator_factory.py.sem.uast

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,18 @@
223223
Variadic: false,
224224
},
225225
],
226-
Returns: ~,
226+
Returns: [
227+
{ '@type': "uast:Argument",
228+
Init: { '@type': "uast:Identifier",
229+
Name: "None",
230+
},
231+
MapVariadic: false,
232+
Name: ~,
233+
Receiver: false,
234+
Type: ~,
235+
Variadic: false,
236+
},
237+
],
227238
},
228239
},
229240
},
@@ -302,7 +313,18 @@
302313
Variadic: false,
303314
},
304315
],
305-
Returns: ~,
316+
Returns: [
317+
{ '@type': "uast:Argument",
318+
Init: { '@type': "uast:Identifier",
319+
Name: "None",
320+
},
321+
MapVariadic: false,
322+
Name: ~,
323+
Receiver: false,
324+
Type: ~,
325+
Variadic: false,
326+
},
327+
],
306328
},
307329
},
308330
},

fixtures/bench_binary_search.py.sem.uast

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,18 @@
962962
Variadic: false,
963963
},
964964
],
965-
Returns: ~,
965+
Returns: [
966+
{ '@type': "uast:Argument",
967+
Init: { '@type': "uast:Identifier",
968+
Name: "None",
969+
},
970+
MapVariadic: false,
971+
Name: ~,
972+
Receiver: false,
973+
Type: ~,
974+
Variadic: false,
975+
},
976+
],
966977
},
967978
},
968979
},

fixtures/bench_fibonacci.py.sem.uast

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,18 @@
388388
Variadic: false,
389389
},
390390
],
391-
Returns: ~,
391+
Returns: [
392+
{ '@type': "uast:Argument",
393+
Init: { '@type': "uast:Identifier",
394+
Name: "None",
395+
},
396+
MapVariadic: false,
397+
Name: ~,
398+
Receiver: false,
399+
Type: ~,
400+
Variadic: false,
401+
},
402+
],
392403
},
393404
},
394405
},

fixtures/bench_gcd.py.sem.uast

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,18 @@
18801880
Variadic: false,
18811881
},
18821882
],
1883-
Returns: ~,
1883+
Returns: [
1884+
{ '@type': "uast:Argument",
1885+
Init: { '@type': "uast:Identifier",
1886+
Name: "None",
1887+
},
1888+
MapVariadic: false,
1889+
Name: ~,
1890+
Receiver: false,
1891+
Type: ~,
1892+
Variadic: false,
1893+
},
1894+
],
18841895
},
18851896
},
18861897
},

fixtures/bench_happy_numbers.py.sem.uast

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,18 @@
703703
Variadic: false,
704704
},
705705
],
706-
Returns: ~,
706+
Returns: [
707+
{ '@type': "uast:Argument",
708+
Init: { '@type': "uast:Identifier",
709+
Name: "None",
710+
},
711+
MapVariadic: false,
712+
Name: ~,
713+
Receiver: false,
714+
Type: ~,
715+
Variadic: false,
716+
},
717+
],
707718
},
708719
},
709720
},

fixtures/bench_is_prime.py.sem.uast

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,18 @@
13781378
Variadic: false,
13791379
},
13801380
],
1381-
Returns: ~,
1381+
Returns: [
1382+
{ '@type': "uast:Argument",
1383+
Init: { '@type': "uast:Identifier",
1384+
Name: "None",
1385+
},
1386+
MapVariadic: false,
1387+
Name: ~,
1388+
Receiver: false,
1389+
Type: ~,
1390+
Variadic: false,
1391+
},
1392+
],
13821393
},
13831394
},
13841395
},

0 commit comments

Comments
 (0)