Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Extracting Type information from pytype #1678

Open
R3x opened this issue Jul 11, 2024 · 3 comments
Open

Question: Extracting Type information from pytype #1678

R3x opened this issue Jul 11, 2024 · 3 comments

Comments

@R3x
Copy link

R3x commented Jul 11, 2024

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
Copy link
Contributor

kamahen commented Jul 11, 2024

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
Copy link
Author

R3x commented Jul 11, 2024

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants