@@ -514,6 +514,7 @@ def check_for_b016(self, node):
514
514
515
515
def check_for_b017 (self , node ):
516
516
"""Checks for use of the evil syntax 'with assertRaises(Exception):'
517
+ or 'with pytest.raises(Exception)'.
517
518
518
519
This form of assertRaises will catch everything that subclasses
519
520
Exception, which happens to be the vast majority of Python internal
@@ -523,10 +524,21 @@ def check_for_b017(self, node):
523
524
"""
524
525
item = node .items [0 ]
525
526
item_context = item .context_expr
527
+
526
528
if (
527
529
hasattr (item_context , "func" )
528
- and hasattr (item_context .func , "attr" )
529
- and item_context .func .attr == "assertRaises"
530
+ and (
531
+ (
532
+ hasattr (item_context .func , "attr" )
533
+ and item_context .func .attr == "assertRaises"
534
+ )
535
+ or (
536
+ isinstance (item_context .func , ast .Attribute )
537
+ and item_context .func .attr == "raises"
538
+ and isinstance (item_context .func .value , ast .Name )
539
+ and item_context .func .value .id == "pytest"
540
+ )
541
+ )
530
542
and len (item_context .args ) == 1
531
543
and isinstance (item_context .args [0 ], ast .Name )
532
544
and item_context .args [0 ].id == "Exception"
@@ -1282,11 +1294,11 @@ def visit_Lambda(self, node):
1282
1294
)
1283
1295
B017 = Error (
1284
1296
message = (
1285
- "B017 assertRaises(Exception): should be considered evil. "
1286
- "It can lead to your test passing even if the code being tested is "
1287
- "never executed due to a typo. Either assert for a more specific "
1288
- "exception (builtin or custom), use assertRaisesRegex, or use the "
1289
- "context manager form of assertRaises."
1297
+ "B017 assertRaises(Exception): or pytest.raises(Exception) should "
1298
+ "be considered evil. It can lead to your test passing even if the "
1299
+ "code being tested is never executed due to a typo. Either assert "
1300
+ "for a more specific exception (builtin or custom), use "
1301
+ "assertRaisesRegex, or use the context manager form of assertRaises."
1290
1302
)
1291
1303
)
1292
1304
B018 = Error (
0 commit comments