Skip to content

Question: Is the a project to automatically annotate functions? #9504

@MartinThoma

Description

@MartinThoma

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
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions