Skip to content

Commit 10b55c1

Browse files
gvanrossummiss-islington
authored andcommitted
bpo-35766: Change format for feature_version to (major, minor) (GH-13992)
(A single int is still allowed, but undocumented.) https://bugs.python.org/issue35766
1 parent 04856c2 commit 10b55c1

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

Doc/library/ast.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ The abstract grammar is currently defined as follows:
126126
Apart from the node classes, the :mod:`ast` module defines these utility functions
127127
and classes for traversing abstract syntax trees:
128128

129-
.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=-1)
129+
.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None)
130130

131131
Parse the source into an AST node. Equivalent to ``compile(source,
132132
filename, mode, ast.PyCF_ONLY_AST)``.
@@ -145,11 +145,12 @@ and classes for traversing abstract syntax trees:
145145
modified to correspond to :pep:`484` "signature type comments",
146146
e.g. ``(str, int) -> List[str]``.
147147

148-
Also, setting ``feature_version`` to the minor version of an
149-
earlier Python 3 version will attempt to parse using that version's
150-
grammar. For example, setting ``feature_version=4`` will allow
151-
the use of ``async`` and ``await`` as variable names. The lowest
152-
supported value is 4; the highest is ``sys.version_info[1]``.
148+
Also, setting ``feature_version`` to a tuple ``(major, minor)``
149+
will attempt to parse using that Python version's grammar.
150+
Currently ``major`` must equal to ``3``. For example, setting
151+
``feature_version=(3, 4)`` will allow the use of ``async`` and
152+
``await`` as variable names. The lowest supported version is
153+
``(3, 4)``; the highest is ``sys.version_info[0:2]``.
153154

154155
.. warning::
155156
It is possible to crash the Python interpreter with a

Doc/whatsnew/3.8.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ The :func:`ast.parse` function has some new flags:
378378
* ``mode='func_type'`` can be used to parse :pep:`484` "signature type
379379
comments" (returned for function definition AST nodes);
380380

381-
* ``feature_version=N`` allows specifying the minor version of an
382-
earlier Python 3 version. (For example, ``feature_version=4`` will
383-
treat ``async`` and ``await`` as non-reserved words.)
381+
* ``feature_version=(3, N)`` allows specifying an earlier Python 3
382+
version. (For example, ``feature_version=(3, 4)`` will treat
383+
``async`` and ``await`` as non-reserved words.)
384384

385385
New function :func:`ast.get_source_segment` returns the source code
386386
for a specific AST node.

Lib/ast.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
def parse(source, filename='<unknown>', mode='exec', *,
31-
type_comments=False, feature_version=-1):
31+
type_comments=False, feature_version=None):
3232
"""
3333
Parse the source into an AST node.
3434
Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
@@ -37,6 +37,13 @@ def parse(source, filename='<unknown>', mode='exec', *,
3737
flags = PyCF_ONLY_AST
3838
if type_comments:
3939
flags |= PyCF_TYPE_COMMENTS
40+
if isinstance(feature_version, tuple):
41+
major, minor = feature_version # Should be a 2-tuple.
42+
assert major == 3
43+
feature_version = minor
44+
elif feature_version is None:
45+
feature_version = -1
46+
# Else it should be an int giving the minor version for 3.x.
4047
return compile(source, filename, mode, flags,
4148
feature_version=feature_version)
4249

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Change the format of feature_version to be a (major, minor) tuple.

0 commit comments

Comments
 (0)