@@ -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" ),
0 commit comments