bpo-35766: Change format for feature_version to (major, minor) (GH-13… · python/cpython@3ba2107 · GitHub
Skip to content

Commit 3ba2107

Browse files
miss-islingtongvanrossum
authored andcommitted
bpo-35766: Change format for feature_version to (major, minor) (GH-13992) (GH-13993)
(A single int is still allowed, but undocumented.) https://bugs.python.org/issue35766 (cherry picked from commit 10b55c1) Co-authored-by: Guido van Rossum <guido@python.org>
1 parent 36eea7a commit 3ba2107

4 files changed

Lines changed: 19 additions & 10 deletions

File tree

Doc/library/ast.rst

Lines changed: 7 additions & 6 deletions

Doc/whatsnew/3.8.rst

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

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

359359
New function :func:`ast.get_source_segment` returns the source code
360360
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

0 commit comments

Comments
 (0)