Skip to content

Commit

Permalink
Inihbit AST transformations inside ClassDef nodes
Browse files Browse the repository at this point in the history
Without this inhibition, the following test case

    >>> %reload_ext awaitless
    >>> import asyncio
    >>> class Foo:
    ...     x = asyncio.sleep(0)

would trigger AST transformation - and we can't use awaits in class
definitions.
  • Loading branch information
Graeme Smecher committed Aug 22, 2024
1 parent 026b572 commit c60fc9b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions awaitless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def visit_AsyncFunctionDef(self, node):

return node

def visit_ClassDef(self, node):
"""
Inhibit AST transformation inside ClassDef nodes.
We need to prevent assignments inside class bodies from being
transformed, since an "await" there is illegal. (Functions defined in
class bodies are already covered.)
"""

return node

def visit_Expr(self, node):
"""
Rewrite Expr nodes
Expand Down
8 changes: 8 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,11 @@ def doctest_lambda():
The actual conversion from coroutine to task occurred in the last line,
which was an Expr.
"""


def doctest_assignment_in_classdef():
"""
>>> %reload_ext awaitless
>>> import asyncio
>>> class Foo: x = asyncio.sleep(0)
"""

0 comments on commit c60fc9b

Please sign in to comment.