Skip to main content

Python type inferencer

Project description

Pytype

Pytype is a static analyzer for Python code.

License

Apache 2.0

Abstract

pytype can statically analyze your Python code, and point out bugs and errors it finds. It works on any kind of code, and doesn’t need any special directives to be useful.

However, it can additionally verify (and leverage) type annotations.

Source

Pytype’s sources can be found on github: https://github.com/google/pytype/

Installation

Install pytype from pip

$ pip install pytype

Usage

NOTE: pytype analyzes a single file. To analyze an entire project, use the included pytype-all tool.

For more detailed explanations of pytype’s error messages, see this doc

Usage: pytype [options] file.py

Infer/check types in a Python module

Selected options:
  -h, --help            Show the full list of options
  -C, --check           Don't do type inference. Only check for type errors.
  -o OUTPUT, --output=OUTPUT
                        Output file. Use '-' for stdout.
  -V PYTHON_VERSION, --python_version=PYTHON_VERSION
                        Python version to emulate ("major.minor", e.g. "2.7")

Example

Consider the following code, which uses the type annotation syntax from PEP 3107 and PEP 484 to declare the parameter and return types of the function f:

$ cat t.py

def f(x: int, y: str = 'default') -> int:
  return "foo"

Note that the code above has a bug: The return type is declared to be an integer, but the function actually returns a string.

Now check it with pytype:

$ pytype -V 3.6 t.py

File "t.py", line 2, in f: bad option in return type [bad-return-type]
  Expected: int
  Actually returned: str

Pytype can also infer type annotations if they are not explicitly provided.

$ cat t.py

class A(object):
  def __init__(self):
    self.x = 10

p = A()
q = p.x

Run pytype in inference mode (using the -o or --output option):

$ pytype t.py -o -

p = ...  # type: A
q = ...  # type: int

class A(object):
    x = ...  # type: int

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pytype-2018.5.22.tar.gz (1.1 MB view details)

Uploaded Source

File details

Details for the file pytype-2018.5.22.tar.gz.

File metadata

  • Download URL: pytype-2018.5.22.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pytype-2018.5.22.tar.gz
Algorithm Hash digest
SHA256 505053ddfd60171bc431afd995d53f3df397777596451e84d82f5d70d2a138a5
MD5 374fc27ee7953d6052f907ec6df516d2
BLAKE2b-256 805cdb4ceca77ef0323a5d94d2d64981936a58926953136a7cf7bbb0446cfd19

See more details on using hashes here.

Supported by

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