API to get type-annotated AST
See original GitHub issueIs there an existing API, that would let me programmatically analyze my Python project?
If there’s not one yet, I could work on one if only team can point which classes could provide such a thing, and, if possible, what to do to have a minimal setup to get them up and running on a specific Python module.
To give some background: I was already able to get PythonInterpreterFactoryWithDatabase.GenerateDatabase
complete successfully from a console app (exitCode == 0) on my Python 3.6 installation without passing path to the module I want to analyze yet.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
TypeAnnotation class - ast library - Dart API
Return an iterator that can be used to iterate through all the entities (either AST nodes or tokens) that make up the contents...
Read more >nikic/php-ast: Extension exposing PHP 7 abstract syntax tree
Unix (PECL): Run pecl install ast and add extension=ast.so to your php.ini . Unix (Compile): Compile and install the extension as follows. phpize...
Read more >Scala Reflection Library 2.13.3 - scala.reflect.api.Trees
This trait defines the node types used in Scala abstract syntax trees (AST) and operations on them. Trees are the basis for Scala's...
Read more >get the renamed (with fully qualified import) haskell AST ...
I can get the following ghc compiler working using ghc api to compile a single file.I would like to get the renamed AST...
Read more >Artifact.ContainsMany | Android Developers
Artifact types annotated with this marker interface are backed up by a DIRECTORY whose content should be read using the com.android.build.api.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Right now it exists, but we are in the process of reworking it significantly and the existing API is likely to become unsupported in favour of running our analyzer and using the JSON protocol (which will eventually be the language server protocol).
I can’t stop right now to do a full write-up of the current API, unfortunately (and I can’t find any of our existing ones), but if you look through the AnalysisTests project you’ll see plenty of examples where we analyze small snippets of code and examine the results. You should be able to do the same thing from your own application requiring only
Microsoft.PythonTools.Analysis.dll
andMicrosoft.PythonTools.Common.dll
(and if your application is a VS extension, then add a prerequisite for Python support and you won’t even need to install those DLLs).Ah, I see. We rely on SourceLocation.Index to be set correctly when using it to resolve the lookup scope, but we don’t actually make the index available anywhere - we have private methods on the AST to figure it out.
I think the problem is leaking out the
LocationInfo
class, which seems to be meant to be internal only.SourceLocation
should be the only public one (the difference is thatLocationInfo
knows which file the location is within, and has internal mechanisms for lazy resolution).We do have a likely big breaking change coming up (#2846) when we can also make
LocationInfo
private and update the other APIs to return and acceptSourceLocation
(since in most cases the file they belong to is known). Looking at this use case, we probably also need to add a better public API for requesting specific locations (e.g. location of name, location within scope, etc.).The best way I can see to get the information you need is to use an AST walker (
PythonWalker
subclass andast.Walk(walker)
) to find the function, rather than the ModuleAnalysis APIs. This will give you access to theFunctionDefinition
object, which has theSourceLocation
values you need in order to query analysis in a particular scope. OurQualifiedFunctionNameWalker
is a fairly simple example of locating a specific function and generating its full name (though in most cases you’ll want to overrideWalk
rather thanPostWalk
). If you provide the ModuleAnalysis object when constructing your walker, you can call into it as you walk each function to get whatever info you need.Hope that helps!