Skip to content

Commit 73cbe7a

Browse files
Remove the docstring attribute of AST types and restore docstring expression as a first stmt in their body. Co-authored-by: INADA Naoki <methane@users.noreply.github.com>
1 parent 2179022 commit 73cbe7a

File tree

17 files changed

+3282
-3353
lines changed

17 files changed

+3282
-3353
lines changed

Doc/library/ast.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ and classes for traversing abstract syntax trees:
151151
.. versionchanged:: 3.5
152152
:class:`AsyncFunctionDef` is now supported.
153153

154-
.. versionchanged:: 3.7
155-
The docstring is now exported from the node docstring field, instead of
156-
the first body statement.
157-
158154

159155
.. function:: fix_missing_locations(node)
160156

Doc/whatsnew/3.7.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,13 +2161,6 @@ Changes in Python Behavior
21612161
Changes in the Python API
21622162
-------------------------
21632163

2164-
* ``Module``, ``FunctionDef``, ``AsyncFunctionDef``, and
2165-
``ClassDef`` AST nodes now have the new ``docstring`` attribute.
2166-
The first statement in their body is not considered as a docstring
2167-
anymore. ``co_firstlineno`` and ``co_lnotab`` of code object for class
2168-
and module are affected by this change. (Contributed by INADA Naoki and
2169-
Eugene Toder in :issue:`29463`.)
2170-
21712164
* :meth:`socketserver.ThreadingMixIn.server_close` now waits until all
21722165
non-daemon threads complete. Set the new
21732166
:attr:`socketserver.ThreadingMixIn.block_on_close` class attribute to

Include/Python-ast.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ struct _mod {
4646
union {
4747
struct {
4848
asdl_seq *body;
49-
string docstring;
5049
} Module;
5150

5251
struct {
@@ -81,7 +80,6 @@ struct _stmt {
8180
asdl_seq *body;
8281
asdl_seq *decorator_list;
8382
expr_ty returns;
84-
string docstring;
8583
} FunctionDef;
8684

8785
struct {
@@ -90,7 +88,6 @@ struct _stmt {
9088
asdl_seq *body;
9189
asdl_seq *decorator_list;
9290
expr_ty returns;
93-
string docstring;
9491
} AsyncFunctionDef;
9592

9693
struct {
@@ -99,7 +96,6 @@ struct _stmt {
9996
asdl_seq *keywords;
10097
asdl_seq *body;
10198
asdl_seq *decorator_list;
102-
string docstring;
10399
} ClassDef;
104100

105101
struct {
@@ -443,27 +439,26 @@ struct _withitem {
443439
};
444440

445441

446-
#define Module(a0, a1, a2) _Py_Module(a0, a1, a2)
447-
mod_ty _Py_Module(asdl_seq * body, string docstring, PyArena *arena);
442+
#define Module(a0, a1) _Py_Module(a0, a1)
443+
mod_ty _Py_Module(asdl_seq * body, PyArena *arena);
448444
#define Interactive(a0, a1) _Py_Interactive(a0, a1)
449445
mod_ty _Py_Interactive(asdl_seq * body, PyArena *arena);
450446
#define Expression(a0, a1) _Py_Expression(a0, a1)
451447
mod_ty _Py_Expression(expr_ty body, PyArena *arena);
452448
#define Suite(a0, a1) _Py_Suite(a0, a1)
453449
mod_ty _Py_Suite(asdl_seq * body, PyArena *arena);
454-
#define FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8)
450+
#define FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7) _Py_FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7)
455451
stmt_ty _Py_FunctionDef(identifier name, arguments_ty args, asdl_seq * body,
456-
asdl_seq * decorator_list, expr_ty returns, string
457-
docstring, int lineno, int col_offset, PyArena *arena);
458-
#define AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8)
452+
asdl_seq * decorator_list, expr_ty returns, int lineno,
453+
int col_offset, PyArena *arena);
454+
#define AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7) _Py_AsyncFunctionDef(a0, a1, a2, a3, a4, a5, a6, a7)
459455
stmt_ty _Py_AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq *
460456
body, asdl_seq * decorator_list, expr_ty returns,
461-
string docstring, int lineno, int col_offset,
462-
PyArena *arena);
463-
#define ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8)
457+
int lineno, int col_offset, PyArena *arena);
458+
#define ClassDef(a0, a1, a2, a3, a4, a5, a6, a7) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6, a7)
464459
stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords,
465-
asdl_seq * body, asdl_seq * decorator_list, string
466-
docstring, int lineno, int col_offset, PyArena *arena);
460+
asdl_seq * body, asdl_seq * decorator_list, int lineno,
461+
int col_offset, PyArena *arena);
467462
#define Return(a0, a1, a2, a3) _Py_Return(a0, a1, a2, a3)
468463
stmt_ty _Py_Return(expr_ty value, int lineno, int col_offset, PyArena *arena);
469464
#define Delete(a0, a1, a2, a3) _Py_Delete(a0, a1, a2, a3)

Lib/ast.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,15 @@ def get_docstring(node, clean=True):
206206
"""
207207
if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
208208
raise TypeError("%r can't have docstrings" % node.__class__.__name__)
209-
text = node.docstring
209+
if not node.body:
210+
return None
211+
node = node.body[0].value
212+
if isinstance(node, Str):
213+
text = node.s
214+
elif isinstance(node, Constant) and isinstance(node.value, str):
215+
text = node.value
216+
else:
217+
return None
210218
if clean and text:
211219
import inspect
212220
text = inspect.cleandoc(text)

0 commit comments

Comments
 (0)