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.

Installation

python3 -m pip install infer-types retype

This will also install retype which we're going to use to apply generated type annotations back to the code (see Usage below).

Usage

First of all, run the tool:

python3 -m infer_types ./example/

It will infer types for all code in the example directory and save stub files inside of types directory.

The next thing you need to do is to apply the stub files back to the code. For that, we're going to use retype:

retype -it ./example/ ./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.

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

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.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

infer_types-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for infer_types-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8773da52256a88e0528986cd3adf342955737c50559aa3d9cf79fee5d9674f5e
MD5 111f2e6c228f0741334677c3e6b55d69
BLAKE2b-256 396994fe8fd753e08a4527235dd11c4267265906b970ab93ec1cf2b5f3827cfd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for infer_types-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c625de62a8ca115067a0ea7a509c460a9c519d6d4951736ee0e6d2bd128fb5a7
MD5 7fb366868233e19fccf6c916585d010f
BLAKE2b-256 ca5c9b90283e540e3eae4d26cbc8ab3bc23bc3788f4acc489936cdb4c7b1b0c6

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