Mypy: Question: Is the a project to automatically annotate functions?

Created on 29 Sep 2020  路  4Comments  路  Source: python/mypy

I am aware of MonkeyType, but as far as I know they are "only" using unit tests to infer types. This means if there are no unit tests for a piece of code, MonkeyType cannot help.

But sometimes you can still guess the type from the structure. I've added examples below.

How would such a "type guessing" tool be used?

  1. One has a code base without type annotations and possibly with no or few tests
  2. The type guesser is run and annotates the code in a way so that mypy would not complain about it.
  3. The annotations are reviewed as the type guessing cannot be perfect

Is there something like that? Possibly a machine learning project which takes annotated code as input and builds a model to represent which variable names/methods typically represent which classes.

Example 1: Guess by usage within known functions

def a(b):
    c, d = 0, 1
    for _ in range(b):
        c, d = d, c+d
    return c

Even if you don't know what the code is doing, it is very reasonable to assume that b is an integer as it is used as an argument to range.

Example 2: Guess by method usage

def a(b):
    return b.split('.')[-1]

It is likely that b is a string, although a lot more is possible

Example 3: Guess by name

Some parameter names also give clear indicators:

  • config => Dict[str, Any]
  • index => int
  • date => date or datetime
  • y => float or int
    ...
question

Most helpful comment

Also, MonkeyType doesn't use exclusively unit tests鈥攊t can also be used at runtime (at some performance cost) to gather type information.

All 4 comments

Thanks for asking! There's an experimental mypy feature, mypy suggest, that can do this (under limited circumstances).

If you need more help I recommend Gitter.

I'm sorry, there are other tools that do this too. In particular pytype implements your "example 2" and make-stub-files can do (3).

Thank you so much! I will have a look at all of them :-) (And thank you for developing / steering Python for so many years!)

Also, MonkeyType doesn't use exclusively unit tests鈥攊t can also be used at runtime (at some performance cost) to gather type information.

Was this page helpful?
0 / 5 - 0 ratings