Wemake-python-styleguide: Feature: test async list comprehensions

Created on 1 Oct 2018  路  3Comments  路  Source: wemake-services/wemake-python-styleguide

Currently WrongListComprehensionVisitor is not tested with async for comprehensions.
It might work, it might not work.

What needs to be done?

  1. We need to provider new unit tests for the async for cases here: https://github.com/wemake-services/wemake-python-styleguide/blob/master/tests/test_visitors/test_ast/test_general/test_wrong_keyword/test_comprehensions.py
  2. I am pretty sure that we do not need to copy paste all data, just use something like: 'nodes = [node {0}for node in 'abc']' and then format this string with either 'async ' or ''
Hacktoberfest feature help wanted starter

Most helpful comment

Yeah, I'll do it.

All 3 comments

It is not that simple.
Ast can't parse single async comprehension without async function wrapped around.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 2
    nodes = [node async for node in 'abc' if node != 'a' if node != 'b' if node != 'c']
                      ^
SyntaxError: invalid syntax

@sobolevn how about wrap all comprehensions with something like

async_wrapper = """
async def d():
    {comprehension}
    """

list_ifs_multiple = """
nodes = [node {async}for node in 'abc' if node != 'a' if node != 'b' if node != 'c']
"""

@AlwxSin hm, I have totally missed it!

Yes, your solution seems reasonable. Let's do that. Are you willing to put this together in a PR?
Is it worth to create a fixture called async_wrapper that can be reused latter for other tests?

Yeah, I'll do it.

Was this page helpful?
0 / 5 - 0 ratings