backward compatibility library for Python standard library
What is AST-Compat for?
backward compatibility for constructing ASTs
ast.argumentsis changed since Python 3.8, that another field
posonlyargis introduced, hence you should construct an
ast.argumentswith an additional argument
posonlyarg=...since Python 3.8 and it wouldn't work if your code is written before Python 3.8.
ast_compat, you don't have to worry about above question if a new argument is optional or a list.
Note that you should use
ast.XXXto construct ASTs.
Supporting ast.Constant before Python 3.6.
ast.Constantis convenient, and things like
ast.Numare redundant according to this observation and improvement: https://bugs.python.org/issue32892
ast.Constantis not available in 3.5 or earlier versions, thus we backport
ast.Constantin this library.
To access the content of
ast.Constantin a compatible way, use
import ast_compat as astc from ast_compat import get_constant assert get_constant(astc.Constant((1, 2))) == (1, 2) empty_args = astc.arguments() # work for all of Python 3.5-3.9
Implementation, and rebuild
The compatibility is following the specification of Python ASTs, i.e, the ADSL file you can find at
We parse the file, and generate verifications and default argument factories,
ast_compat/compat3k*.py. To explain,
ast_compat/compat3k5.py is for
Python 3.5, and
ast_compat/compat3k9.py is for Python 3.9.
The file of generator is
generate_ast_compat.py, and the use of generator API is in this way:
from generate_ast_compat import compat compat((3, 5)) # generate ast_compat/compat3k5.py compat((3, 6)) compat((3, 7)) compat((3, 8)) prerelease_url = "https://raw.githubusercontent.com/python/cpython/v3.9.0a3/parser/python.asdl" compat((3, 9), prerelease_url)
The code generation needs Python 3.7+, though this library works for Python 3.5 and 3.5+.