classification
Title: Default values for AST Nodes
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, benjamin.peterson, pablogsal, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-03-16 19:17 by BTaskaya, last changed 2020-03-16 19:53 by BTaskaya.

Pull Requests
URL Status Linked Edit
PR 19031 open BTaskaya, 2020-03-16 19:53
Messages (1)
msg364355 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python triager) Date: 2020-03-16 19:17
For omitting some defaults, @serhiy.storchaka already added support to initialize some ast nodes with some default values (optional fields). An example;
>>> ast.Constant().kind is None
True

This isn't exactly a default value, but some kind of class attribute. I think we can push this one step further and initialize all kinds of default values (both optionals and sequences). An example;
>>> func = ast.FunctionDef("easy_func", ast.arguments(), body=[ast.Pass()])
>>> func = ast.fix_missing_locations(func)
>>> exec(compile(ast.Module(body=[func]), "<stdin>", "exec"))
>>> easy_func()

compared to this (other way around, compiler gives errors so does most of the ast based tool, including ast.unparser)
>>> func = ast.FunctionDef("easy_func", ast.arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[]), decorator_list=[], body=[ast.Pass()])
>>> func = ast.fix_missing_locations(func)
>>> exec(compile(ast.Module(body=[func], type_ignores=[]), "<stdin>", "exec"))
>>> easy_func()
History
Date User Action Args
2020-05-03 12:15:12BTaskayalinkissue40483 dependencies
2020-04-08 14:28:27BTaskayalinkissue36287 dependencies
2020-03-16 19:53:55BTaskayasetkeywords: + patch
stage: patch review
pull_requests: + pull_request18380
2020-03-16 19:17:20BTaskayacreate