Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

Commit 157aec13 authored by Pierre Augier's avatar Pierre Augier
Browse files

extract_variable_annotations

parent e25c9074d445
......@@ -3,7 +3,7 @@ import gast as ast
from transonic.analyses.capturex import CaptureX
from transonic.analyses import extast
from transonic.analyses.util import print_dumped, print_unparsed
from transonic.analyses.util import print_dumped, print_unparsed, extract_variable_annotations
with open("simple.py") as file:
code = file.read()
......@@ -20,6 +20,9 @@ for node in module.body:
capturex = CaptureX([fdef], module, consider_annotations="only")
code_ext = capturex.make_code_external()
code_dependance_annotations = capturex.make_code_external()
print("code:", code_ext, sep="\n")
\ No newline at end of file
namespace = {}
exec(code_dependance_annotations, namespace)
print(extract_variable_annotations(fdef, namespace))
\ No newline at end of file
from transonic import boost
tmp = float
T = tmp
T1 = int
T2 = float
const = 1
const_ann = 1
def func():
@boost
def func(a: T2):
i: T
i = const
ii: T1 = const_ann
return i * ii
return i * ii * a
......@@ -28,13 +28,12 @@ from .util import (
print_dumped,
print_unparsed,
find_path,
change_import_name,
filter_external_code,
get_exterior_code,
extract_variable_annotations,
)
from .capturex import CaptureX
from .blocks_if import get_block_definitions
from .parser import parse_code
from .parser import parse_transonic_def_commands
from . import extast
......@@ -218,8 +217,8 @@ def analyse_aot(code, pathfile):
def_nodes = [
def_node
for boosted_dict in boosted_dicts.values()
for backend in boosted_dict.values()
for def_node in backend.values()
for boosted_dict_backend in boosted_dict.values()
for def_node in boosted_dict_backend.values()
]
capturex = CaptureX(
......@@ -306,10 +305,12 @@ def analyse_aot(code, pathfile):
)
code_dependance = code_dependance[cls]
debug(code_dependance)
debug("parse_code")
signatures_p = parse_code(code)
annotations["comments"] = {}
debug("parse_transonic_def_commands")
signatures_p = parse_transonic_def_commands(code)
annotations["__in_comments__"] = {}
annotations["__locals__"] = {}
for functions_backend in boosted_dicts["functions"].values():
for name_func, fdef in functions_backend.items():
......@@ -318,13 +319,19 @@ def analyse_aot(code, pathfile):
except KeyError:
signatures = tuple()
arg_names = [arg.id for arg in fdef.args.args]
annotations_sign = annotations["comments"][name_func] = []
annotations_sign = annotations["__in_comments__"][name_func] = []
for sig in signatures:
types = [
type_.strip()
for type_ in sig[len(fdef.name) + 1 : -1].split(",")
]
annotations_sign.append(
{arg_name: type_ for arg_name, type_ in zip(arg_names, types)}
)
annotations_sign.append(dict(zip(arg_names, types)))
# locals: variable annotations
annotations_locals = extract_variable_annotations(fdef, namespace)
if annotations_locals:
annotations["__locals__"][name_func] = annotations_locals
debug("annotations:\n" + pformat(annotations))
return boosted_dicts, code_dependance, annotations, blocks, code_ext
......@@ -7,7 +7,7 @@ from tokenize import tokenize, COMMENT
from io import BytesIO
def parse_code(code: str):
def parse_transonic_def_commands(code: str):
"""Parse the code of a .py file and return data"""
functions = set()
......
......@@ -412,3 +412,16 @@ def get_exterior_code(
relative,
)
return codes_dependance, code_ext, jitted_dicts, special
def extract_variable_annotations(fdef, namespace):
variable_types = {}
for node in fdef.body:
if isinstance(node, ast.AnnAssign):
name = node.target.id
source = extast.unparse(node.annotation)
result = eval(source, namespace)
variable_types[name] = result
return variable_types
......@@ -482,9 +482,7 @@ def compute_pythran_types_from_valued_types(types):
pythran_types = []
names = values_template_parameters.keys()
for set_types in itertools.product(*values_template_parameters.values()):
template_variables = {
name: value for name, value in zip(names, set_types)
}
template_variables = dict(zip(names, set_types))
pythran_types.append(
compute_pythran_types_from_types(types, **template_variables)
......@@ -563,9 +561,7 @@ def make_signatures_from_typehinted_func(func):
names = values_template_parameters.keys()
signatures = []
for set_types in itertools.product(*values_template_parameters.values()):
template_variables = {
name: value for name, value in zip(names, set_types)
}
template_variables = dict(zip(names, set_types))
signatures.extend(
make_signature_from_template_variables(
func, _signature=_signature, **template_variables
......
......@@ -33,7 +33,7 @@ class PythranBackend(BackendAOT):
f"# pythran export {func_name}({', '.join(types)})"
)
anns = annotations["comments"][func_name]
anns = annotations["__in_comments__"][func_name]
if not fdef.args.args:
signatures_func.add(f"# pythran export {func_name}()")
for ann in anns:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment