You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
The type of some nodes is easy to infer. For example, 13 is always int.
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 type list. It might be not true if you shadow list with 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.
About
Python library to infer types for AST nodes. Make the most powerful Python linters and formatters!