question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API to get type-annotated AST

See original GitHub issue

Is 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:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zoobacommented, Aug 28, 2017

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 and Microsoft.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).

0reactions
zoobacommented, Sep 13, 2017

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 that LocationInfo 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 accept SourceLocation (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 and ast.Walk(walker)) to find the function, rather than the ModuleAnalysis APIs. This will give you access to the FunctionDefinition object, which has the SourceLocation values you need in order to query analysis in a particular scope. Our QualifiedFunctionNameWalker is a fairly simple example of locating a specific function and generating its full name (though in most cases you’ll want to override Walk rather than PostWalk). 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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found