Skip to main content

CLI tool to automatically annotate Python code.

Project description

infer-types

A CLI tool to automatically add type annotations into Python code.

The main scenario for using the tool is to help you with annotating a big and old codebase. It won't solve the task for you 100% but will definitely help you tremendously, because many of the functions in the real world have quite simple return types that are easy to infer automatically.

Features:

  • 100% automated, get a bunch of type annotations with no effort.
  • 100% static, all types are inferred without running the code.
  • A lot of heuristics and smart inference.
  • Actively uses typeshed to find annotations for unannotated dependencies.

Example

Let's say, you have the following method:

class Database:
    def users_count(self):
        return len(self.users)

Since len always returns int, infer-types is able to infer the return type of the method. So, after running the tool, the code will look like this:

class Database:
    def users_count(self) -> int:
        return len(self.users)

Installation

python3 -m pip install infer-types

Usage

python3 -m infer_types ./example/

The tool will add new import statements that can be duplicated and are located not at the top of the file. To fix it, run isort:

python3 -m isort ./example/

The infer-types tool uses the new fancy syntax for type annotations introduced in Python 3.10. So, instead of Optional[str] it will emit str | None. If your code is supposed to run on an older version of Python, add from __future__ import annotations at the beginning of each file. It will solve the issue and also make startup of your app faster. You can also do that with isort:

python3 -m isort --add-import 'from __future__ import annotations' ./example/

See awesome-python-typing for more tools to help you with annotating your code.

How it works

  • Most of heuristics live in astypes package. Check it out learn more about the main inference logic.
  • If the same method is defined in a base class, copy the type annotations from there.
  • If there are no return statements returning a value, the return type is None.
  • If there is a yield statement, the return type is typing.Iterator.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

infer_types-0.3.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

infer_types-0.3.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file infer_types-0.3.0.tar.gz.

File metadata

  • Download URL: infer_types-0.3.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for infer_types-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a05e6f87daf0f6ac3f0a7b9785ef5a479176de34dd42372258e5e49fb1b3ace3
MD5 b44c74710681979b048928325f447a21
BLAKE2b-256 ec2b95790a5cd40b6655cef96c0f3f660a23c04b6321dd3154a566256fc51e1a

See more details on using hashes here.

File details

Details for the file infer_types-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for infer_types-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 465f8af0db5616fcce9370d2a3f64becdfd46405be7f98ce31432ccd2b1f53cb
MD5 f31270bc960708bfa586f7a0f8c0b930
BLAKE2b-256 8b785ad859096f7ec535e39fbf3c27533dfe7c2f1f8edf71bd0fa7860d2c7afd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page