From 04513ad5d0e0dcfaaaafca743e3ac75245730e69 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 4 Dec 2026 18:52:13 +0000 Subject: [PATCH] Only strip `turtle.`/`screen.` from example code in `turtle` docstrings --- Lib/test/test_turtle.py | 67 +++++++++++++++++++ Lib/turtle.py | 10 ++- ...-12-04-18-50-36.gh-issue-156360.MWlux2.rst | 3 + 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-12-04-18-50-36.gh-issue-156360.MWlux2.rst diff --git a/Lib/test/test_turtle.py b/Lib/test/test_turtle.py index 88e18e7daa5c5a..f4a2706d0339ec 100644 --- a/Lib/test/test_turtle.py +++ b/Lib/test/test_turtle.py @@ -742,6 +742,73 @@ def test_all_signatures(self): self.assertEqual(str(sig), known_signatures[name]) +class TestFunctionDocstrings(unittest.TestCase): + + def test_turtle_docrevise(self): + doc = ( + "Undo the last action of the turtle.\n" + "\n" + "Example (for a Turtle instance named turtle):\n" + " >>> turtle.fd(50); turtle.lt(80)\n" + " >>> for i in range(2):\n" + " ... turtle.undo()\n" + " ...\n" + " >>> turtle.getturtle()\n" + " \n" + "\n" + "This does not move any other turtle.\n" + ) + expected = ( + "Undo the last action of the turtle.\n" + "\n" + "Example:\n" + " >>> fd(50); lt(80)\n" + " >>> for i in range(2):\n" + " ... undo()\n" + " ...\n" + " >>> getturtle()\n" + " \n" + "\n" + "This does not move any other turtle.\n" + ) + self.assertEqual(turtle._turtle_docrevise(doc), expected) + self.assertIsNone(turtle._turtle_docrevise(None)) + + def test_screen_docrevise(self): + doc = ( + "Reset all turtles on the screen.\n" + "\n" + "This method is only available under the name\n" + "resetscreen. See also clearscreen.\n" + "\n" + "Example (for a TurtleScreen instance named screen):\n" + " >>> screen.resetscreen()\n" + ) + expected = ( + "Reset all turtles on the screen.\n" + "\n" + "This method is only available under the name\n" + "resetscreen. See also clearscreen.\n" + "\n" + "Example:\n" + " >>> resetscreen()\n" + ) + self.assertEqual(turtle._screen_docrevise(doc), expected) + self.assertIsNone(turtle._screen_docrevise(None)) + + @support.requires_docstrings + def test_summary_lines(self): + # The summary line of a function is that of its method. + for cls, names in [(turtle.Turtle, turtle._tg_turtle_functions), + (turtle._Screen, turtle._tg_screen_functions)]: + for name in names: + with self.subTest(name=name): + method_doc = getattr(cls, name).__doc__ + func_doc = getattr(turtle, name).__doc__ + self.assertEqual(func_doc.partition('\n')[0], + method_doc.partition('\n')[0]) + + class TurtleDocstringTranslationTest(unittest.TestCase): def _make_translation(self, dirname, filename, docstring): diff --git a/Lib/turtle.py b/Lib/turtle.py index a060d3aa651013..e96770cc4abb8a 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -4057,7 +4057,10 @@ def _turtle_docrevise(docstr): if docstr is None: return None turtlename = _CFG["exampleturtle"] - newdocstr = docstr.replace("%s." % turtlename,"") + # Only the code of the examples refers to the example object. + newdocstr = re.sub(r'^\s*(?:>>>|\.\.\.).*', + lambda m: m[0].replace("%s." % turtlename, ""), + docstr, flags=re.MULTILINE) parexp = re.compile(r' \(.+ %s\):' % turtlename) newdocstr = parexp.sub(":", newdocstr) return newdocstr @@ -4068,7 +4071,10 @@ def _screen_docrevise(docstr): if docstr is None: return None screenname = _CFG["examplescreen"] - newdocstr = docstr.replace("%s." % screenname,"") + # Only the code of the examples refers to the example object. + newdocstr = re.sub(r'^\s*(?:>>>|\.\.\.).*', + lambda m: m[0].replace("%s." % screenname, ""), + docstr, flags=re.MULTILINE) parexp = re.compile(r' \(.+ %s\):' % screenname) newdocstr = parexp.sub(":", newdocstr) return newdocstr diff --git a/Misc/NEWS.d/next/Library/2026-12-04-18-50-36.gh-issue-156360.MWlux2.rst b/Misc/NEWS.d/next/Library/2026-12-04-18-50-36.gh-issue-156360.MWlux2.rst new file mode 100644 index 00000000000000..15ff983025dc1b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-04-18-50-36.gh-issue-156360.MWlux2.rst @@ -0,0 +1,3 @@ +Fix the docstrings of the :mod:`turtle` functions generated from methods. The +names of the example turtle and screen are now only removed from the example +code, and no longer from the prose.