Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:

PS2 = "... " is defined with a trailing space which is not stripped for empty 
lines with only PS2 in the doctest. A patch would be to strip the trailing 
space in PS2 for empty lines and a unittest would be as below. There are no 
test cases for this scenario asserting against exact format and hence the patch 
doesn't break any tests. Currently I don't see any doctest in the codebase 
having this leading whitespace. I can try converting this patch as PR if 
Benjamin is okay with the change.


diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py
index 7841b99a5c..135678b46e 100644
--- a/Lib/lib2to3/refactor.py
+++ b/Lib/lib2to3/refactor.py
@@ -599,7 +599,11 @@ class RefactoringTool(object):
                 new[-1] += "\n"
             block = [indent + self.PS1 + new.pop(0)]
             if new:
-                block += [indent + self.PS2 + line for line in new]
+                for line in new:
+                    if not line.strip():
+                        block += [indent + self.PS2.strip() + line]
+                    else:
+                        block += [indent + self.PS2 + line]
         return block
 
     def summarize(self):
diff --git a/Lib/lib2to3/tests/test_refactor.py 
b/Lib/lib2to3/tests/test_refactor.py
index 9e3b8fbb90..f6a5e2d589 100644
--- a/Lib/lib2to3/tests/test_refactor.py
+++ b/Lib/lib2to3/tests/test_refactor.py
@@ -331,3 +331,22 @@ from __future__ import print_function"""
                 break
         else:
             self.fail("explicit fixer not loaded")
+
+    def test_refactor_doctest(self):
+        rt = self.rt()
+
+        expected = """
+>>> class Foo():
+...    def cheese(self):
+...        pass
+...
+"""
+
+        doc = """
+>>> class Foo():
+...    def parrot(self):
+...        pass
+...
+"""
+        out = rt.refactor_docstring(doc, "<test>")
+        self.assertEqual(out, expected)


Without patch this fails as below : 

$ ./python.exe -m unittest -v 
lib2to3.tests.test_refactor.TestRefactoringTool.test_refactor_doctest
test_refactor_doctest (lib2to3.tests.test_refactor.TestRefactoringTool) ... FAIL

======================================================================
FAIL: test_refactor_doctest (lib2to3.tests.test_refactor.TestRefactoringTool)
----------------------------------------------------------------------
Traceback (most recent call last):
  File 
"/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/lib2to3/tests/test_refactor.py",
 line 352, in test_refactor_doctest
    self.assertEqual(out, expected)
AssertionError: '\n>>> class Foo():\n...    def cheese(self):\n...        
pass\n... \n' != '\n>>> class Foo():\n...    def cheese(self):\n...        
pass\n...\n'

  >>> class Foo():
  ...    def cheese(self):
  ...        pass
- ...
?    -
+ ...


----------------------------------------------------------------------
Ran 1 test in 0.032s

FAILED (failures=1)

----------
nosy: +xtreak
versions: +Python 3.7, Python 3.8 -Python 3.4, Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue12771>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to