Skip to main content

Infer types for AST nodes.

Project description

astypes

Python library to statically detect types for AST nodes.

A good use case is a linter that needs to run some rules only for particular types. For instance, to check arguments of something.format(a=b) only if something has type str.

python3 -m pip install astypes

Usage

Astypes uses astroid to infer definitions of nodes. So, if your code works with ast nodes, you'll need to convert them into astroid first:

import astroid
import astypes

module = astroid.parse(source_code)
node = astypes.find_node(module, ast_node)

And when you have an astroid node, you can get its type:

node_type = astype.get_node(node)
print(node_type.annotation)

For a real-world usage example, check out infer-types. It is a CLI tool that automatically adds type annotations into Python code using astypes.

How it works

You can find most of the logic in astypes/_handlers.py. In short:

  1. There are some nodes that are easy to infer type of. For example, 13 is always int.
  2. Some nodes are also to infer, but only if to make some assumptions. Assumptions that we make are the ones that are true in 99% of cases. For example, we assume that list(x) returns type list. It might be not true if you shadow list with something else.
  3. If the type cannot be assumed just looking at the node, we try to use astroid to infer the type.
  4. If the returned value is a function call, we use astroid to find the definition of the function. The annotated return annotation of the function is what we need.
  5. If resolved function doesn't have annotation, we use typeshed_client to get its annotation from typeshed. For example, for all built-in functions.

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

astypes-0.1.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

astypes-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for astypes-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0f7e4a82bd937c757b59e3d2c9bf1ace72734048ee864e9dcb237b3bd3194b57
MD5 c0940300a0a8b472c0c2c714316b504b
BLAKE2b-256 d36bcb9e48080259c9cc51ed9714cf982ebbda148463d278c899237e6fb8a4f3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: astypes-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for astypes-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db6953c64d46f1b6c6adaf357cf5428a996f3356cc4728b4fe5bdd383d2dd1ad
MD5 60ea65e75595890e13b8d10426bf6eff
BLAKE2b-256 56b9b6e520f4a04ba30b9e58e2cd2bd6f97f1dfb6d3f87c45bde53f62f2dc882

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