pytype icon indicating copy to clipboard operation
pytype copied to clipboard

Question: Extracting Type information from pytype

Open R3x opened this issue 1 year ago • 3 comments

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

R3x avatar Jul 11 '24 15:07 R3x

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.

kamahen avatar Jul 11 '24 18:07 kamahen

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.

R3x avatar Jul 11 '24 19:07 R3x

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.

martindemello avatar Jul 12 '24 05:07 martindemello

Hi, thank you for your report. Google is shifting its effort to a different approach for type checking apart from pytype, and we're not planning to put in any effort in the near future in pytype, so we'll close this issue. Please see the announcement here.

h-joo avatar Aug 20 '25 16:08 h-joo