[New-bugs-announce] [issue32892] Remove specific constant AST types in favor of ast.Constant

Serhiy Storchaka report at bugs.python.org
Wed Feb 21 04:16:37 EST 2018


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

Currently different literals are represented by different types in AST:

Num -- for int, float and complex
Str -- for str
Bytes -- for bytes
Ellipsis -- for Ellipsis
NameConstant -- for True, False and None

And Constant (added in 3.6, issue26146) can represent any immutable value, including tuples and frozensets of constants. Instances of Constant are not created by the AST parser, they are created by the builtin AST optimizer and can be created manually.

These AST types don't have one-to-one correspondence with Python types, since Num represents three numeric types, NameConstant represents bool and NoneType, and any constant type can be represented as Constant.

I propose to get rid of Num, Str, Bytes, Ellipsis and NameConstant and always use Constant. This will simplify the code which currently needs to repeat virtually identical code for all types.

I have almost ready patch, the only question is whether it is worth to keep deprecated aliases Num, Str, Bytes, Ellipsis and NameConstant.

----------
components: Interpreter Core
messages: 312482
nosy: benjamin.peterson, brett.cannon, gvanrossum, ncoghlan, serhiy.storchaka, vstinner, yselivanov
priority: normal
severity: normal
status: open
title: Remove specific constant AST types in favor of ast.Constant
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32892>
_______________________________________


More information about the New-bugs-announce mailing list