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 2630b64d authored by Pierre Augier's avatar Pierre Augier
Browse files

Cleaner recreate annotations + bugfix Union repr

parent 0be18bf322b7
import numpy as np
import transonic as ts
from transonic import Type, NDim, Array
from transonic import Type, NDim, Array, Union
T = Type("T")
T1 = Type("T1")
......@@ -14,6 +14,9 @@ assert repr(N - 1) == "N - 1"
print(repr(A1))
repr(Union[A, A1])
@ts.boost
def compute(a: A, b: A1, c: T, d: A, e: str):
print(e)
......
......@@ -8,31 +8,33 @@ from pprint import pprint
from capturex import CaptureX, make_code_external
def dump(node):
print(astunparse.dump(node))
from transonic.analyses.util import get_annotations, print_ast, print_dump
path_examples = Path("examples")
files = sorted(path_examples.glob("*.py"))
with open(files[3]) as file:
with open(files[2]) as file:
code = file.read()
print("ast.parse")
module = ast.parse(code)
print("compute DefUseChains")
duc = beniget.DefUseChains()
duc.visit(module)
print("compute Ancestors")
ancestors = beniget.Ancestors()
ancestors.visit(module)
print("compute UseDefChains")
udc = beniget.UseDefChains(duc)
functions_boosted = {}
class_boosted = {}
classes_boosted = {}
methods_boosted = {}
# we first need to find the node where transonic.boost is defined...
......@@ -49,7 +51,7 @@ def add_definition(definition_node):
else:
tmp_boosted = functions_boosted
elif isinstance(definition_node, ast.ClassDef):
tmp_boosted = class_boosted
tmp_boosted = classes_boosted
if tmp_boosted is not None:
tmp_boosted[key] = definition_node
......@@ -79,19 +81,20 @@ for node in module.body:
add_definition(definition_node)
print("functions:")
pprint(functions_boosted)
print("methods:")
pprint(methods_boosted)
pprint(class_boosted)
print("classes:")
pprint(classes_boosted)
objects = []
boosteds = {"functions": functions_boosted, "methods": methods_boosted, "classes": classes_boosted}
for boosted in (functions_boosted, methods_boosted):
for fdef in boosted.values():
fdef.decorator_list = []
objects.append(fdef)
objects = []
for cdef in class_boosted.values():
objects.append(cdef)
for boosted in boosteds.values():
for definition in boosted.values():
objects.append(definition)
capturex_annotations = CaptureX(
objects,
......@@ -114,3 +117,17 @@ capturex = CaptureX(
)
code_dependance = make_code_external(capturex.external)
namespace = {}
exec(code_dependance_annotations, namespace)
annotations = {}
for kind, boosted in boosteds.items():
annotations[kind] = {}
for key, definition in boosted.items():
annotations[kind][key] = get_annotations(definition, namespace)
pprint(annotations)
\ No newline at end of file
import ast
import astunparse
def print_ast(code):
module = ast.parse(code)
print(astunparse.dump(module))
return module
def dump(node):
print(astunparse.dump(node))
import gast as ast
from transonic.analyses.util import get_annotations
code = """
......@@ -22,19 +12,6 @@ def myfunc(a: int, b: type_):
"""
module = ast.parse(code)
fdef = module.body[1]
new_ast = ast.Assign(
targets=[ast.Name(id="annotations", ctx=ast.Store())],
value=ast.Dict(keys=[], values=[]),
)
dict_node = new_ast.value
for arg in fdef.args.args:
name = arg.arg
dict_node.keys.append(ast.Str(s=name))
dict_node.values.append(arg.annotation)
print(astunparse.unparse(new_ast))
print(get_annotations(fdef, {"type_": float}))
\ No newline at end of file
import gast as ast
import astunparse
def print_ast(code):
module = ast.parse(code)
print(astunparse.dump(module))
return module
def print_dump(node):
print(astunparse.dump(node))
def _fill_ast_annotations_function(function_def, ast_annotations):
dict_node = ast_annotations.value
for arg in function_def.args.args:
if arg.annotation is not None:
try:
name = arg.arg
except AttributeError:
name = arg.id
dict_node.keys.append(ast.Str(s=name))
dict_node.values.append(arg.annotation)
def _fill_ast_annotations_class(class_def, ast_annotations):
dict_node = ast_annotations.value
for node in class_def.body:
if not isinstance(node, ast.AnnAssign):
continue
if node.annotation is not None:
name = node.target.id
dict_node.keys.append(ast.Str(s=name))
dict_node.values.append(node.annotation)
def get_annotations(object_def, namespace):
# print_dump(object_def)
ast_annotations = ast.Assign(
targets=[ast.Name(id="annotations", ctx=ast.Store(), annotation=None)],
value=ast.Dict(keys=[], values=[]),
)
if isinstance(object_def, ast.FunctionDef):
_fill_ast_annotations_function(object_def, ast_annotations)
elif isinstance(object_def, ast.ClassDef):
_fill_ast_annotations_class(object_def, ast_annotations)
else:
raise NotImplementedError
# print_dump(ast_annotations)
source = astunparse.unparse(ast_annotations)
try:
del namespace["__builtins__"]
except KeyError:
pass
exec(source, namespace)
return namespace["annotations"]
......@@ -342,7 +342,7 @@ class UnionMeta(type):
def __repr__(self):
strings = []
for p in self.parameters.values():
for p in self.types:
if isinstance(p, type):
string = p.__name__
else:
......
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