https://github.com/python/cpython/commit/e97910cdb76c1f1dadfc4721b828611e4f4b6449
commit: e97910cdb76c1f1dadfc4721b828611e4f4b6449
branch: main
author: Irit Katriel <[email protected]>
committer: iritkatriel <[email protected]>
date: 2024-10-15T17:00:04+01:00
summary:

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

files:
M Lib/test/test_cmd_line_script.py
M Lib/test/test_coroutines.py
M Lib/test/test_file.py
M Lib/test/test_listcomps.py
M Lib/test/test_logging.py
M Lib/test/test_pdb.py
M Lib/test/test_peepholer.py
M Lib/test/test_raise.py
M Lib/test/test_sys_setprofile.py
M Lib/test/test_unittest/test_result.py
M Lib/test/test_with.py

diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index 3a5a8abf81e43d..f30107225ff612 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -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:
diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py
index a677301c62becc..e6d65e7d90abb1 100644
--- a/Lib/test/test_coroutines.py
+++ b/Lib/test/test_coroutines.py
@@ -1185,7 +1185,7 @@ async def f():
         async def g():
             try:
                 raise KeyError
-            except:
+            except KeyError:
                 return await f()
 
         _, result = run_async(g())
diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py
index 9df55278693531..1206032a93566e 100644
--- a/Lib/test/test_file.py
+++ b/Lib/test/test_file.py
@@ -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):
diff --git a/Lib/test/test_listcomps.py b/Lib/test/test_listcomps.py
index 45644d6c092782..cffdeeacc5d73b 100644
--- a/Lib/test/test_listcomps.py
+++ b/Lib/test/test_listcomps.py
@@ -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
         """
@@ -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]})
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index d4ceb7c8dc0b41..e72f222e1c7eeb 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -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()
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 474d31f1ae03d9..084b7cd4cad219 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -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
 
@@ -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
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py
index dd3eaeb39e7fe3..b143f3d27a1537 100644
--- a/Lib/test/test_peepholer.py
+++ b/Lib/test/test_peepholer.py
@@ -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')
diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py
index 6d26a61bee4292..dcf0753bc828f3 100644
--- a/Lib/test/test_raise.py
+++ b/Lib/test/test_raise.py
@@ -48,7 +48,7 @@ def test_except_reraise(self):
         def reraise():
             try:
                 raise TypeError("foo")
-            except:
+            except TypeError:
                 try:
                     raise KeyError("caught")
                 except KeyError:
@@ -60,7 +60,7 @@ def test_finally_reraise(self):
         def reraise():
             try:
                 raise TypeError("foo")
-            except:
+            except TypeError:
                 try:
                     raise KeyError("caught")
                 finally:
@@ -73,7 +73,7 @@ def nested_reraise():
         def reraise():
             try:
                 raise TypeError("foo")
-            except:
+            except TypeError:
                 nested_reraise()
         self.assertRaises(TypeError, reraise)
 
@@ -81,7 +81,7 @@ 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)
@@ -91,7 +91,7 @@ def test_with_reraise1(self):
         def reraise():
             try:
                 raise TypeError("foo")
-            except:
+            except TypeError:
                 with Context():
                     pass
                 raise
@@ -101,7 +101,7 @@ def test_with_reraise2(self):
         def reraise():
             try:
                 raise TypeError("foo")
-            except:
+            except TypeError:
                 with Context():
                     raise KeyError("caught")
                 raise
@@ -111,7 +111,7 @@ def test_yield_reraise(self):
         def reraise():
             try:
                 raise TypeError("foo")
-            except:
+            except TypeError:
                 yield 1
                 raise
         g = reraise()
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -459,7 +459,7 @@ def f():
             try:
                 try:
                     raise ValueError
-                except:
+                except ValueError:
                     del g
                     raise KeyError
             except Exception as e:
@@ -475,7 +475,7 @@ class C:
             def __del__(self):
                 try:
                     1/0
-                except:
+                except ZeroDivisionError:
                     raise
 
         def f():
diff --git a/Lib/test/test_sys_setprofile.py b/Lib/test/test_sys_setprofile.py
index b2e8e8a15b67ea..311a4d2cafe88d 100644
--- a/Lib/test/test_sys_setprofile.py
+++ b/Lib/test/test_sys_setprofile.py
@@ -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),
@@ -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),
@@ -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),
@@ -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),
@@ -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),
@@ -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),
diff --git a/Lib/test/test_unittest/test_result.py 
b/Lib/test/test_unittest/test_result.py
index 15e3f62ef66a4b..4e5ec54e9c892a 100644
--- a/Lib/test/test_unittest/test_result.py
+++ b/Lib/test/test_unittest/test_result.py
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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()
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py
index 839cdec68d573e..e3e2de09496728 100644
--- a/Lib/test/test_with.py
+++ b/Lib/test/test_with.py
@@ -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)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to