Exemplo n.º 1
0
 def _get_imports(self) -> List[ast.stmt]:
     return [
         ast.ImportFrom(
             module='__future__',
             names=[ast.alias(name='absolute_import', asname=None)],
             level=0),
         ast.ImportFrom(module='__future__',
                        names=[ast.alias(name='division', asname=None)],
                        level=0),
         ast.ImportFrom(
             module='__future__',
             names=[ast.alias(name='print_function', asname=None)],
             level=0)
     ]
Exemplo n.º 2
0
def _split_imports(body):
    """Given a body reurn the import statemens and remaining statements."""
    imports = []
    remainder = []
    stdimports = []
    in_imports = True
    for line in body:
        if in_imports:
            if isinstance(line, ast3.Import):
                for alias in line.names:
                    if alias.name in constants.STANDARD_PYTHON_MODULES:
                        stdimports.append(ast3.Import(names=[alias]))
                    else:
                        imports.append(ast3.Import(names=[alias]))
            elif isinstance(line, ast3.ImportFrom):
                new_imports = [
                    ast3.ImportFrom(
                        module=line.module,
                        names=[name],
                    ) for name in line.names
                ]
                if line.module in constants.STANDARD_PYTHON_MODULES:
                    stdimports += new_imports
                else:
                    imports += new_imports
            else:
                in_imports = False
        if not in_imports:
            remainder.append(line)
    return stdimports, imports, remainder
Exemplo n.º 3
0
 def ensure_mpi(self, canonical_name, alias):
     # if ('mpi4py', None) not in self._import_statements:
     self._import_statements[canonical_name, alias] = [
         typed_ast3.ImportFrom(
             module='mpi4py',
             names=[typed_ast3.alias(name='MPI', asname=None)],
             level=0),
         # typed_ast3.parse('mpi4py.config = no_auto_init', mode='eval') # TODO: may be needed
     ]
Exemplo n.º 4
0
 def visit_ImportFrom(self,
                      node: ast3.ImportFrom) -> Iterable[ast3.ImportFrom]:
     parent_module_path = to_parent_module_path(
         node, parent_module_path=self.module_path)
     for name_alias in node.names:
         actual_path = to_actual_path(name_alias)
         if actual_path == catalog.WILDCARD_IMPORT:
             yield from to_flat_root(parent_module_path).body
         else:
             yield ast3.ImportFrom(str(parent_module_path), [name_alias], 0)
Exemplo n.º 5
0
    def _replace_import_from_module(self, node: ast.ImportFrom, from_: str,
                                    to: str) -> ast.Try:
        """Replaces import from with try/except with old and new import module."""
        self._tree_changed = True

        rewrote_module = node.module.replace(from_, to, 1)
        rewrote = ast.ImportFrom(module=rewrote_module,
                                 names=node.names,
                                 level=node.level)

        return self.wrapper.get_body(
            previous=node,  # type: ignore
            current=rewrote)[0]
Exemplo n.º 6
0
 def _get_replaced_import_from_part(
         self, node: ast.ImportFrom, alias: ast.alias,
         names_to_replace: Dict[str, Tuple[str, str]]) -> ast.ImportFrom:
     """Returns import from statement with changed module or alias."""
     full_name = '{}.{}'.format(node.module, alias.name)
     if full_name in names_to_replace:
         full_name = full_name.replace(names_to_replace[full_name][0],
                                       names_to_replace[full_name][1], 1)
     module_name = '.'.join(full_name.split('.')[:-1])
     name = full_name.split('.')[-1]
     return ast.ImportFrom(
         module=module_name,
         names=[ast.alias(name=name, asname=alias.asname or alias.name)],
         level=node.level)
 def test_ast_validator_synthetic(self):
     examples = ((typed_ast3,
                  typed_ast3.FormattedValue(typed_ast3.Str('value'), None,
                                            None)),
                 (typed_ast3,
                  typed_ast3.keyword(
                      None, typed_ast3.Name('value', typed_ast3.Load()))),
                 (typed_ast3,
                  typed_ast3.ImportFrom('pkg',
                                        [typed_ast3.alias('module', None)],
                                        None)))
     for fields_first, (ast_module, example) in itertools.product(
         (False, True), examples):
         with self.subTest(example=example):
             # tree = ast_module.Expression(example)
             validator = AstValidator[ast_module](fields_first=fields_first,
                                                  mode=None)
             validator.visit(example)
Exemplo n.º 8
0
    def visit_ImportFrom(self, node: ast3.ImportFrom) -> VisitorOutput:
        """Defines how to import (from) modules (supported and nonsupported)

        For example, it converts::

            from numpy import array
            from numpy import *
            from somelib import var, othervar as var2
            from otherlib import *

        into::

            from pytropos.libs_checking import numpy_module
            st['array'] = numpy_module.attr['array', pos...]

            from pytropos.libs_checking import numpy_module
            st.importStar(numpy_module)

            st['var'] = pt.Top
            st['var2'] = pt.Top

            st.importStar()
            """

        libs: 'List[ast3.AST]' = []

        if node.module in self._supported_modules:
            module_name = self._supported_modules[node.module]
            # from pytropos.libs_checking import module_name
            libs.append(
                ast3.ImportFrom(
                    module='pytropos.libs_checking',
                    names=[ast3.alias(name=module_name, asname=None)],
                    level=0,
                ))
            if node.names[0].name == '*':
                # st.importStar(module_name)
                libs.append(
                    ast3.Expr(value=ast3.Call(
                        func=ast3.Attribute(
                            value=ast3.Name(id='st', ctx=ast3.Load()),
                            attr='importStar',
                            ctx=ast3.Load(),
                        ),
                        args=[ast3.Name(id=module_name, ctx=ast3.Load())],
                        keywords=[],
                    ), ))
            else:
                for alias in node.names:
                    # st['asname'] = modname.attr['name']

                    pos = pos_as_tuple(node)

                    if pos is not None:
                        attrname = ast3.Tuple(
                            elts=[ast3.Str(s=alias.name), pos],
                            ctx=ast3.Load())  # type: ast3.expr
                    else:
                        attrname = ast3.Str(s=alias.name)

                    libs.append(
                        ast3.Assign(
                            targets=[
                                ast3.Subscript(
                                    value=ast3.Name(id='st', ctx=ast3.Load()),
                                    slice=ast3.Index(value=ast3.Str(
                                        s=alias.asname if alias.
                                        asname else alias.name), ),
                                    ctx=ast3.Store(),
                                ),
                            ],
                            value=ast3.Subscript(
                                value=ast3.Attribute(
                                    value=ast3.Name(id=module_name,
                                                    ctx=ast3.Load()),
                                    attr='attr',
                                    ctx=ast3.Load(),
                                ),
                                slice=ast3.Index(value=attrname, ),
                                ctx=ast3.Load(),
                            ),
                        ))
        else:
            if node.names[0].name == '*':
                # st.importStar()
                libs.append(
                    ast3.Expr(value=ast3.Call(
                        func=ast3.Attribute(
                            value=ast3.Name(id='st', ctx=ast3.Load()),
                            attr='importStar',
                            ctx=ast3.Load(),
                        ),
                        args=[],
                        keywords=[],
                    ), ))
            else:
                libs.extend(
                    ast3.parse(  # type: ignore
                        '\n'.join([
                            "st['{asname}'] = pt.Top".format(
                                asname=alias.asname if alias.asname else alias.
                                name) for alias in node.names
                        ])).body)

        return libs
Exemplo n.º 9
0
    def visit_Import(self, node: ast3.Import) -> VisitorOutput:
        """Defines how to import modules (supported and nonsupported)

        For example, it converts::

            import numpy
            import numpy as np
            import somelib, otherlib as other

        into::

            from pytropos.libs_checking import numpy_module
            st['numpy'] = numpy_module

            from pytropos.libs_checking import numpy_module
            st['np'] = numpy_module

            st['somelib'] = pt.ModuleTop
            st['other'] = pt.ModuleTop
        """

        # Checking if the library being loaded is supported by pytropos
        non_supported_modules: 'List[str]' = []
        modules_supported: 'List[Tuple[str, Optional[str]]]' = []
        for alias in node.names:
            if alias.name in self._supported_modules:
                modules_supported.append(
                    (alias.name, alias.asname))  # noqa: E201,E202
            else:
                non_supported_modules.append(
                    alias.name if alias.asname is None else alias.asname)

        # Loading fake modules from pytropos (if supported)
        libs: 'List[ast3.AST]' = []
        if len(modules_supported) > 0:
            libs.append(
                ast3.ImportFrom(
                    module='pytropos.libs_checking',
                    names=[
                        ast3.alias(name=self._supported_modules[name],
                                   asname=None)
                        for [name, asname] in modules_supported
                    ],
                    level=0,
                ))
            libs.extend(
                ast3.parse(  # type: ignore
                    '\n'.join([
                        f"st['{asname}'] = {self._supported_modules[name]}"
                        for name, asname in modules_supported
                    ])).body)

        # Loading modules as Any (if not supported)
        if non_supported_modules:
            libs.extend(
                ast3.parse(  # type: ignore
                    '\n'.join([
                        f"st['{name}'] = pt.ModuleTop"
                        for name in non_supported_modules
                    ])).body)

        return libs