Type checking on all expressions

28 views
Skip to first unread message

Rocky Bernstein

unread,
Jul 29, 2020, 8:26:08 PM7/29/20
to pytype
Recently, I have crude translator from Mathematica input to Python, based on Robert Jacobson's FoxySheep. See FoxySheep2 

Mathematica "InputForm" string input is parsed to an "FullForm" parse tree, and then using that to convert to a Python AST. Finally a Python module is used to dump this to text.

However in order to do better translations, types for the parts of the AST would be helpful. 

For example, in Mathematica: 
  {1, 2, 3} + 2  

could be translated to Python as 

 [i + 2 for i in [1, 2, 3]] 

 And obviously not: 

 [1, 2, 3] + 2 

I looked at annotate-ast and that seems in spirit what I want to use. However when I tried it, it seems focused on variables and call signatures, not expressions as are found above. (Also, I'd hook into an API from Python rather than that command-line utility) 

So my question is about to what extent this is being contemplated in this project. Should I should try extending this from the outside? 

 Any other thoughts?

Rebecca Chen

unread,
Jul 31, 2020, 8:26:09 PM7/31/20
to Rocky Bernstein, pytype
Hi Rocky,

annotate-ast is an incomplete tool that unfortunately does not have anyone working full-time on it or any timeline for completion, due to limited resources. The eventual goal is to be able to annotate all AST nodes, so that would include expressions as you want. You can call it from Python using annotate_ast.annotate_source: https://github.com/google/pytype/blob/e2d73f1e0be6f1c6a22f040e785e5e2e348d0cf5/pytype/tools/annotate_ast/annotate_ast.py#L12.

External contributions are always welcome! If you're interested in extending annotated_ast, my recommendation would be to run the command-line tool over some (simple) piece of code containing the types of features you're interested in annotating, see what nodes in the output are missing from https://github.com/google/pytype/blob/e2d73f1e0be6f1c6a22f040e785e5e2e348d0cf5/pytype/tools/annotate_ast/annotate_ast.py#L56, and add new visit_X methods for them. Let me know if you want to know more about how the visitor works or how to extend it.

Best,
Rebecca

--
You received this message because you are subscribed to the Google Groups "pytype" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pytype+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pytype/7e7c1e77-173f-4f05-91da-121aea291634n%40googlegroups.com.

Rocky Bernstein

unread,
Aug 1, 2020, 6:36:07 AM8/1/20
to pytype
Ok. Thanks for the information. 

I am all too familiar with the underfunded and overcommitted aspect. 

We may have race to see which project progresses slowest. However, when working on extending using types, I'll consider adding to the visitor functions in pytype. I believe I could problably subclass that from the outside, while also having PR's to extend from inside the package. 
Reply all
Reply to author
Forward
0 new messages