Skip to content
Open
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
67 changes: 67 additions & 0 deletions Lib/test/test_turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
" <turtle.Turtle object at 0x...>\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"
" <turtle.Turtle object at 0x...>\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):
Expand Down
10 changes: 8 additions & 2 deletions Lib/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading