[New-bugs-announce] [issue39463] ast.Constant, bytes, and ast.unparse

Tal Ben-Nun report at bugs.python.org
Mon Jan 27 06:40:03 EST 2020

New submission from Tal Ben-Nun <tbennun at gmail.com>:

In Python 3.8, the "kind" field was introduced into the Constant AST class. This brings about a problem when unparsing the AST for various packages. First, it breaks backward compatibility for older code that creates ast.Num without specifying kind (which is optional anyway and does not exist in its fields). Second, since bytes are parsed as a Constant without a kind, one can create the following (valid as of now) AST and unparse it:

ast.unparse(ast.Constant(value=b"bad", kind="u"))

Getting "ub'bad'", which is invalid Python syntax AFAIU.

Could something be done with the classes that extend ast.Constant and with bytes being a Constant with a "kind" of "b"?

components: Library (Lib)
messages: 360754
nosy: Tal Ben-Nun
priority: normal
severity: normal
status: open
title: ast.Constant, bytes, and ast.unparse
versions: Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list