add static type information to Python abstract syntax trees
Project description
Attempt to add static type information to Python abstract syntax trees (ASTs).
Works best with ASTs from typed_ast module, however it also works with built-in ast module.
Be advised that this is an ongoing work, and current implementation is subject to sudden changes.
how to use
You can use the static_typing module to parse the code directly using parse() function:
import static_typing as st
class MyClass:
pass
module = st.parse('def my_fun(obj: MyClass) -> MyClass: return obj')
# TODO: currently there is no public API yet
functions = module._functions
my_fun = module._functions['my_fun']
assert MyClass in my_fun._params['obj']
Or, you can augment existing AST using augment() function:
import static_typing as st
import typed_ast.ast3
module = typed_ast.ast3.parse('''def spam(): x, y, z = 'ham', 42, 3.1415 # type: str, int, float''')
module = st.augment(module)
# TODO: currently there is no public API yet
function = module._functions['spam']
assert len(function._local_vars) == 3
assert float in function._local_vars['z']
For more examples see examples.ipynb notebook.
how it’s implemented
First of all a type hint resolver has been implemented. It uses provided Python symbol tables to resolve type hints into actual type objects using introspection, and stores the resolved type hints directly in the AST. Thus, Python type information becomes static.
By default, the resolver uses only built-in symbols when called directly or through augment(). However, when called through parse() it uses globals() and locals() of the caller by default.
Secondly, new fields have been added to several AST nodes, currently these are: Module, FunctionDef, ClassDef, Assign, AnnAssign, For and With. These new fields store useful static type information in an organized and easy-to-access manner.
For Module:
defined constants (TODO)
defined functions
defined classes
For FunctionDef:
parameters and their types
return types
kind (i.e. function, instance method, class method, static method, etc.)
local variables and their types
For ClassDef:
defined methods (all together and grouped by kind)
class fields and their types
instance fields and their types
For Assign and AnnAssign:
assignment targets and their types
For For:
index variables and their types
For With:
context variables and their types
Thirdly, an AST augumenting function was implemented. This function resolves type hints in the AST and replaces all ordinary AST nodes listed above with their extended versions.
requirements
Python version >= 3.4.
Python libraries as specified in requirements.txt.
Building and running tests additionally requires packages listed in test_requirements.txt.
Tested on Linux and Windows.
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
Built Distribution
Hashes for static_typing-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fa374d9a38a9655799a519e69d53c95f7c7788a89af504efe2f692186e69a0a |
|
MD5 | 3b033e39177fc60074a575e2b36e7cbf |
|
BLAKE2b-256 | 7ff223e095be5a45ae1f3a48bd0108dfb343493101fe7e5b05fe99437a8a2d53 |