From ef994b461dc6e1fd76f7419c01d19d2e3e5b0b26 2023-05-01 04:06:17
From: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
Date: 2023-05-01 04:06:17
Subject: [PATCH] Show PEP-678 notes in ultratb (Plain), add tests

---

diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py
index d8c969a..87f6b73 100644
--- a/IPython/core/tests/test_ultratb.py
+++ b/IPython/core/tests/test_ultratb.py
@@ -363,6 +363,29 @@ def r3o2():
             ip.run_cell("r3o2()")
 
 
+class PEP678NotesReportingTest(unittest.TestCase):
+    ERROR_WITH_NOTE = """
+try:
+    raise AssertionError("Message")
+except Exception as e:
+    try:
+        e.add_note("This is a PEP-678 note.")
+    except AttributeError:  # Python <= 3.10
+        e.__notes__ = ("This is a PEP-678 note.",)
+    raise
+    """
+
+    def test_verbose_reports_notes(self):
+        with tt.AssertPrints(["AssertionError", "Message", "This is a PEP-678 note."]):
+            ip.run_cell(self.ERROR_WITH_NOTE)
+
+    def test_plain_reports_notes(self):
+        with tt.AssertPrints(["AssertionError", "Message", "This is a PEP-678 note."]):
+            ip.run_cell("%xmode Plain")
+            ip.run_cell(self.ERROR_WITH_NOTE)
+            ip.run_cell("%xmode Verbose")
+
+
 #----------------------------------------------------------------------------
 
 # module testing (minimal)
diff --git a/IPython/core/ultratb.py b/IPython/core/ultratb.py
index 56da5ff..30367d7 100644
--- a/IPython/core/ultratb.py
+++ b/IPython/core/ultratb.py
@@ -686,6 +686,9 @@ class ListTB(TBTools):
             else:
                 list.append('%s\n' % stype)
 
+            # PEP-678 notes
+            list.extend(f"{x}\n" for x in getattr(value, "__notes__", []))
+
         # sync with user hooks
         if have_filedata:
             ipinst = get_ipython()
diff --git a/docs/source/whatsnew/pr/pep678-notes.rst b/docs/source/whatsnew/pr/pep678-notes.rst
new file mode 100644
index 0000000..7d564d6
--- /dev/null
+++ b/docs/source/whatsnew/pr/pep678-notes.rst
@@ -0,0 +1,5 @@
+Support for PEP-678 Exception Notes
+-----------------------------------
+
+Ultratb now shows :pep:`678` notes, improving your debugging experience on
+Python 3.11+ or with libraries such as Pytest and Hypothesis.