Commit ed119774 authored by Tom Niget's avatar Tom Niget

Remove PyType references

parent 558acbaa
......@@ -173,7 +173,6 @@ else:
def transpile(source, name="<module>", path=None):
TB = f"transpiling module {cf.white(name)}"
res = ast.parse(source, type_comments=True)
# res = initial_pytype.run(source, res)
IfMainVisitor().visit(res)
res = DesugarWith().visit(res)
......
import ast
import pytype.config
from pytype import io
from pytype.pytd import pytd_utils
from pytype.tools.traces import traces
def run(source: str, module: ast.Module) -> ast.Module:
opt = pytype.config.Options.create(None, no_return_any=True, precise_return=True)
source_code = infer_types(source, opt)
visitor = AnnotateAstVisitor(source_code, ast)
visitor.visit(module)
return module
def infer_types(source: str, options: pytype.config.Options) -> "source.Code":
with io.wrap_pytype_exceptions(PytypeError, filename=options.input):
return traces.trace(source, options)
class AnnotateAstVisitor(traces.MatchAstVisitor):
def visit_Name(self, node):
self._maybe_annotate(node)
def visit_Attribute(self, node):
self._maybe_annotate(node)
def visit_FunctionDef(self, node):
self._maybe_annotate(node)
def _maybe_annotate(self, node):
"""Annotates a node."""
try:
ops = self.match(node)
except NotImplementedError:
return
# For lack of a better option, take the first one.
unused_loc, entry = next(iter(ops), (None, None))
self._maybe_set_type(node, entry)
def _maybe_set_type(self, node, trace):
"""Sets type information on the node, if there is any to set."""
if not trace:
return
node.resolved_type = trace.types[-1]
node.resolved_annotation = _annotation_str_from_type_def(trace.types[-1])
class PytypeError(Exception):
"""Wrap exceptions raised by Pytype."""
def _annotation_str_from_type_def(type_def):
return pytd_utils.Print(type_def)
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