Reminder: an oft-forgotten rule about docstring formatting (PEP 257)
PEP 257 says this on the formatting of multi-line docstrings: """ Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line. [...] """ I still like this rule, but it is violated frequently, in the stdlib and elsewhere. I'd like to urge stdlib contributors and core devs to heed it -- or explain why you can't. -- --Guido van Rossum (python.org/~guido)
I'd like to urge stdlib contributors and core devs to heed it -- or explain why you can't.
Where I can, I do, however I often find it difficult to come up with a one-liner, especially one that mentions the parameters to functions. If the one-line rule is going to be violated, I go whole hog and don't bother with the blank line either. (If I'm going to punch a hole in a rule, I might as well create one big enough to comfortably walk through.) Skip
On 06/26/2013 07:08 PM, Skip Montanaro wrote:
I'd like to urge stdlib contributors and core devs to heed it -- or explain why you can't.
Where I can, I do, however I often find it difficult to come up with a one-liner, especially one that mentions the parameters to functions. If the one-line rule is going to be violated, I go whole hog and don't bother with the blank line either. (If I'm going to punch a hole in a rule, I might as well create one big enough to comfortably walk through.)
Why do the parameters have to be in the summary? You could do them better justice in the following paragraphs. -- ~Ethan~
Skip Montanaro
Where I can [conform to PEP 257], I do, however I often find it difficult to come up with a one-liner, especially one that mentions the parameters to functions.
I think it's far better to make the synopsis a summary of the purpose of the function; no need to force the parameters in there if they don't fit naturally in the sentence fragment. A list of the parameters and return value can go in the detailed description. -- \ “Theology is the effort to explain the unknowable in terms of | `\ the not worth knowing.” —Henry L. Mencken | _o__) | Ben Finney
Le Wed, 26 Jun 2013 18:56:10 -0700,
Guido van Rossum
PEP 257 says this on the formatting of multi-line docstrings:
""" Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line. [...] """
I still like this rule, but it is violated frequently, in the stdlib and elsewhere. I'd like to urge stdlib contributors and core devs to heed it -- or explain why you can't.
I don't always find it easy to summarize a function in one line. Regards Antoine.
On Thu, Jun 27, 2013 at 1:21 AM, Antoine Pitrou
I don't always find it easy to summarize a function in one line.
Neither do I. But I always manage to do it anyway. My reasoning is, you can always leave out more detail and add it to the subsequent paragraph. -- --Guido van Rossum (python.org/~guido)
On Thu, Jun 27, 2013 at 10:20 AM, Guido van Rossum
On Thu, Jun 27, 2013 at 1:21 AM, Antoine Pitrou
wrote: I don't always find it easy to summarize a function in one line.
Neither do I. But I always manage to do it anyway.
+1 If you cannot summarize what your function does in one line, chances are it is time to split your function, not the summary line. Ideally, the name of the function should already give a good idea of what it does. A summary line can just rephrase the same in a complete sentence. I took a quick look at the stdlib and in many cases a summary line is already there. It is just the issue of formatting. 250http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/suite.py#l250 class _ErrorHolder(object): 251http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/suite.py#l251 """ 252http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/suite.py#l252 Placeholder for a TestCase inside a result. As far as a TestResult 253http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/suite.py#l253 is concerned, this looks exactly like a unit test. Used to insert 254http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/suite.py#l254 arbitrary errors into a test suite run. 255http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/suite.py#l255 """ *http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/suite.py#l250* 81 http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/main.py#l81 class TestProgram(object): 82 http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/main.py#l82 """A command-line program that runs a set of tests; this is primarily 83 http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/main.py#l83 for making test modules conveniently executable. 84 http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/main.py#l84 """ *http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/main.py#l81* 109http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l109 @failfast 110http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l110 def addError(self, test, err): 111http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l111 """Called when an error has occurred. 'err' is a tuple of values as 112http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l112 returned by sys.exc_info(). 113http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l113 """ 114http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l114 self.errors.append((test, self._exc_info_to_string(err, test))) 115http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l115 self._mirrorOutput = True 116http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l116 117http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l117 @failfast 118http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l118 def addFailure(self, test, err): 119http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l119 """Called when an error has occurred. 'err' is a tuple of values as 120http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l120 returned by sys.exc_info().""" 121http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l121 self.failures.append((test, self._exc_info_to_string(err, test))) 122http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l122 self._mirrorOutput = True 123http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l123 124http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l124 @failfast 125http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l125 def addSubTest(self, test, subtest, err): 126http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l126 """Called at the end of a subtest. 127http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l127 'err' is None if the subtest ended successfully, otherwise it's a 128http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l128 tuple of values as returned by sys.exc_info(). 129http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l129 """ *http://hg.python.org/cpython/file/44f455e6163d/Lib/unittest/result.py#l109* * * 589 http://hg.python.org/cpython/file/44f455e6163d/Lib/pdb.py#l589 def do_break(self, arg, temporary = 0): 590 http://hg.python.org/cpython/file/44f455e6163d/Lib/pdb.py#l590 """b(reak) [ ([filename:]lineno | function) [, condition] ] 591 http://hg.python.org/cpython/file/44f455e6163d/Lib/pdb.py#l591 Without argument, list all breaks. *http://hg.python.org/cpython/file/44f455e6163d/Lib/pdb.py#l589*
Antoine Pitrou
I don't always find it easy to summarize a function in one line.
Difficulty in coming up with an accurate one-line summary of the intent of a function is a code smell: the function probably needs to be simplified so it is easier to describe accurately. This is, IMO, one of the primary benefits of having a convention of expecting a one-line summary for every function (and module and class). -- \ “Courage is not the absence of fear, but the decision that | `\ something else is more important than fear.” —Ambrose Redmoon | _o__) | Ben Finney
On 06/26/2013 08:56 PM, Guido van Rossum wrote:
PEP 257 says this on the formatting of multi-line docstrings:
""" Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line. [...] """
I still like this rule, but it is violated frequently, in the stdlib and elsewhere. I'd like to urge stdlib contributors and core devs to heed it -- or explain why you can't.
Argument Clinic could conceivably enforce this. It could mandate that the first paragraph of the function docstring contain exactly one sentence (must end in a period, all embedded periods cannot be followed by whitespace). This would make some things nicer; I could automatically insert the per-parameter docstrings in after the summary. Should it? //arry/
Yes on one line, capitalized, period. No on single sentence.
--Guido van Rossum (sent from Android phone)
On Jun 27, 2013 8:17 AM, "Larry Hastings"
On 06/26/2013 08:56 PM, Guido van Rossum wrote:
PEP 257 says this on the formatting of multi-line docstrings:
""" Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line. [...] """
I still like this rule, but it is violated frequently, in the stdlib and elsewhere. I'd like to urge stdlib contributors and core devs to heed it -- or explain why you can't.
Argument Clinic could conceivably enforce this. It could mandate that the first paragraph of the function docstring contain exactly one sentence (must end in a period, all embedded periods cannot be followed by whitespace). This would make some things nicer; I could automatically insert the per-parameter docstrings in after the summary.
Should it?
*/arry*
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
On 6/27/2013 11:57 AM, Guido van Rossum wrote:
Yes on one line, capitalized, period. No on single sentence.
Complete and correct docstrings are somewhat rare in idlelib. About half are missing. Single lines typically omit the period. Multiple lines often omit the blank line after the first. I will take your reminder as permission to make changes as I review the code. I wish there were more good summary lines already. My understanding is that SubclassTests and test_methods should not have docstrings. -- Terry Jan Reedy
It was never my intention to enforce that everything has a docstring.
Just that if it does, it looks good.
On Thu, Jun 27, 2013 at 9:40 AM, Terry Reedy
On 6/27/2013 11:57 AM, Guido van Rossum wrote:
Yes on one line, capitalized, period. No on single sentence.
Complete and correct docstrings are somewhat rare in idlelib. About half are missing. Single lines typically omit the period. Multiple lines often omit the blank line after the first.
I will take your reminder as permission to make changes as I review the code. I wish there were more good summary lines already.
My understanding is that SubclassTests and test_methods should not have docstrings.
-- Terry Jan Reedy
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (python.org/~guido)
On 6/27/2013 12:57 PM, Guido van Rossum wrote:
It was never my intention to enforce that everything has a docstring. Just that if it does, it looks good.
Ok, I won't add them when a function's name actually makes what it does obvious. But when I have to spend at least a few minutes reading the body to make sense of it, I will try to add the summary line that I wish had been there already. -- Terry Jan Reedy
Terry Reedy writes:
Ok, I won't add them when a function's name actually makes what it does obvious. But when I have to spend at least a few minutes reading the body to make sense of it, I will try to add the summary line that I wish had been there already.
+1 That's a *great* rule-of-thumb!
On 6/26/2013 9:56 PM, Guido van Rossum wrote:
PEP 257 says this on the formatting of multi-line docstrings:
""" Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description.
fileinput has docstrings like """ Return the file number of the current file. When no file is currently opened, returns -1. """ and, perhaps neater, """ Return the name of the file currently being read. Before the first line has been read, returns None. """ From the above, I presume these should become """ Return the file number of the current file. When no file is currently opened, returns -1. """ and """ Return the name of the file currently being read. Before the first line has been read, returns None. """ -- Terry Jan Reedy
participants (9)
-
Alexander Belopolsky
-
Antoine Pitrou
-
Ben Finney
-
Ethan Furman
-
Guido van Rossum
-
Larry Hastings
-
Skip Montanaro
-
Stephen J. Turnbull
-
Terry Reedy