@@ -15,12 +15,12 @@ class ParserTestCase(unittest.TestCase):
15
15
16
16
maxDiff = None
17
17
18
- versions = [(2 , 6 ), (2 , 7 )]
18
+ versions = [(2 , 6 ), (2 , 7 ), ( 3 , 0 ), ( 3 , 1 ) ]
19
19
20
20
def parser_for (self , code , version , interactive = False ):
21
21
code = code .replace ("·" , "\n " )
22
22
23
- self .source_buffer = source .Buffer (code )
23
+ self .source_buffer = source .Buffer (code , str ( version ) )
24
24
self .lexer = lexer .Lexer (self .source_buffer , version , interactive = interactive )
25
25
26
26
old_next = self .lexer .next
@@ -123,14 +123,15 @@ def assertParsesGen(self, expected_flat_ast, code,
123
123
124
124
ast = self .parser_for (code + "\n " , version ).file_input ()
125
125
flat_ast = self .flatten_ast (ast )
126
- python_ast = pyast .parse (code .replace ("·" , "\n " ) + "\n " )
127
- flat_python_ast = self .flatten_python_ast (python_ast )
128
126
self .assertEqual ({'ty' : 'Module' , 'body' : expected_flat_ast },
129
127
flat_ast )
130
- if validate_if ():
128
+ self .match_loc (ast , loc_matcher , ast_slicer )
129
+
130
+ if version == sys .version_info [0 :2 ] and validate_if ():
131
+ python_ast = pyast .parse (code .replace ("·" , "\n " ) + "\n " )
132
+ flat_python_ast = self .flatten_python_ast (python_ast )
131
133
self .assertEqual ({'ty' : 'Module' , 'body' : expected_flat_ast },
132
134
flat_python_ast )
133
- self .match_loc (ast , loc_matcher , ast_slicer )
134
135
135
136
def assertParsesSuite (self , expected_flat_ast , code , loc_matcher = "" , ** kwargs ):
136
137
self .assertParsesGen (expected_flat_ast , code ,
@@ -155,8 +156,12 @@ def assertParsesToplevel(self, expected_flat_ast, code,
155
156
ast = getattr (self .parser_for (code , version = version , interactive = interactive ), mode )()
156
157
self .assertEqual (expected_flat_ast , self .flatten_ast (ast ))
157
158
158
- def assertDiagnoses (self , code , level , reason , args = {}, loc_matcher = "" ):
159
+ def assertDiagnoses (self , code , level , reason , args = {}, loc_matcher = "" ,
160
+ only_if = lambda ver : True ):
159
161
for version in self .versions :
162
+ if not only_if (version ):
163
+ continue
164
+
160
165
try :
161
166
self .parser_for (code , version ).file_input ()
162
167
self .fail ("Expected a diagnostic" )
@@ -170,7 +175,8 @@ def assertDiagnoses(self, code, level, reason, args={}, loc_matcher=""):
170
175
self .match_loc ([e .diagnostic .location ] + e .diagnostic .highlights ,
171
176
loc_matcher )
172
177
173
- def assertDiagnosesUnexpected (self , code , err_token , loc_matcher = "" ):
178
+ def assertDiagnosesUnexpected (self , code , err_token , loc_matcher = "" ,
179
+ only_if = lambda ver : True ):
174
180
self .assertDiagnoses (code ,
175
181
"fatal" , "unexpected {actual}: expected {expected}" ,
176
182
{'actual' : err_token }, loc_matcher = "" )
@@ -190,6 +196,11 @@ def assertDiagnosesUnexpected(self, code, err_token, loc_matcher=""):
190
196
ast_z = {'ty' : 'Name' , 'id' : 'z' , 'ctx' : None }
191
197
ast_t = {'ty' : 'Name' , 'id' : 't' , 'ctx' : None }
192
198
199
+ ast_arg_x = {'ty' : 'arg' , 'arg' : 'x' , 'annotation' : None }
200
+ ast_arg_y = {'ty' : 'arg' , 'arg' : 'y' , 'annotation' : None }
201
+ ast_arg_z = {'ty' : 'arg' , 'arg' : 'z' , 'annotation' : None }
202
+ ast_arg_t = {'ty' : 'arg' , 'arg' : 't' , 'annotation' : None }
203
+
193
204
#
194
205
# LITERALS
195
206
#
@@ -233,6 +244,25 @@ def test_ident(self):
233
244
"foo" ,
234
245
"~~~ loc" )
235
246
247
+ def test_named (self ):
248
+ self .assertParsesExpr (
249
+ {'ty' : 'NameConstant' , 'value' : None },
250
+ "None" ,
251
+ "~~~~ loc" ,
252
+ only_if = lambda ver : ver >= (3 , 0 ))
253
+
254
+ self .assertParsesExpr (
255
+ {'ty' : 'NameConstant' , 'value' : True },
256
+ "True" ,
257
+ "~~~~ loc" ,
258
+ only_if = lambda ver : ver >= (3 , 0 ))
259
+
260
+ self .assertParsesExpr (
261
+ {'ty' : 'NameConstant' , 'value' : False },
262
+ "False" ,
263
+ "~~~~~ loc" ,
264
+ only_if = lambda ver : ver >= (3 , 0 ))
265
+
236
266
#
237
267
# OPERATORS
238
268
#
@@ -378,7 +408,8 @@ def test_compare(self):
378
408
'left' : self .ast_1 , 'comparators' : [self .ast_1 ]},
379
409
"1 <> 1" ,
380
410
"~~~~~~ loc"
381
- " ~~ ops.0.loc" )
411
+ " ~~ ops.0.loc" ,
412
+ only_if = lambda ver : ver < (3 , 0 ))
382
413
383
414
self .assertParsesExpr (
384
415
{'ty' : 'Compare' , 'ops' : [{'ty' : 'In' }],
@@ -550,7 +581,8 @@ def test_repr(self):
550
581
"`1`" ,
551
582
"^ begin_loc"
552
583
" ^ end_loc"
553
- "~~~ loc" )
584
+ "~~~ loc" ,
585
+ only_if = lambda ver : ver < (3 , 0 ))
554
586
555
587
#
556
588
# GENERATOR AND CONDITIONAL EXPRESSIONS
@@ -639,29 +671,46 @@ def test_lambda(self):
639
671
self .assertParsesExpr (
640
672
{'ty' : 'Lambda' ,
641
673
'args' : {'ty' : 'arguments' , 'args' : [], 'defaults' : [],
674
+ 'kwonlyargs' : [], 'kw_defaults' : [],
642
675
'kwarg' : None , 'vararg' : None },
643
676
'body' : self .ast_x },
644
677
"lambda: x" ,
645
678
"~~~~~~ lambda_loc"
646
679
" < args.loc"
647
680
" ^ colon_loc"
648
- "~~~~~~~~~ loc" )
681
+ "~~~~~~~~~ loc" ,
682
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
649
683
650
- def test_old_lambda (self ):
684
+ def test_lambda_nocond (self ):
651
685
self .assertParsesExpr (
652
- {'ty' : 'ListComp ' , 'elt' : self .ast_x , 'generators' : [
686
+ {'ty' : 'GeneratorExp ' , 'elt' : self .ast_x , 'generators' : [
653
687
{'ty' : 'comprehension' , 'iter' : self .ast_z , 'target' : self .ast_y ,
654
688
'ifs' : [{'ty' : 'Lambda' ,
655
689
'args' : {'ty' : 'arguments' , 'args' : [], 'defaults' : [],
690
+ 'kwonlyargs' : [], 'kw_defaults' : [],
656
691
'kwarg' : None , 'vararg' : None },
657
692
'body' : self .ast_t }
658
693
]}
659
694
]},
660
- "[ x for y in z if lambda: t] " ,
695
+ "( x for y in z if lambda: t) " ,
661
696
" ~~~~~~ generators.0.ifs.0.lambda_loc"
662
697
" < generators.0.ifs.0.args.loc"
663
698
" ^ generators.0.ifs.0.colon_loc"
664
- " ~~~~~~~~~ generators.0.ifs.0.loc" )
699
+ " ~~~~~~~~~ generators.0.ifs.0.loc" ,
700
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
701
+
702
+ self .assertParsesExpr (
703
+ {'ty' : 'GeneratorExp' , 'elt' : self .ast_x , 'generators' : [
704
+ {'ty' : 'comprehension' , 'iter' : self .ast_z , 'target' : self .ast_y ,
705
+ 'ifs' : [{'ty' : 'Lambda' ,
706
+ 'args' : {'ty' : 'arguments' , 'args' : [self .ast_arg_t ], 'defaults' : [],
707
+ 'kwonlyargs' : [], 'kw_defaults' : [],
708
+ 'kwarg' : None , 'vararg' : None },
709
+ 'body' : self .ast_t }
710
+ ]}
711
+ ]},
712
+ "(x for y in z if lambda t: t)" ,
713
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
665
714
666
715
#
667
716
# CALLS, ATTRIBUTES AND SUBSCRIPTS
@@ -704,31 +753,41 @@ def test_call(self):
704
753
'args' : [], 'keywords' : []},
705
754
"x(*y)" ,
706
755
" ^ star_loc"
707
- "~~~~~ loc" )
756
+ "~~~~~ loc" ,
757
+ # This and following tests fail because of a grammar bug (conflict)
758
+ # in upstream Python. We get different results because our parsers
759
+ # are different, and upstream works more or less by accident.
760
+ # Upstream "fixed" it with a gross workaround in a minor version
761
+ # (at least 3.1.5).
762
+ # Not really worth fixing for us, so skip.
763
+ only_if = lambda ver : ver not in ((3 , 0 ), (3 , 1 )))
708
764
709
765
self .assertParsesExpr (
710
766
{'ty' : 'Call' , 'func' : self .ast_x , 'starargs' : self .ast_y , 'kwargs' : self .ast_z ,
711
767
'args' : [], 'keywords' : []},
712
768
"x(*y, **z)" ,
713
769
" ^ star_loc"
714
770
" ^^ dstar_loc"
715
- "~~~~~~~~~~ loc" )
771
+ "~~~~~~~~~~ loc" ,
772
+ only_if = lambda ver : ver not in ((3 , 0 ), (3 , 1 )))
716
773
717
774
self .assertParsesExpr (
718
775
{'ty' : 'Call' , 'func' : self .ast_x , 'starargs' : self .ast_y , 'kwargs' : self .ast_z ,
719
776
'args' : [], 'keywords' : [{'ty' : 'keyword' , 'arg' : 't' , 'value' : self .ast_t }]},
720
777
"x(*y, t=t, **z)" ,
721
778
" ^ star_loc"
722
779
" ^^ dstar_loc"
723
- "~~~~~~~~~~~~~~~ loc" )
780
+ "~~~~~~~~~~~~~~~ loc" ,
781
+ only_if = lambda ver : ver not in ((3 , 0 ), (3 , 1 )))
724
782
725
783
self .assertParsesExpr (
726
784
{'ty' : 'Call' , 'func' : self .ast_x , 'starargs' : self .ast_z , 'kwargs' : self .ast_t ,
727
785
'args' : [self .ast_y ], 'keywords' : []},
728
786
"x(y, *z, **t)" ,
729
787
" ^ star_loc"
730
788
" ^^ dstar_loc"
731
- "~~~~~~~~~~~~~ loc" )
789
+ "~~~~~~~~~~~~~ loc" ,
790
+ only_if = lambda ver : ver not in ((3 , 0 ), (3 , 1 )))
732
791
733
792
self .assertParsesExpr (
734
793
{'ty' : 'Call' , 'func' : self .ast_x , 'starargs' : None , 'kwargs' : self .ast_z ,
@@ -808,7 +867,7 @@ def test_subscript(self):
808
867
" ~~~~ slice.loc"
809
868
"~~~~~~~ loc" ,
810
869
# A Python bug places ast.Name(id='None') instead of None in step on <3.0
811
- validate_if = lambda : sys .version_info [ 0 ] > 2 )
870
+ validate_if = lambda : sys .version_info >= ( 3 , 0 ) )
812
871
813
872
self .assertParsesExpr (
814
873
{'ty' : 'Subscript' , 'value' : self .ast_x , 'ctx' : None ,
@@ -824,7 +883,16 @@ def test_subscript(self):
824
883
'slice' : {'ty' : 'Ellipsis' }},
825
884
"x[...]" ,
826
885
" ~~~ slice.loc"
827
- "~~~~~~ loc" )
886
+ "~~~~~~ loc" ,
887
+ only_if = lambda ver : ver < (3 , 0 ))
888
+
889
+ self .assertParsesExpr (
890
+ {'ty' : 'Subscript' , 'value' : self .ast_x , 'ctx' : None ,
891
+ 'slice' : {'ty' : 'Index' , 'value' : {'ty' : 'Ellipsis' }}},
892
+ "x[...]" ,
893
+ " ~~~ slice.loc"
894
+ "~~~~~~ loc" ,
895
+ only_if = lambda ver : ver >= (3 , 0 ))
828
896
829
897
def test_attribute (self ):
830
898
self .assertParsesExpr (
@@ -866,6 +934,16 @@ def test_assign_tuplerhs(self):
866
934
" ~~~~ 0.value.loc"
867
935
"~~~~~~~~ 0.loc" )
868
936
937
+ def test_assign_starred (self ):
938
+ self .assertParsesSuite (
939
+ [{'ty' : 'Assign' , 'targets' : [{'ty' : 'Starred' , 'value' : self .ast_x }],
940
+ 'value' : self .ast_y }],
941
+ "*x = y" ,
942
+ "^ 0.targets.0.star_loc"
943
+ "~~ 0.targets.0.loc"
944
+ "~~~~~~ 0.loc" ,
945
+ only_if = lambda ver : ver >= (3 , 0 ))
946
+
869
947
def test_augassign (self ):
870
948
self .assertParsesSuite (
871
949
[{'ty' : 'AugAssign' , 'op' : {'ty' : 'Add' }, 'target' : self .ast_x , 'value' : self .ast_1 }],
@@ -950,27 +1028,31 @@ def test_print(self):
950
1028
[{'ty' : 'Print' , 'dest' : None , 'values' : [self .ast_1 ], 'nl' : True }],
951
1029
"print 1" ,
952
1030
"~~~~~ 0.keyword_loc"
953
- "~~~~~~~ 0.loc" )
1031
+ "~~~~~~~ 0.loc" ,
1032
+ only_if = lambda ver : ver < (3 , 0 ))
954
1033
955
1034
self .assertParsesSuite (
956
1035
[{'ty' : 'Print' , 'dest' : None , 'values' : [self .ast_1 ], 'nl' : False }],
957
1036
"print 1," ,
958
1037
"~~~~~ 0.keyword_loc"
959
- "~~~~~~~~ 0.loc" )
1038
+ "~~~~~~~~ 0.loc" ,
1039
+ only_if = lambda ver : ver < (3 , 0 ))
960
1040
961
1041
self .assertParsesSuite (
962
1042
[{'ty' : 'Print' , 'dest' : self .ast_2 , 'values' : [self .ast_1 ], 'nl' : True }],
963
1043
"print >>2, 1" ,
964
1044
"~~~~~ 0.keyword_loc"
965
1045
" ~~ 0.dest_loc"
966
- "~~~~~~~~~~~~ 0.loc" )
1046
+ "~~~~~~~~~~~~ 0.loc" ,
1047
+ only_if = lambda ver : ver < (3 , 0 ))
967
1048
968
1049
self .assertParsesSuite (
969
1050
[{'ty' : 'Print' , 'dest' : self .ast_2 , 'values' : [self .ast_1 ], 'nl' : False }],
970
1051
"print >>2, 1," ,
971
1052
"~~~~~ 0.keyword_loc"
972
1053
" ~~ 0.dest_loc"
973
- "~~~~~~~~~~~~~ 0.loc" )
1054
+ "~~~~~~~~~~~~~ 0.loc" ,
1055
+ only_if = lambda ver : ver < (3 , 0 ))
974
1056
975
1057
def test_del (self ):
976
1058
self .assertParsesSuite (
@@ -1029,28 +1111,48 @@ def test_yield(self):
1029
1111
1030
1112
def test_raise (self ):
1031
1113
self .assertParsesSuite (
1032
- [{'ty' : 'Raise' , 'type' : None , 'inst' : None , 'tback' : None }],
1114
+ [{'ty' : 'Raise' , 'exc' : None , 'inst' : None ,
1115
+ 'tback' : None , 'cause' : None }],
1033
1116
"raise" ,
1034
1117
"~~~~~ 0.keyword_loc"
1035
- "~~~~~ 0.loc" )
1118
+ "~~~~~ 0.loc" ,
1119
+ validate_if = lambda : False )
1036
1120
1037
1121
self .assertParsesSuite (
1038
- [{'ty' : 'Raise' , 'type' : self .ast_x , 'inst' : None , 'tback' : None }],
1122
+ [{'ty' : 'Raise' , 'exc' : self .ast_x , 'inst' : None ,
1123
+ 'tback' : None , 'cause' : None }],
1039
1124
"raise x" ,
1040
1125
"~~~~~ 0.keyword_loc"
1041
- "~~~~~~~ 0.loc" )
1126
+ "~~~~~~~ 0.loc" ,
1127
+ validate_if = lambda : False )
1042
1128
1043
1129
self .assertParsesSuite (
1044
- [{'ty' : 'Raise' , 'type' : self .ast_x , 'inst' : self .ast_y , 'tback' : None }],
1130
+ [{'ty' : 'Raise' , 'exc' : self .ast_x , 'inst' : self .ast_y ,
1131
+ 'tback' : None , 'cause' : None }],
1045
1132
"raise x, y" ,
1046
1133
"~~~~~ 0.keyword_loc"
1047
- "~~~~~~~~~~ 0.loc" )
1134
+ "~~~~~~~~~~ 0.loc" ,
1135
+ only_if = lambda ver : ver < (3 , 0 ),
1136
+ validate_if = lambda : False )
1048
1137
1049
1138
self .assertParsesSuite (
1050
- [{'ty' : 'Raise' , 'type' : self .ast_x , 'inst' : self .ast_y , 'tback' : self .ast_z }],
1139
+ [{'ty' : 'Raise' , 'exc' : self .ast_x , 'inst' : self .ast_y ,
1140
+ 'tback' : self .ast_z , 'cause' : None }],
1051
1141
"raise x, y, z" ,
1052
1142
"~~~~~ 0.keyword_loc"
1053
- "~~~~~~~~~~~~~ 0.loc" )
1143
+ "~~~~~~~~~~~~~ 0.loc" ,
1144
+ only_if = lambda ver : ver < (3 , 0 ),
1145
+ validate_if = lambda : False )
1146
+
1147
+ self .assertParsesSuite (
1148
+ [{'ty' : 'Raise' , 'exc' : self .ast_x , 'inst' : None ,
1149
+ 'tback' : None , 'cause' : self .ast_y }],
1150
+ "raise x from y" ,
1151
+ "~~~~~ 0.keyword_loc"
1152
+ " ~~~~ 0.from_loc"
1153
+ "~~~~~~~~~~~~~~ 0.loc" ,
1154
+ only_if = lambda ver : ver >= (3 , 0 ),
1155
+ validate_if = lambda : False )
1054
1156
1055
1157
def test_import (self ):
1056
1158
self .assertParsesSuite (
@@ -1149,6 +1251,14 @@ def test_from(self):
1149
1251
" ~~ 0.dots_loc"
1150
1252
"~~~~~~~~~~~~~~~~~~ 0.loc" )
1151
1253
1254
+ self .assertParsesSuite (
1255
+ [{'ty' : 'ImportFrom' , 'names' : [
1256
+ {'ty' : 'alias' , 'name' : 'foo' , 'asname' : None }
1257
+ ], 'module' : None , 'level' : 3 }],
1258
+ "from ... import foo" ,
1259
+ " ~~~ 0.dots_loc"
1260
+ "~~~~~~~~~~~~~~~~~~~ 0.loc" )
1261
+
1152
1262
def test_global (self ):
1153
1263
self .assertParsesSuite (
1154
1264
[{'ty' : 'Global' , 'names' : ['x' , 'y' ]}],
@@ -1158,26 +1268,39 @@ def test_global(self):
1158
1268
" ^ 0.name_locs.1"
1159
1269
"~~~~~~~~~~~ 0.loc" )
1160
1270
1271
+ def test_nonlocal (self ):
1272
+ self .assertParsesSuite (
1273
+ [{'ty' : 'Nonlocal' , 'names' : ['x' , 'y' ]}],
1274
+ "nonlocal x, y" ,
1275
+ "~~~~~~~~ 0.keyword_loc"
1276
+ " ^ 0.name_locs.0"
1277
+ " ^ 0.name_locs.1"
1278
+ "~~~~~~~~~~~~~ 0.loc" ,
1279
+ only_if = lambda ver : ver >= (3 , 0 ))
1280
+
1161
1281
def test_exec (self ):
1162
1282
self .assertParsesSuite (
1163
1283
[{'ty' : 'Exec' , 'body' : self .ast_1 , 'globals' : None , 'locals' : None }],
1164
1284
"exec 1" ,
1165
1285
"~~~~ 0.keyword_loc"
1166
- "~~~~~~ 0.loc" )
1286
+ "~~~~~~ 0.loc" ,
1287
+ only_if = lambda ver : ver < (3 , 0 ))
1167
1288
1168
1289
self .assertParsesSuite (
1169
1290
[{'ty' : 'Exec' , 'body' : self .ast_1 , 'globals' : self .ast_2 , 'locals' : None }],
1170
1291
"exec 1 in 2" ,
1171
1292
"~~~~ 0.keyword_loc"
1172
1293
" ~~ 0.in_loc"
1173
- "~~~~~~~~~~~ 0.loc" )
1294
+ "~~~~~~~~~~~ 0.loc" ,
1295
+ only_if = lambda ver : ver < (3 , 0 ))
1174
1296
1175
1297
self .assertParsesSuite (
1176
1298
[{'ty' : 'Exec' , 'body' : self .ast_1 , 'globals' : self .ast_2 , 'locals' : self .ast_3 }],
1177
1299
"exec 1 in 2, 3" ,
1178
1300
"~~~~ 0.keyword_loc"
1179
1301
" ~~ 0.in_loc"
1180
- "~~~~~~~~~~~~~~ 0.loc" )
1302
+ "~~~~~~~~~~~~~~ 0.loc" ,
1303
+ only_if = lambda ver : ver < (3 , 0 ))
1181
1304
1182
1305
def test_assert (self ):
1183
1306
self .assertParsesSuite (
@@ -1304,7 +1427,8 @@ def test_try(self):
1304
1427
'body' : [self .ast_expr_2 ]}
1305
1428
]}],
1306
1429
"try:· 1·except y, t:· 2" ,
1307
- " ^ 0.handlers.0.as_loc" )
1430
+ " ^ 0.handlers.0.as_loc" ,
1431
+ only_if = lambda ver : ver < (3 , 0 ))
1308
1432
1309
1433
self .assertParsesSuite (
1310
1434
[{'ty' : 'TryExcept' , 'body' : [self .ast_expr_1 ], 'orelse' : [self .ast_expr_3 ],
@@ -1376,81 +1500,97 @@ def test_with(self):
1376
1500
" ~~~~~~ 0.items.0.loc"
1377
1501
" ~ 0.items.1.loc"
1378
1502
"~~~~~~~~~~~~~~~~~~~ 0.loc" ,
1379
- only_if = lambda ver : ver >= (2 , 7 ),
1503
+ only_if = lambda ver : ver == (2 , 7 ) or ver >= ( 3 , 1 ),
1380
1504
validate_if = lambda : sys .version_info >= (3 , 0 ))
1381
1505
1382
1506
def test_class (self ):
1383
1507
self .assertParsesSuite (
1384
1508
[{'ty' : 'ClassDef' , 'name' : 'x' , 'bases' : [],
1509
+ 'keywords' : [], 'starargs' : None , 'kwargs' : None ,
1385
1510
'body' : [{'ty' : 'Pass' }], 'decorator_list' : []}],
1386
1511
"class x:· pass" ,
1387
1512
"~~~~~ 0.keyword_loc"
1388
1513
" ^ 0.name_loc"
1389
1514
" ^ 0.colon_loc"
1390
- "~~~~~~~~~~~~~~~ 0.loc" )
1515
+ "~~~~~~~~~~~~~~~ 0.loc" ,
1516
+ validate_if = lambda : sys .version_info >= (3 , 0 ))
1391
1517
1392
1518
self .assertParsesSuite (
1393
1519
[{'ty' : 'ClassDef' , 'name' : 'x' , 'bases' : [self .ast_y , self .ast_z ],
1520
+ 'keywords' : [], 'starargs' : None , 'kwargs' : None ,
1394
1521
'body' : [{'ty' : 'Pass' }], 'decorator_list' : []}],
1395
1522
"class x(y, z):· pass" ,
1396
1523
" ^ 0.lparen_loc"
1397
1524
" ^ 0.rparen_loc"
1398
- "~~~~~~~~~~~~~~~~~~~~~ 0.loc" )
1525
+ "~~~~~~~~~~~~~~~~~~~~~ 0.loc" ,
1526
+ validate_if = lambda : sys .version_info >= (3 , 0 ))
1399
1527
1400
1528
def test_func (self ):
1401
1529
self .assertParsesSuite (
1402
1530
[{'ty' : 'FunctionDef' , 'name' : 'foo' ,
1403
1531
'args' : {'ty' : 'arguments' , 'args' : [], 'defaults' : [],
1532
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1404
1533
'kwarg' : None , 'vararg' : None },
1534
+ 'returns' : None ,
1405
1535
'body' : [{'ty' : 'Pass' }], 'decorator_list' : []}],
1406
1536
"def foo():· pass" ,
1407
1537
"~~~ 0.keyword_loc"
1408
1538
" ~~~ 0.name_loc"
1409
1539
" ^ 0.args.begin_loc"
1410
1540
" ^ 0.args.end_loc"
1411
1541
" ^ 0.colon_loc"
1412
- "~~~~~~~~~~~~~~~~~ 0.loc" )
1542
+ "~~~~~~~~~~~~~~~~~ 0.loc" ,
1543
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1413
1544
1414
1545
def test_decorated (self ):
1415
1546
self .assertParsesSuite (
1416
1547
[{'ty' : 'ClassDef' , 'name' : 'x' , 'bases' : [],
1548
+ 'keywords' : [], 'starargs' : None , 'kwargs' : None ,
1417
1549
'body' : [{'ty' : 'Pass' }], 'decorator_list' : [self .ast_x ]}],
1418
1550
"@x·class x:· pass" ,
1419
1551
"^ 0.at_locs.0"
1420
1552
" ^ 0.decorator_list.0.loc"
1421
- "~~~~~~~~~~~~~~~~~~ 0.loc" )
1553
+ "~~~~~~~~~~~~~~~~~~ 0.loc" ,
1554
+ validate_if = lambda : sys .version_info >= (3 , 0 ))
1422
1555
1423
1556
self .assertParsesSuite (
1424
1557
[{'ty' : 'ClassDef' , 'name' : 'x' , 'bases' : [],
1558
+ 'keywords' : [], 'starargs' : None , 'kwargs' : None ,
1425
1559
'body' : [{'ty' : 'Pass' }], 'decorator_list' : [
1426
1560
{'ty' : 'Call' , 'func' : self .ast_x ,
1427
1561
'args' : [], 'keywords' : [], 'kwargs' : None , 'starargs' : None }
1428
1562
]}],
1429
1563
"@x()·class x:· pass" ,
1430
1564
"^ 0.at_locs.0"
1431
1565
" ~~~ 0.decorator_list.0.loc"
1432
- "~~~~~~~~~~~~~~~~~~~~ 0.loc" )
1566
+ "~~~~~~~~~~~~~~~~~~~~ 0.loc" ,
1567
+ validate_if = lambda : sys .version_info >= (3 , 0 ))
1433
1568
1434
1569
self .assertParsesSuite (
1435
1570
[{'ty' : 'ClassDef' , 'name' : 'x' , 'bases' : [],
1571
+ 'keywords' : [], 'starargs' : None , 'kwargs' : None ,
1436
1572
'body' : [{'ty' : 'Pass' }], 'decorator_list' : [
1437
1573
{'ty' : 'Call' , 'func' : self .ast_x ,
1438
1574
'args' : [self .ast_1 ], 'keywords' : [], 'kwargs' : None , 'starargs' : None }
1439
1575
]}],
1440
1576
"@x(1)·class x:· pass" ,
1441
1577
"^ 0.at_locs.0"
1442
1578
" ~~~~ 0.decorator_list.0.loc"
1443
- "~~~~~~~~~~~~~~~~~~~~~ 0.loc" )
1579
+ "~~~~~~~~~~~~~~~~~~~~~ 0.loc" ,
1580
+ validate_if = lambda : sys .version_info >= (3 , 0 ))
1444
1581
1445
1582
self .assertParsesSuite (
1446
1583
[{'ty' : 'FunctionDef' , 'name' : 'x' ,
1447
1584
'args' : {'ty' : 'arguments' , 'args' : [], 'defaults' : [],
1585
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1448
1586
'kwarg' : None , 'vararg' : None },
1587
+ 'returns' : None ,
1449
1588
'body' : [{'ty' : 'Pass' }], 'decorator_list' : [self .ast_x ]}],
1450
1589
"@x·def x():· pass" ,
1451
1590
"^ 0.at_locs.0"
1452
1591
" ^ 0.decorator_list.0.loc"
1453
- "~~~~~~~~~~~~~~~~~~ 0.loc" )
1592
+ "~~~~~~~~~~~~~~~~~~ 0.loc" ,
1593
+ validate_if = lambda : sys .version_info >= (3 , 0 ))
1454
1594
1455
1595
#
1456
1596
# FUNCTION AND LAMBDA ARGUMENTS
@@ -1459,101 +1599,158 @@ def test_decorated(self):
1459
1599
def test_args (self ):
1460
1600
self .assertParsesArgs (
1461
1601
{'ty' : 'arguments' , 'args' : [], 'defaults' : [],
1602
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1462
1603
'vararg' : None , 'kwarg' : None },
1463
- "" )
1604
+ "" ,
1605
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1464
1606
1465
1607
self .assertParsesArgs (
1466
- {'ty' : 'arguments' , 'args' : [self .ast_x ], 'defaults' : [],
1608
+ {'ty' : 'arguments' , 'args' : [self .ast_arg_x ], 'defaults' : [],
1609
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1467
1610
'vararg' : None , 'kwarg' : None },
1468
1611
"x" ,
1469
1612
"~ args.0.loc"
1470
- "~ loc" )
1613
+ "~ loc" ,
1614
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1471
1615
1472
1616
self .assertParsesArgs (
1473
- {'ty' : 'arguments' , 'args' : [self .ast_x ], 'defaults' : [self .ast_1 ],
1617
+ {'ty' : 'arguments' , 'args' : [self .ast_arg_x ], 'defaults' : [self .ast_1 ],
1618
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1474
1619
'vararg' : None , 'kwarg' : None },
1475
1620
"x=1" ,
1476
1621
"~ args.0.loc"
1477
1622
" ~ equals_locs.0"
1478
1623
" ~ defaults.0.loc"
1479
- "~~~ loc" )
1624
+ "~~~ loc" ,
1625
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1480
1626
1481
1627
self .assertParsesArgs (
1482
- {'ty' : 'arguments' , 'args' : [self .ast_x , self .ast_y ], 'defaults' : [],
1628
+ {'ty' : 'arguments' , 'args' : [self .ast_arg_x , self .ast_arg_y ], 'defaults' : [],
1629
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1483
1630
'vararg' : None , 'kwarg' : None },
1484
1631
"x, y" ,
1485
- "~~~~ loc" )
1632
+ "~~~~ loc" ,
1633
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1486
1634
1487
1635
self .assertParsesArgs (
1488
1636
{'ty' : 'arguments' , 'args' : [], 'defaults' : [],
1489
- 'vararg' : 'y' , 'kwarg' : None },
1637
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1638
+ 'vararg' : self .ast_arg_y , 'kwarg' : None },
1490
1639
"*y" ,
1491
1640
"^ star_loc"
1492
- " ~ vararg_loc"
1493
- "~~ loc" )
1641
+ " ~ vararg.arg_loc"
1642
+ " ~ vararg.loc"
1643
+ "~~ loc" ,
1644
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1645
+
1646
+ self .assertParsesArgs (
1647
+ {'ty' : 'arguments' , 'args' : [], 'defaults' : [],
1648
+ 'kwonlyargs' : [self .ast_arg_z ], 'kw_defaults' : [],
1649
+ 'vararg' : self .ast_arg_y , 'kwarg' : None },
1650
+ "*y, z" ,
1651
+ "~~~~~ loc" ,
1652
+ only_if = lambda ver : ver >= (3 , 0 ),
1653
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1654
+
1655
+ self .assertParsesArgs (
1656
+ {'ty' : 'arguments' , 'args' : [], 'defaults' : [],
1657
+ 'kwonlyargs' : [self .ast_arg_z , self .ast_arg_t ], 'kw_defaults' : [self .ast_1 ],
1658
+ 'vararg' : self .ast_arg_y , 'kwarg' : None },
1659
+ "*y, z, t=1" ,
1660
+ "~~~~~~~~~~ loc" ,
1661
+ only_if = lambda ver : ver >= (3 , 0 ),
1662
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1494
1663
1495
1664
self .assertParsesArgs (
1496
- {'ty' : 'arguments' , 'args' : [self .ast_x ], 'defaults' : [],
1497
- 'vararg' : 'y' , 'kwarg' : None },
1665
+ {'ty' : 'arguments' , 'args' : [self .ast_arg_x ], 'defaults' : [],
1666
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1667
+ 'vararg' : self .ast_arg_y , 'kwarg' : None },
1498
1668
"x, *y" ,
1499
1669
" ^ star_loc"
1500
- " ~ vararg_loc"
1501
- "~~~~~ loc" )
1670
+ "~~~~~ loc" ,
1671
+ validate_if = lambda : sys . version_info >= ( 3 , 2 ) )
1502
1672
1503
1673
self .assertParsesArgs (
1504
1674
{'ty' : 'arguments' , 'args' : [], 'defaults' : [],
1505
- 'vararg' : None , 'kwarg' : 'y' },
1675
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1676
+ 'vararg' : None , 'kwarg' : self .ast_arg_y },
1506
1677
"**y" ,
1507
1678
"^^ dstar_loc"
1508
- " ~ kwarg_loc"
1509
- "~~~ loc" )
1679
+ "~~~ loc" ,
1680
+ validate_if = lambda : sys . version_info >= ( 3 , 2 ) )
1510
1681
1511
1682
self .assertParsesArgs (
1512
- {'ty' : 'arguments' , 'args' : [self .ast_x ], 'defaults' : [],
1513
- 'vararg' : None , 'kwarg' : 'y' },
1683
+ {'ty' : 'arguments' , 'args' : [self .ast_arg_x ], 'defaults' : [],
1684
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1685
+ 'vararg' : None , 'kwarg' : self .ast_arg_y },
1514
1686
"x, **y" ,
1515
1687
" ^^ dstar_loc"
1516
- " ~ kwarg_loc"
1517
- "~~~~~~ loc" )
1688
+ "~~~~~~ loc" ,
1689
+ validate_if = lambda : sys . version_info >= ( 3 , 2 ) )
1518
1690
1519
1691
self .assertParsesArgs (
1520
- {'ty' : 'arguments' , 'args' : [self .ast_x ], 'defaults' : [],
1521
- 'vararg' : 'y' , 'kwarg' : 'z' },
1692
+ {'ty' : 'arguments' , 'args' : [self .ast_arg_x ], 'defaults' : [],
1693
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1694
+ 'vararg' : self .ast_arg_y , 'kwarg' : self .ast_arg_z },
1522
1695
"x, *y, **z" ,
1523
1696
" ^ star_loc"
1524
- " ~ vararg_loc"
1525
1697
" ^^ dstar_loc"
1526
- " ~ kwarg_loc"
1527
- "~~~~~~~~~~ loc" )
1698
+ "~~~~~~~~~~ loc" ,
1699
+ validate_if = lambda : sys . version_info >= ( 3 , 2 ) )
1528
1700
1529
1701
self .assertParsesArgs (
1530
1702
{'ty' : 'arguments' , 'defaults' : [], 'vararg' : None , 'kwarg' : None ,
1531
- 'args' : [{'ty' : 'Tuple' , 'ctx' : None , 'elts' : [self .ast_x , self .ast_y ]}]},
1703
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1704
+ 'args' : [{'ty' : 'Tuple' , 'ctx' : None , 'elts' : [self .ast_arg_x , self .ast_arg_y ]}]},
1532
1705
"(x,y)" ,
1533
1706
"^ args.0.begin_loc"
1534
1707
" ^ args.0.end_loc"
1535
1708
"~~~~~ args.0.loc"
1536
- "~~~~~ loc" )
1709
+ "~~~~~ loc" ,
1710
+ only_if = lambda ver : ver < (3 , 0 ),
1711
+ validate_if = lambda : False )
1537
1712
1538
1713
def test_args_def (self ):
1539
1714
self .assertParsesSuite (
1540
1715
[{'ty' : 'FunctionDef' , 'name' : 'foo' ,
1541
- 'args' : {'ty' : 'arguments' , 'args' : [self .ast_x ], 'defaults' : [],
1716
+ 'args' : {'ty' : 'arguments' , 'args' : [self .ast_arg_x ], 'defaults' : [],
1717
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1718
+ 'kwarg' : None , 'vararg' : None },
1719
+ 'returns' : None ,
1720
+ 'body' : [{'ty' : 'Pass' }], 'decorator_list' : []}],
1721
+ "def foo(x):· pass" ,
1722
+ validate_if = lambda : sys .version_info >= (3 , 0 ))
1723
+
1724
+ def test_def_typed (self ):
1725
+ self .assertParsesSuite (
1726
+ [{'ty' : 'FunctionDef' , 'name' : 'foo' ,
1727
+ 'args' : {'ty' : 'arguments' ,
1728
+ 'args' : [{'ty' : 'arg' , 'arg' : 'x' , 'annotation' : self .ast_y }],
1729
+ 'defaults' : [],
1730
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1542
1731
'kwarg' : None , 'vararg' : None },
1732
+ 'returns' : self .ast_z ,
1543
1733
'body' : [{'ty' : 'Pass' }], 'decorator_list' : []}],
1544
- "def foo(x):· pass" )
1734
+ "def foo(x: y) -> z:· pass" ,
1735
+ " ^ 0.args.args.0.colon_loc"
1736
+ " ~~~~ 0.args.args.0.loc"
1737
+ " ^^ 0.arrow_loc" ,
1738
+ only_if = lambda ver : ver >= (3 , 0 ),
1739
+ validate_if = lambda : sys .version_info >= (3 , 0 ))
1545
1740
1546
1741
def test_args_oldlambda (self ):
1547
1742
self .assertParsesExpr (
1548
1743
{'ty' : 'ListComp' , 'elt' : self .ast_x , 'generators' : [
1549
1744
{'ty' : 'comprehension' , 'iter' : self .ast_z , 'target' : self .ast_y ,
1550
1745
'ifs' : [{'ty' : 'Lambda' ,
1551
- 'args' : {'ty' : 'arguments' , 'args' : [self .ast_x ], 'defaults' : [],
1746
+ 'args' : {'ty' : 'arguments' , 'args' : [self .ast_arg_x ], 'defaults' : [],
1747
+ 'kwonlyargs' : [], 'kw_defaults' : [],
1552
1748
'kwarg' : None , 'vararg' : None },
1553
1749
'body' : self .ast_t }
1554
1750
]}
1555
1751
]},
1556
- "[x for y in z if lambda x: t]" )
1752
+ "[x for y in z if lambda x: t]" ,
1753
+ validate_if = lambda : sys .version_info >= (3 , 2 ))
1557
1754
1558
1755
#
1559
1756
# PARSING MODES
@@ -1657,7 +1854,9 @@ def test_diag_call(self):
1657
1854
"x(*y, z)" ,
1658
1855
'fatal' , "only named arguments may follow *expression" , {},
1659
1856
" ^ 0"
1660
- " ~~ 1" )
1857
+ " ~~ 1" ,
1858
+ # see test_call
1859
+ only_if = lambda ver : ver not in ((3 , 0 ), (3 , 1 )))
1661
1860
1662
1861
self .assertDiagnoses (
1663
1862
"x(y=1, z)" ,
0 commit comments