# 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`.
```bash
python3 -m pip install astypes
```
## Usage
Astypes uses [astroid](https://github.com/PyCQA/astroid) to infer definitions of nodes. So, if your code works with [ast](https://docs.python.org/3/library/ast.html) nodes, you'll need to convert them into astroid first:
```python
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:
```python
node_type = astype.get_node(node)
print(node_type.annotation)
```
Example:
```python
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](https://github.com/orsinium-labs/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](./astypes/_handlers.py). In short:
1. There are some nodes that are easy to infer type of. For example, `13` is always `int`.
1. 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.
1. If the type cannot be assumed just looking at the node, we try to use [astroid](https://github.com/PyCQA/astroid) to infer the type.
1. 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.
1. If resolved function doesn't have annotation, we use [typeshed_client](https://github.com/JelleZijlstra/typeshed_client) to get its annotation from [typeshed](https://github.com/python/typeshed). For example, for all built-in functions.
Raw data
{
"_id": null,
"home_page": null,
"name": "astypes",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "typing,annotations,type annotations,ast,astroid",
"author": null,
"author_email": "Gram <git@orsinium.dev>",
"download_url": "https://files.pythonhosted.org/packages/39/be/ddca111da9e3beead918ff9298d6b75ca4b28641f54fbbbb8a12bfccdbc5/astypes-0.2.5.tar.gz",
"platform": null,
"description": "# astypes\n\nPython library to statically detect types for AST nodes.\n\nA 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`.\n\n```bash\npython3 -m pip install astypes\n```\n\n## Usage\n\nAstypes uses [astroid](https://github.com/PyCQA/astroid) to infer definitions of nodes. So, if your code works with [ast](https://docs.python.org/3/library/ast.html) nodes, you'll need to convert them into astroid first:\n\n```python\nimport astroid\nimport astypes\n\nmodule = astroid.parse(source_code)\nnode = astypes.find_node(module, ast_node)\n```\n\nAnd when you have an astroid node, you can get its type:\n\n```python\nnode_type = astype.get_node(node)\nprint(node_type.annotation)\n```\n\nExample:\n\n```python\nimport astroid\nimport astypes\n\nnode = astroid.extract_node('1 + 2.3')\nt = astypes.get_type(node)\nprint(t.annotation) # 'float'\n```\n\nFor a real-world usage example, check out [infer-types](https://github.com/orsinium-labs/infer-types). It is a CLI tool that automatically adds type annotations into Python code using astypes.\n\n## How it works\n\nYou can find most of the logic in [astypes/_handlers.py](./astypes/_handlers.py). In short:\n\n1. There are some nodes that are easy to infer type of. For example, `13` is always `int`.\n1. 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.\n1. If the type cannot be assumed just looking at the node, we try to use [astroid](https://github.com/PyCQA/astroid) to infer the type.\n1. 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.\n1. If resolved function doesn't have annotation, we use [typeshed_client](https://github.com/JelleZijlstra/typeshed_client) to get its annotation from [typeshed](https://github.com/python/typeshed). For example, for all built-in functions.\n",
"bugtrack_url": null,
"license": null,
"summary": "Infer types for AST nodes.",
"version": "0.2.5",
"split_keywords": [
"typing",
"annotations",
"type annotations",
"ast",
"astroid"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "ebac465ea160b3b2b4a10ace3f59d62345794f19a6ceeece5fb71a45672551c9",
"md5": "c66b9df89a2d6ca42449ab93f426858c",
"sha256": "fd57644206b6d206cdaf93fbb7bbcc80a7c0dedeedf0c9bdba2c545cef243f8c"
},
"downloads": -1,
"filename": "astypes-0.2.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c66b9df89a2d6ca42449ab93f426858c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 10039,
"upload_time": "2023-03-16T16:20:09",
"upload_time_iso_8601": "2023-03-16T16:20:09.154011Z",
"url": "https://files.pythonhosted.org/packages/eb/ac/465ea160b3b2b4a10ace3f59d62345794f19a6ceeece5fb71a45672551c9/astypes-0.2.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "39beddca111da9e3beead918ff9298d6b75ca4b28641f54fbbbb8a12bfccdbc5",
"md5": "44a390f0bdd3e0665dafbf36e4c8bc50",
"sha256": "a3d192557be9ae0eb13359b5965d4d99b47c0d6dddd93dee8f962a4bbfc02ea8"
},
"downloads": -1,
"filename": "astypes-0.2.5.tar.gz",
"has_sig": false,
"md5_digest": "44a390f0bdd3e0665dafbf36e4c8bc50",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 13222,
"upload_time": "2023-03-16T16:20:12",
"upload_time_iso_8601": "2023-03-16T16:20:12.058810Z",
"url": "https://files.pythonhosted.org/packages/39/be/ddca111da9e3beead918ff9298d6b75ca4b28641f54fbbbb8a12bfccdbc5/astypes-0.2.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-16 16:20:12",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "astypes"
}