Python ast.AsyncWith() Examples

The following are 7 code examples of ast.AsyncWith(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ast , or try the search function .
Example #1
Source File: helper.py    From YAPyPy with MIT License 5 votes vote down vote up
def with_stmt_rewrite(mark, items, body, is_async=False):
    ty = ast.AsyncWith if is_async else ast.With
    return ty(items, body, **loc @ mark) 
Example #2
Source File: manhole.py    From mautrix-python with Mozilla Public License 2.0 5 votes vote down vote up
def insert_returns(body: List[ast.AST]) -> None:
    if isinstance(body[-1], ast.Expr):
        body[-1] = ast.Return(body[-1].value)
        ast.fix_missing_locations(body[-1])
    elif isinstance(body[-1], ast.If):
        insert_returns(body[-1].body)
        insert_returns(body[-1].orelse)
    elif isinstance(body[-1], (ast.With, ast.AsyncWith)):
        insert_returns(body[-1].body) 
Example #3
Source File: test_checker.py    From pyflakes with MIT License 5 votes vote down vote up
def test_py35_node_types(self):
        """
        Test that the PEP 492 node types are collected
        """
        visitor = self._run_visitor(
            """\
async def f():  # async def
    async for x in y:  pass  # async for
    async with a as b: pass  # async with
"""
        )
        self.assertEqual(visitor.typeable_lines, [1, 2, 3])
        self.assertIsInstance(visitor.typeable_nodes[1], ast.AsyncFunctionDef)
        self.assertIsInstance(visitor.typeable_nodes[2], ast.AsyncFor)
        self.assertIsInstance(visitor.typeable_nodes[3], ast.AsyncWith) 
Example #4
Source File: ast3.py    From gast with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_AsyncWith(self, node):
            new_node = gast.AsyncWith(
                self._visit(node.items),
                self._visit(node.body),
                None,  # type_comment
            )
            gast.copy_location(new_node, node)
            return new_node 
Example #5
Source File: ast3.py    From gast with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_AsyncWith(self, node):
            new_node = ast.AsyncWith(
                self._visit(node.items),
                self._visit(node.body),
            )
            ast.copy_location(new_node, node)
            return new_node 
Example #6
Source File: annotate.py    From pasta with Apache License 2.0 5 votes vote down vote up
def visit_With_3(self, node):
    if hasattr(ast, 'AsyncWith') and isinstance(node, ast.AsyncWith):
      self.attr(node, 'with', ['async', self.ws, 'with', self.ws],
                default='async with ')
    else:
      self.attr(node, 'with', ['with', self.ws], default='with ')

    for i, withitem in enumerate(node.items):
      self.visit(withitem)
      if i != len(node.items) - 1:
        self.token(',')

    self.attr(node, 'with_body_open', [':', self.ws_oneline], default=':\n')
    for stmt in self.indented(node, 'body'):
      self.visit(stmt) 
Example #7
Source File: bugfixes.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def fix_async_offset(tree: ast.AST) -> ast.AST:
    """
    Fixes ``col_offest`` values for async nodes.

    This is a temporary check for async-based expressions, because offset
    for them isn't calculated properly. We can calculate right version
    of offset with subscripting ``6`` (length of "async " part).

    Affected ``python`` versions:

    - all versions below ``python3.6.7``

    Read more:
        https://bugs.python.org/issue29205
        https://github.com/wemake-services/wemake-python-styleguide/issues/282

    """
    nodes_to_fix = (
        ast.AsyncFor,
        ast.AsyncWith,
        ast.AsyncFunctionDef,
    )
    for node in ast.walk(tree):
        if isinstance(node, nodes_to_fix):
            error = 6 if node.col_offset % 4 else 0
            node.col_offset = node.col_offset - error
    return tree