Skip to content

Commit 04513ad

Browse files
Only strip turtle./screen. from example code in turtle docstrings
1 parent 11a14db commit 04513ad

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

‎Lib/test/test_turtle.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,73 @@ def test_all_signatures(self):
742742
self.assertEqual(str(sig), known_signatures[name])
743743

744744

745+
class TestFunctionDocstrings(unittest.TestCase):
746+
747+
def test_turtle_docrevise(self):
748+
doc = (
749+
"Undo the last action of the turtle.\n"
750+
"\n"
751+
"Example (for a Turtle instance named turtle):\n"
752+
" >>> turtle.fd(50); turtle.lt(80)\n"
753+
" >>> for i in range(2):\n"
754+
" ... turtle.undo()\n"
755+
" ...\n"
756+
" >>> turtle.getturtle()\n"
757+
" <turtle.Turtle object at 0x...>\n"
758+
"\n"
759+
"This does not move any other turtle.\n"
760+
)
761+
expected = (
762+
"Undo the last action of the turtle.\n"
763+
"\n"
764+
"Example:\n"
765+
" >>> fd(50); lt(80)\n"
766+
" >>> for i in range(2):\n"
767+
" ... undo()\n"
768+
" ...\n"
769+
" >>> getturtle()\n"
770+
" <turtle.Turtle object at 0x...>\n"
771+
"\n"
772+
"This does not move any other turtle.\n"
773+
)
774+
self.assertEqual(turtle._turtle_docrevise(doc), expected)
775+
self.assertIsNone(turtle._turtle_docrevise(None))
776+
777+
def test_screen_docrevise(self):
778+
doc = (
779+
"Reset all turtles on the screen.\n"
780+
"\n"
781+
"This method is only available under the name\n"
782+
"resetscreen. See also clearscreen.\n"
783+
"\n"
784+
"Example (for a TurtleScreen instance named screen):\n"
785+
" >>> screen.resetscreen()\n"
786+
)
787+
expected = (
788+
"Reset all turtles on the screen.\n"
789+
"\n"
790+
"This method is only available under the name\n"
791+
"resetscreen. See also clearscreen.\n"
792+
"\n"
793+
"Example:\n"
794+
" >>> resetscreen()\n"
795+
)
796+
self.assertEqual(turtle._screen_docrevise(doc), expected)
797+
self.assertIsNone(turtle._screen_docrevise(None))
798+
799+
@support.requires_docstrings
800+
def test_summary_lines(self):
801+
# The summary line of a function is that of its method.
802+
for cls, names in [(turtle.Turtle, turtle._tg_turtle_functions),
803+
(turtle._Screen, turtle._tg_screen_functions)]:
804+
for name in names:
805+
with self.subTest(name=name):
806+
method_doc = getattr(cls, name).__doc__
807+
func_doc = getattr(turtle, name).__doc__
808+
self.assertEqual(func_doc.partition('\n')[0],
809+
method_doc.partition('\n')[0])
810+
811+
745812
class TurtleDocstringTranslationTest(unittest.TestCase):
746813

747814
def _make_translation(self, dirname, filename, docstring):

‎Lib/turtle.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4057,7 +4057,10 @@ def _turtle_docrevise(docstr):
40574057
if docstr is None:
40584058
return None
40594059
turtlename = _CFG["exampleturtle"]
4060-
newdocstr = docstr.replace("%s." % turtlename,"")
4060+
# Only the code of the examples refers to the example object.
4061+
newdocstr = re.sub(r'^\s*(?:>>>|\.\.\.).*',
4062+
lambda m: m[0].replace("%s." % turtlename, ""),
4063+
docstr, flags=re.MULTILINE)
40614064
parexp = re.compile(r' \(.+ %s\):' % turtlename)
40624065
newdocstr = parexp.sub(":", newdocstr)
40634066
return newdocstr
@@ -4068,7 +4071,10 @@ def _screen_docrevise(docstr):
40684071
if docstr is None:
40694072
return None
40704073
screenname = _CFG["examplescreen"]
4071-
newdocstr = docstr.replace("%s." % screenname,"")
4074+
# Only the code of the examples refers to the example object.
4075+
newdocstr = re.sub(r'^\s*(?:>>>|\.\.\.).*',
4076+
lambda m: m[0].replace("%s." % screenname, ""),
4077+
docstr, flags=re.MULTILINE)
40724078
parexp = re.compile(r' \(.+ %s\):' % screenname)
40734079
newdocstr = parexp.sub(":", newdocstr)
40744080
return newdocstr
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the docstrings of the :mod:`turtle` functions generated from methods. The
2+
names of the example turtle and screen are now only removed from the example
3+
code, and no longer from the prose.

0 commit comments

Comments
 (0)