Skip to content

gh-125522 : add explicit exception types to bare excepts in tests #125523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def test_pep_409_verbiage(self):
script = textwrap.dedent("""\
try:
raise ValueError
except:
except ValueError:
raise NameError from None
""")
with os_helper.temp_dir() as script_dir:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ async def f():
async def g():
try:
raise KeyError
except:
except KeyError:
return await f()

_, result = run_async(g())
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def testMethods(self):
# it must also return None if an exception was given
try:
1/0
except:
except ZeroDivisionError:
self.assertEqual(self.f.__exit__(*sys.exc_info()), None)

def testReadWhenWriting(self):
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def test_comp_in_try_except(self):
result = snapshot = None
try:
result = [{func}(value) for value in value]
except:
except ValueError:
snapshot = value
raise
"""
Expand Down Expand Up @@ -643,7 +643,7 @@ def test_exception_in_post_comp_call(self):
value = [1, None]
try:
[v for v in value].sort()
except:
except TypeError:
pass
"""
self._check_in_scopes(code, {"value": [1, None]})
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4877,7 +4877,7 @@ def test_formatting(self):
r.addHandler(h)
try:
raise RuntimeError('deliberate mistake')
except:
except RuntimeError:
logging.exception('failed', stack_info=True)
r.removeHandler(h)
h.close()
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ def test_convenience_variables():
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... try:
... raise Exception('test')
... except:
... except Exception:
... pass
... return 1

Expand Down Expand Up @@ -1153,7 +1153,7 @@ def test_convenience_variables():
Exception('test')
(Pdb) next
> <doctest test.test_pdb.test_convenience_variables[0]>(5)util_function()
-> except:
-> except Exception:
(Pdb) $_exception
*** KeyError: '_exception'
(Pdb) return
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ def test_load_fast_unknown_after_error_2(self):
def f():
try:
1 / 0
except:
except ZeroDivisionError:
print(a, b, c, d, e, f, g)
a = b = c = d = e = f = g = 1
self.assertInBytecode(f, 'LOAD_FAST_CHECK')
Expand Down
28 changes: 14 additions & 14 deletions Lib/test/test_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_except_reraise(self):
def reraise():
try:
raise TypeError("foo")
except:
except TypeError:
try:
raise KeyError("caught")
except KeyError:
Expand All @@ -60,7 +60,7 @@ def test_finally_reraise(self):
def reraise():
try:
raise TypeError("foo")
except:
except TypeError:
try:
raise KeyError("caught")
finally:
Expand All @@ -73,15 +73,15 @@ def nested_reraise():
def reraise():
try:
raise TypeError("foo")
except:
except TypeError:
nested_reraise()
self.assertRaises(TypeError, reraise)

def test_raise_from_None(self):
try:
try:
raise TypeError("foo")
except:
except TypeError:
raise ValueError() from None
except ValueError as e:
self.assertIsInstance(e.__context__, TypeError)
Expand All @@ -91,7 +91,7 @@ def test_with_reraise1(self):
def reraise():
try:
raise TypeError("foo")
except:
except TypeError:
with Context():
pass
raise
Expand All @@ -101,7 +101,7 @@ def test_with_reraise2(self):
def reraise():
try:
raise TypeError("foo")
except:
except TypeError:
with Context():
raise KeyError("caught")
raise
Expand All @@ -111,7 +111,7 @@ def test_yield_reraise(self):
def reraise():
try:
raise TypeError("foo")
except:
except TypeError:
yield 1
raise
g = reraise()
Expand Down Expand Up @@ -314,7 +314,7 @@ def test_instance_context_instance_raise(self):
try:
try:
raise context
except:
except IndexError:
raise OSError()
except OSError as e:
self.assertIs(e.__context__, context)
Expand All @@ -326,7 +326,7 @@ def test_class_context_instance_raise(self):
try:
try:
raise context
except:
except IndexError:
raise OSError()
except OSError as e:
self.assertIsNot(e.__context__, context)
Expand All @@ -339,7 +339,7 @@ def test_class_context_class_raise(self):
try:
try:
raise context
except:
except IndexError:
raise OSError
except OSError as e:
self.assertIsNot(e.__context__, context)
Expand All @@ -351,7 +351,7 @@ def test_c_exception_context(self):
try:
try:
1/0
except:
except ZeroDivisionError:
raise OSError
except OSError as e:
self.assertIsInstance(e.__context__, ZeroDivisionError)
Expand All @@ -362,7 +362,7 @@ def test_c_exception_raise(self):
try:
try:
1/0
except:
except ZeroDivisionError:
xyzzy
except NameError as e:
self.assertIsInstance(e.__context__, ZeroDivisionError)
Expand Down Expand Up @@ -459,7 +459,7 @@ def f():
try:
try:
raise ValueError
except:
except ValueError:
del g
raise KeyError
except Exception as e:
Expand All @@ -475,7 +475,7 @@ class C:
def __del__(self):
try:
1/0
except:
except ZeroDivisionError:
raise

def f():
Expand Down
14 changes: 7 additions & 7 deletions Lib/test/test_sys_setprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def f(p):
def test_caught_exception(self):
def f(p):
try: 1/0
except: pass
except ZeroDivisionError: pass
f_ident = ident(f)
self.check_events(f, [(1, 'call', f_ident),
(1, 'return', f_ident),
Expand All @@ -133,7 +133,7 @@ def f(p):
def test_caught_nested_exception(self):
def f(p):
try: 1/0
except: pass
except ZeroDivisionError: pass
f_ident = ident(f)
self.check_events(f, [(1, 'call', f_ident),
(1, 'return', f_ident),
Expand All @@ -156,9 +156,9 @@ def f(p):
def g(p):
try:
f(p)
except:
except ZeroDivisionError:
try: f(p)
except: pass
except ZeroDivisionError: pass
f_ident = ident(f)
g_ident = ident(g)
self.check_events(g, [(1, 'call', g_ident),
Expand Down Expand Up @@ -187,7 +187,7 @@ def g(p):
def test_raise_twice(self):
def f(p):
try: 1/0
except: 1/0
except ZeroDivisionError: 1/0
f_ident = ident(f)
self.check_events(f, [(1, 'call', f_ident),
(1, 'return', f_ident),
Expand All @@ -196,7 +196,7 @@ def f(p):
def test_raise_reraise(self):
def f(p):
try: 1/0
except: raise
except ZeroDivisionError: raise
f_ident = ident(f)
self.check_events(f, [(1, 'call', f_ident),
(1, 'return', f_ident),
Expand Down Expand Up @@ -320,7 +320,7 @@ def f(p):
def test_caught_exception(self):
def f(p):
try: 1/0
except: pass
except ZeroDivisionError: pass
f_ident = ident(f)
self.check_events(f, [(1, 'call', f_ident),
(1, 'return', f_ident),
Expand Down
14 changes: 7 additions & 7 deletions Lib/test/test_unittest/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def test_1(self):
test = Foo('test_1')
try:
test.fail("foo")
except:
except AssertionError:
exc_info_tuple = sys.exc_info()

result = unittest.TestResult()
Expand Down Expand Up @@ -214,7 +214,7 @@ def test_1(self):
def get_exc_info():
try:
test.fail("foo")
except:
except AssertionError:
return sys.exc_info()

exc_info_tuple = get_exc_info()
Expand All @@ -241,9 +241,9 @@ def get_exc_info():
try:
try:
test.fail("foo")
except:
except AssertionError:
raise ValueError(42)
except:
except ValueError:
return sys.exc_info()

exc_info_tuple = get_exc_info()
Expand Down Expand Up @@ -271,7 +271,7 @@ def get_exc_info():
loop.__cause__ = loop
loop.__context__ = loop
raise loop
except:
except Exception:
return sys.exc_info()

exc_info_tuple = get_exc_info()
Expand Down Expand Up @@ -300,7 +300,7 @@ def get_exc_info():
ex1.__cause__ = ex2
ex2.__context__ = ex1
raise C
except:
except Exception:
return sys.exc_info()

exc_info_tuple = get_exc_info()
Expand Down Expand Up @@ -345,7 +345,7 @@ def test_1(self):
test = Foo('test_1')
try:
raise TypeError()
except:
except TypeError:
exc_info_tuple = sys.exc_info()

result = unittest.TestResult()
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ def testExceptionInExprList(self):
try:
with self.Dummy() as a, self.InitRaises():
pass
except:
except RuntimeError:
pass
self.assertTrue(a.enter_called)
self.assertTrue(a.exit_called)
Expand Down
Loading