>  Also I think this is often alleviated by using super().
super doesn't help for my use case or not in any way I can see.

> Maybe it is possible to preserve backwards compatibility by making
ast_list a subclass of list? Or is it not possible for some reason?
There is a layout conflict between AST and list.


Caleb Donovick


On Fri, Aug 16, 2019 at 5:54 AM Ivan Levkivskyi <levkivs...@gmail.com>
wrote:

> On one hand I can see how this may cause little inconvenience, but on
> other hand this would be a breaking change, so I don't think it is
> realistic.
> Also I think this is often alleviated by using super().
>
> Maybe it is possible to preserve backwards compatibility by making
> ast_list a subclass of list? Or is it not possible for some reason?
>
> --
> Ivan
>
>
>
> On Thu, 15 Aug 2019 at 22:32, Caleb Donovick <donov...@cs.stanford.edu>
> wrote:
>
>> When walking an ast it impossible to know the type of an empty list
>> without writing down some giant lookup from node types and field names to
>> field types.
>>
>> More concretely it would nice be to able to programatically visit all
>> blocks (stmt*)  without having to something like:
>>
>> ```
>> class BlockVisitor(NodeVisitor):
>>     def visit_If(self, node: If):
>>         self.visit(node.test)
>>         self.visit_block(node.body)
>>         self.visit_block(node.orelse)
>>
>>     def visit_FunctionDef(self, node: FunctionDef):
>>         for field, value in iter_fields(node):
>>             if field == 'body':
>>                 self.visit_block(value)
>>             else:
>>                 # the implementation of generic_visit
>> ```
>> Now it turns out that all fields that are lists and are named "body",
>> "orelse", or "finalbody" are stmt* and only such fields are stmt*.  A rule
>> could also be synthesized to identify expr* and so forth but this seems
>> incredibly hacky to me.
>>
>> It would be much cleaner if <type>* were actual nodes in the ast. E.g.
>> something like:
>> ```
>> class ast_list(AST, MutableSequence[T_co]): ...
>> class StmtList(ast_list[stmt]): ...
>> class ExprList(ast_list[expr]): ...
>> ...
>> class FunctionDef(stmt):
>>     name: identifier
>>     args: arguments
>>     body: StmtList
>>     decorator_list: ExprList
>>     returns: Optional[expr]
>> ```
>> This would not change the behavior or structure in any way other than
>> tagging <type>* and allowing <type>* to be visited.
>>
>> It would potentially break old code which relies on stuff like `if
>> isinstance(node.field, list)` e.g. the implementation of generic_visit.
>>
>>
>> Caleb Donovick
>>
>> _______________________________________________
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/ZHOXQTDSHOERZSGUJXLNJCYLKKQJOYTA/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LSPTPYDAEGTY2QFUBT6NPLWGPKSN4DQ3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to