MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
185 stars 19 forks source link

[Adjust Rule] Fix SIM104 false positive inside async functions #28

Closed simon-liebehenschel closed 2 years ago

simon-liebehenschel commented 3 years ago

Desired change

Explanation

Python does not support 'yield from' inside async functions

https://www.python.org/dev/peps/pep-0525/#asynchronous-yield-from

If 'flake8-simplify' already can check if a function is "async" or not, then the issue has to be easy to fix.

Example

This is an example where the mentioned rule(s) would currently be suboptimal:

Copied from the https://www.python.org/dev/peps/pep-0525/#asynchronous-yield-from:

async def g2():
    async for v in g1():
        yield v
MartinThoma commented 3 years ago

Thank you for the example, @AIGeneratedUsername :

async def g2():
    async for v in g1():
        yield v

The AST looks like this:

        AsyncFunctionDef(
            name='g2',
            args=arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
            body=[
                AsyncFor(
                    target=Name(id='v', ctx=Store()),
                    iter=Call(
                        func=Name(id='g1', ctx=Load()),
                        args=[],
                        keywords=[],
                    ),
                    body=[
                        Expr(
                            value=Yield(
                                value=Name(id='v', ctx=Load()),
                            ),
                        ),
                    ],
                    orelse=[],
                    type_comment=None,
                ),
            ],
            decorator_list=[],
            returns=None,
            type_comment=None,
        ),
MartinThoma commented 2 years ago

Fixed via https://github.com/MartinThoma/flake8-simplify/pull/94