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)
Example:
import astroid
import astypes
node = astroid.extract_node('1 + 2.3')
t = astypes.get_type(node)
print(t.annotation) # 'float'
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:
- The type of some nodes is easy to infer. For example,
13is alwaysint. - Some nodes are also can be inferred but only if we 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 typelist. It might be not true if you shadowlistwith something else. - If the type cannot be assumed just by looking at the node, we try to use astroid to infer the type.
- 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.
- If the resolved function doesn't have annotation, we use typeshed_client to get its annotation from typeshed. For example, for all built-in functions.
Metadata
Release files for astypes 0.2.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| astypes-0.2.6.tar.gz | 13.6 kB | Without provenance | Details for astypes-0.2.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| astypes-0.2.6-py3-none-any.whl | Python 3 | none | any | Details for astypes-0.2.6-py3-none-any.whl |
Total release size: 23.9 kB
Release files / astypes-0.2.6.tar.gz
| Download URL | astypes-0.2.6.tar.gz |
|---|---|
| Size | 13.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7bfb49dc207890587cd3d1b2fe57c61aa378fb0603a3590ab5e1c1c78b24e387
|
|
BLAKE2b-256 checksum How to use checksums |
41807028efe25d789b020170cc5937f6d2162a2a53604aa3914d9b03707f9390
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.2
|
Release files / astypes-0.2.6-py3-none-any.whl
| Download URL | astypes-0.2.6-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4274e83a80c616f400d05678f9b8bc8143a94e544bbfa02906cc5805d990bf13
|
|
BLAKE2b-256 checksum How to use checksums |
1c9f087a31145a2b63ce01deb60e160eac29c700562921b43a7cfbbd0daf3bce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.2
|
Security for astypes 0.2.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.