@@ -200,6 +200,10 @@ def RaisesRegexOp(context, designator, exceptionClass, expected_regex,
200200 else :
201201 return Node (syms .suite , [with_stmt ])
202202
203+ def FailOp (indent , kws , arglist , node ):
204+ new = node .clone ()
205+ new .set_child (0 , Name ('pytest' ))
206+ return new
203207
204208def add_import (import_name , node ):
205209 suite = get_parent_of_type (node , syms .suite )
@@ -290,6 +294,8 @@ def get_import_nodes(node):
290294 'assertRaisesRegex' : partial (RaisesRegexOp , 'pytest.raises' , 'excinfo' ),
291295 'assertWarnsRegex' : partial (RaisesRegexOp , 'pytest.warns' , 'record' ),
292296
297+ 'fail' : FailOp ,
298+
293299 #'assertLogs': -- not to be handled here, is an context handler only
294300}
295301
@@ -376,7 +382,7 @@ class FixSelfAssert(BaseFix):
376382 PATTERN = """
377383 power< 'self'
378384 trailer< '.' method=( %s ) >
379- trailer< '(' arglist=any ')' >
385+ trailer< '(' [ arglist=any] ')' >
380386 >
381387 """ % ' | ' .join (map (repr ,
382388 (set (_method_map .keys ()) | set (_method_aliases .keys ()))))
@@ -422,8 +428,10 @@ def process_arg(arg):
422428 posargs = []
423429 kwargs = {}
424430
425- # This is either a "arglist" or a single argument
426- if results ['arglist' ].type == syms .arglist :
431+ # This is either empty, an "arglist", or a single argument
432+ if 'arglist' not in results :
433+ pass
434+ elif results ['arglist' ].type == syms .arglist :
427435 for arg in results ['arglist' ].children :
428436 process_arg (arg )
429437 else :
@@ -437,17 +445,17 @@ def process_arg(arg):
437445
438446 required_args , argsdict = utils .resolve_func_args (test_func , posargs , kwargs )
439447
440- if method .startswith (('assertRaises' , 'assertWarns' )):
448+ if method .startswith (('assertRaises' , 'assertWarns' )) or method == 'fail' :
441449 n_stmt = _method_map [method ](* required_args ,
442450 indent = find_indentation (node ),
443451 kws = argsdict ,
444- arglist = results [ 'arglist' ] ,
452+ arglist = results . get ( 'arglist' ) ,
445453 node = node )
446454 else :
447455 n_stmt = Node (syms .assert_stmt ,
448456 [Name ('assert' ),
449457 _method_map [method ](* required_args , kws = argsdict )])
450- if argsdict .get ('msg' , None ) is not None :
458+ if argsdict .get ('msg' , None ) is not None and method != 'fail' :
451459 n_stmt .children .extend ((Name (',' ), argsdict ['msg' ]))
452460
453461 def fix_line_wrapping (x ):
@@ -463,7 +471,7 @@ def fix_line_wrapping(x):
463471 n_stmt .prefix = node .prefix
464472
465473 # add necessary imports
466- if 'Raises' in method or 'Warns' in method :
474+ if 'Raises' in method or 'Warns' in method or method == 'fail' :
467475 add_import ('pytest' , node )
468476 if ('Regex' in method and not 'Raises' in method and
469477 not 'Warns' in method ):
0 commit comments