Re:Pyre: Fast Type Checking for Python
by mlevental on Friday May 11, 2018 @12:38PM
(#17048749)
what i don't understand about stuff like this (and honestly typescript as well) is if there aren't definitions for type schemas then this doesn't work right? like none of these tools will introspect or something and figure out which types i really mean without me annotating with some kind of crazy sum/tuple type definition (e.g. foo() -> Object{a:b,c:d} or whatever it is in typescript). maybe i'm wrong and there's something different here?
[ Parent
| on HN ]
Instagram actually has a program for stubbing existing code called MonkeyType. It requires running your code to collect the data, and then after your run finishes you can then apply the new inferred types to your program.
so like can i throw my entire django app at this thing and have it spit out types for me? probably not right?
That's actually correct - it will spit out types it can infer. https://github.com/Instagram/MonkeyType
(Oh daaaamn that's cool!)
Even without you explicitly defining types, there is already quite a bit such tools can help you with since they usually come with type annotations for the standard library (or equivalent) of their languages.
>there is already quite a bit such tools
example?
Typeshed[1] identifies the types of all builtins for all current (2.7+) versions of python.
0.to_string() / 5
map([1, 2, 3], lambda n: n * 2).some_field_that_doesnt_exist
A type checker would find both mistakes very easily, by only using the information from the standard library.
pytype [https://github.com/google/pytype] does do type inference (disclaimer - i work on it). if you supply an explicit type annotation it will be checked for correctness, and if not we trace through the control flow graph to figure out the types of everything as best as we can (which does sometimes mean falling back to Any).
we are working on an extension that will add the inferred types as annotations to your code.
PyCharm, and perhaps other tools/editors, does introspect the code. It's not too hard to work out what protocols or interfaces an argument requires, e.g if you use it in a for loop it needs to be iterable, if you index or slice it then it needs to support that, if you add it to an integer etc etc.
It's not about the concrete types but instead about what protocols it supports.