google / pytype

A static type analyzer for Python code
https://google.github.io/pytype
Other
4.71k stars 273 forks source link

Question: Extracting Type information from pytype #1678

Open R3x opened 1 month ago

R3x commented 1 month ago

Hello, I actually want to know if there's a possibility of extracting type information about an application from pytype. I want to analyze an application (along with its tests etc) and see if I can extract the type information for every parameter, return value and attribute.

When I try to run the tool, it tries to every generalize stuff to Any, I would rather have with a list of (incomplete) possible types that can be passed to this function parameter.

PS : also is there any configuration options to make the tool identify cases such as :

def execute(command): <- why is the command parameter not made into type str
    os.system(command) <- here we know it's type str
kamahen commented 1 month ago

Perhaps this will help? https://google.github.io/pytype/developers/tools.html#merge_pyi

As for your example of execute(command) -- there is a way of inferring this, and an early experimental version of pytype did such inferences, but that required solving constraints in order to do the "back propagation" of type information; and if the program had any type errors, the constraints would simply fail with no easy way to find the cause of the failure -- and it turns out that a lot of real programs have subtle type errors, even though they don't get runtime type errors.

For this particular situation, as long as you have a call to execute(), you'll get a type for command; and if it doesn't match with the type that os.system() expects, you'll get an error.

R3x commented 1 month ago

Thanks for the response!

Oh, I thought the .pyi files would not contain the information about the types if they are already annotated or would it always contain the type hints no matter whether it's already annotated or not? I can't seem to find the tool pytd-tool (mentioned to be a parser for .pyi files, in the main readme) is it removed.

I see, I am not really worried about false positive type errors, because I basically want to use pytype as a part of a pipeline for a tool I am trying to build, to extract type information for specific types. Basically, I want to take advantage of the static type propagation to output as many types as possible for specific functions.

martindemello commented 1 month ago

poke around the xref code and see if anything in there helps you. at this point the indexer should contain type information for various symbols. also merge-pyi might be helpful - in general the tools/ directory contains several examples of using pytype as a library.