Commit d308c0b
authored
[mypyc] Fix generation of function wrappers for decorated functions (#20584)
For decorated functions, a `FuncDecl` is constructed during compilation
from another `FuncDecl` but some attributes are not copied. One of those
is `is_coroutine` which is used to determine whether the function will
be replaced in the module dictionary by a `CPyFunction` object.
The `CPyFunction` object is needed for introspection functions to work
so without it, `inspect.iscoroutinefunction(fn)` returns `False` for
decorated async functions. This causes incorrect behavior when the
`inspect` function is used in a decorator, for example:
```python
F = TypeVar("F", bound=Callable[..., Any])
def wrap(fn: F) -> F:
if inspect.iscoroutinefunction(fn):
@wraps(fn)
async def wrapper_async(*args) -> Any:
return await fn(*args) + await fn(*args)
return cast(F, wrapper_async)
@wraps(fn)
def wrapper(*args) -> Any:
return fn(*args) + fn(*args)
return cast(F, wrapper)
@Wrap
async def wrapped_async(val: int) -> int:
return val
```
In existing test cases this did not come up because the decorators would
unconditionally wrap an async function with another async function.
Checking `iscoroutinefunction` of the wrapped function would check the
wrapper instead, for which a `CPyFunction` object was generated
correctly. But during execution of `wrap`, the actual wrapped function
is checked.1 parent 131f9d9 commit d308c0b
2 files changed
+15
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
365 | 365 | | |
366 | 366 | | |
367 | 367 | | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
368 | 372 | | |
369 | 373 | | |
370 | 374 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1432 | 1432 | | |
1433 | 1433 | | |
1434 | 1434 | | |
1435 | | - | |
1436 | | - | |
1437 | | - | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
1438 | 1439 | | |
1439 | | - | |
| 1440 | + | |
1440 | 1441 | | |
1441 | | - | |
1442 | 1442 | | |
1443 | | - | |
1444 | | - | |
| 1443 | + | |
| 1444 | + | |
1445 | 1445 | | |
1446 | 1446 | | |
1447 | 1447 | | |
| |||
1453 | 1453 | | |
1454 | 1454 | | |
1455 | 1455 | | |
1456 | | - | |
| 1456 | + | |
1457 | 1457 | | |
1458 | 1458 | | |
1459 | 1459 | | |
1460 | | - | |
| 1460 | + | |
1461 | 1461 | | |
1462 | 1462 | | |
1463 | 1463 | | |
| |||
1472 | 1472 | | |
1473 | 1473 | | |
1474 | 1474 | | |
1475 | | - | |
| 1475 | + | |
1476 | 1476 | | |
1477 | 1477 | | |
1478 | 1478 | | |
| |||
1569 | 1569 | | |
1570 | 1570 | | |
1571 | 1571 | | |
1572 | | - | |
| 1572 | + | |
1573 | 1573 | | |
1574 | 1574 | | |
1575 | 1575 | | |
| |||
0 commit comments