Exploring Alternatives with the Standard Library ast

Blake Bradford Avatar

·

Typed_ast, a Python 3 package that provides a Python 2.7 and Python 3 parser similar to the standard ast library, has reached its end of life. As of now, the project is no longer maintained. The recommendation to users is to use the standard library ast module instead. In this article, we will discuss the reasons behind this decision and explore the alternatives available.

Development Philosophy

Typed_ast was intended to be a bug-for-bug compatible, mostly drop-in replacement for the builtin ast module. The primary difference was the inclusion of PEP 484 type comments and independence from the version of Python. Therefore, bug fixes for ast were not accepted in typed_ast, and any new features for typed_ast had to have broad utility.

Incompatibilities and Python 3.8

While typed_ast aimed to be a drop-in replacement for consuming syntax trees, it was not intended as a drop-in replacement for users creating or transforming ASTs. Syntax tree classes in typed_ast have additional fields that need to be populated when constructing them.

Support for Python 3.8 and newer will not be added to typed_ast. It is recommended to use the stdlib ast module, which has augmented support for extracting type comments and limited support for parsing older versions of Python 3.

Ast3 and Ast27

The typed_ast package consists of two submodules: ast3 and ast27.

Ast3 produces the AST from Python 3 code, up to Python 3.7. It includes a module docstring in typed_ast/ast3.py that describes the AST it produces. If you need to limit parsing to older versions of Python 3, ast3 can be configured to give a SyntaxError for new syntax features beyond a given Python version.

Ast27 tracks the standard Python 2.7 AST and is not expected to receive further updates. The AST it produces is described in typed_ast/ast27.py.

Conversions

Typed_ast also provides a conversions module that converts ast27 ASTs into ast3 ASTs. This functionality is somewhat experimental. More information can be found in the py2to3 docstring in typed_ast/conversions.py.

Conclusion

With the end of life for typed_ast, users are encouraged to transition to the standard library ast module. This article discussed the reasons behind the discontinuation of typed_ast and gave insights into using the standard library ast. We also explored the submodules ast3 and ast27, along with the conversions module. As a software engineer or solution architect, it is important to stay up to date with changes like these to ensure smooth transitions and maintain compatibility with the latest technologies.

If you have any questions or require further information, please leave a comment below.

References

  1. Typed AST Repository
  2. PEP 484 – Type Hints

Leave a Reply

Your email address will not be published. Required fields are marked *