This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 3.7.0b5 changes the line number of empty functions with docstrings
Type: Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, methane, miss-islington, ncoghlan, ned.deily, nedbat, serhiy.storchaka
Priority: Keywords: 3.7regression, patch

Created on 2018-06-03 11:11 by nedbat, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7611 merged ned.deily, 2018-06-11 02:35
PR 7612 merged miss-islington, 2018-06-11 02:42
Messages (8)
msg318532 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2018-06-03 11:11
I'm not sure if this is a regression or an intentional change.  I know that the behavior has changed.

If a function has a docstring but no other body, Python 3.7b5 assigns the line number of the docstring to the implicit "return None".  Previous versions (including 3.7b4) used the line number of the "def".

Demonstration:

$ cat /tmp/empty.py
def empty():
    pass

def empty_with_docstring():
    '''Docstring'''

def docstring():
    '''Docstring'''
    return 1

import dis, sys

print(sys.version)

for fn in [empty, empty_with_docstring, docstring]:
    print(fn.__name__)
    dis.dis(fn)

$ /usr/local/pythonz/pythons/CPython-2.7.14/bin/python2.7 /tmp/empty.py
2.7.14 (default, Oct  4 2017, 09:45:53)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]
empty
  2           0 LOAD_CONST               0 (None)
              3 RETURN_VALUE
empty_with_docstring
  4           0 LOAD_CONST               1 (None)
              3 RETURN_VALUE
docstring
  9           0 LOAD_CONST               1 (1)
              3 RETURN_VALUE

$ /usr/local/pythonz/pythons/CPython-3.6.4/bin/python3.6 /tmp/empty.py
3.6.4 (default, Dec 19 2017, 08:11:42)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
empty
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
empty_with_docstring
  4           0 LOAD_CONST               1 (None)
              2 RETURN_VALUE
docstring
  9           0 LOAD_CONST               1 (1)
              2 RETURN_VALUE

$ /usr/local/pythonz/pythons/CPython-3.7.0b4/bin/python3.7 /tmp/empty.py
3.7.0b4 (default, May  2 2018, 21:07:21)
[Clang 9.0.0 (clang-900.0.39.2)]
empty
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
empty_with_docstring
  4           0 LOAD_CONST               1 (None)
              2 RETURN_VALUE
docstring
  9           0 LOAD_CONST               1 (1)
              2 RETURN_VALUE

$ /usr/local/pythonz/pythons/CPython-3.7.0b5/bin/python3.7 /tmp/empty.py
3.7.0b5 (default, Jun  2 2018, 11:27:19)
[Clang 9.1.0 (clang-902.0.39.2)]
empty
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
empty_with_docstring
  5           0 LOAD_CONST               1 (None)
              2 RETURN_VALUE
docstring
  9           0 LOAD_CONST               1 (1)
              2 RETURN_VALUE
msg318677 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-06-04 17:11
Setting to deferred blocker for evaluation
msg318761 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-06-05 14:41
While I wouldn't describe this as completely intentional (as it's an artifact of the change-and-revert dance with 3.7 previously removing docstring nodes from the AST entirely), I also wouldn't want to change it again at this late stage of the release process.

Instead I'd suggest simply noting it in the porting section of What's New. Something like "Due to a change in the way docstrings are handled by the compiler, the implicit ``return None`` in a function body consisting solely of a docstring is now marked as occurring on the same line as the docstring, not on the function's header line".
msg319020 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-06-08 05:45
This change looks not intentional, but not incorrect. I'm not sure that it is worth to document it. Any behavior (in 3.6 and in 3.7) is CPython implementation detail.
msg319064 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-06-08 14:00
The rationale for documenting it in the porting section is that even though this isn't a guaranteed stable interface, the output *does* potentially affect development tools like Ned's coverage.py, as well as other implementations attempting to adhere closely to CPython's behaviour for any given version.

(Although it doesn't usually help *Ned* much, since he's often the one letting us know that we inadvertently changed some details of the code generator's output...)
msg319268 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-06-11 02:41
New changeset 12c6cdf4d16078aa09de32a39193c8161177b39d by Ned Deily in branch 'master':
bpo-33745: Add What's New for empty function docstring change. (GH-7611)
https://github.com/python/cpython/commit/12c6cdf4d16078aa09de32a39193c8161177b39d
msg319269 - (view) Author: miss-islington (miss-islington) Date: 2018-06-11 03:00
New changeset 14a190c88273fb22d9439bbed394f19f21e8a0f9 by Miss Islington (bot) in branch '3.7':
bpo-33745: Add What's New for empty function docstring change. (GH-7611)
https://github.com/python/cpython/commit/14a190c88273fb22d9439bbed394f19f21e8a0f9
msg319270 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-06-11 03:02
The 3.7 What's New has been updated as Nick suggested.  Thanks, Nick, and thanks, Ned, for bringing it up!
History
Date User Action Args
2022-04-11 14:59:01adminsetgithub: 77926
2018-06-11 03:02:20ned.deilysetstatus: open -> closed
priority: deferred blocker ->
messages: + msg319270

resolution: fixed
stage: patch review -> resolved
2018-06-11 03:00:19miss-islingtonsetnosy: + miss-islington
messages: + msg319269
2018-06-11 02:42:24miss-islingtonsetpull_requests: + pull_request7237
2018-06-11 02:41:17ned.deilysetmessages: + msg319268
2018-06-11 02:35:07ned.deilysetkeywords: + patch
stage: patch review
pull_requests: + pull_request7236
2018-06-08 14:00:56ncoghlansetnosy: + docs@python
messages: + msg319064

assignee: docs@python
components: + Documentation
2018-06-08 05:45:32serhiy.storchakasetmessages: + msg319020
2018-06-05 14:41:54ncoghlansetnosy: + serhiy.storchaka
messages: + msg318761
2018-06-04 17:11:39ned.deilysetpriority: normal -> deferred blocker
versions: + Python 3.8
nosy: + ned.deily, ncoghlan, methane

messages: + msg318677
2018-06-03 11:11:49nedbatcreate