forked from PyCQA/flake8-bugbear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b017.py
70 lines (56 loc) · 2.3 KB
/
b017.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Should emit:
B017 - on lines 24, 26, 28, 31 and 32.
"""
import asyncio
import unittest
import pytest
from pytest import raises
CONSTANT = True
def something_else() -> None:
for i in (1, 2, 3):
print(i)
class Foo:
pass
class Foobar(unittest.TestCase):
def evil_raises(self) -> None:
with self.assertRaises(Exception):
raise Exception("Evil I say!")
with self.assertRaises(Exception, msg="Generic exception"):
raise Exception("Evil I say!")
with pytest.raises(Exception):
raise Exception("Evil I say!")
with raises(Exception):
raise Exception("Evil I say!")
# These are evil as well but we are only testing inside a with statement
self.assertRaises(Exception, lambda x, y: x / y, 1, y=0)
pytest.raises(Exception, lambda x, y: x / y, 1, y=0)
raises(Exception, lambda x, y: x / y, 1, y=0)
def context_manager_raises(self) -> None:
with self.assertRaises(Exception) as ex:
raise Exception("Context manager is good")
with pytest.raises(Exception) as pyt_ex:
raise Exception("Context manager is good")
with raises(Exception) as r_ex:
raise Exception("Context manager is good")
self.assertEqual("Context manager is good", str(ex.exception))
self.assertEqual("Context manager is good", str(pyt_ex.value))
self.assertEqual("Context manager is good", str(r_ex.value))
def regex_raises(self) -> None:
with self.assertRaisesRegex(Exception, "Regex is good"):
raise Exception("Regex is good")
with pytest.raises(Exception, match="Regex is good"):
raise Exception("Regex is good")
with raises(Exception, match="Regex is good"):
raise Exception("Regex is good")
def non_context_manager_raises(self) -> None:
self.assertRaises(ZeroDivisionError, lambda x, y: x / y, 1, y=0)
pytest.raises(ZeroDivisionError, lambda x, y: x / y, 1, y=0)
raises(ZeroDivisionError, lambda x, y: x / y, 1, y=0)
def raises_with_absolute_reference(self):
with self.assertRaises(asyncio.CancelledError):
Foo()
with pytest.raises(asyncio.CancelledError):
Foo()
with raises(asyncio.CancelledError):
Foo()