Skip to content

Commit 66797fc

Browse files
authored
[mypyc] Fix calling base class async method using super() (#20254)
Fixes mypyc/mypyc#1154. The next label integer value was incorrectly passed as the `self` argument.
1 parent 0c6593b commit 66797fc

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

mypyc/irbuild/expression.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,12 @@ def translate_super_method_call(builder: IRBuilder, expr: CallExpr, callee: Supe
504504
if decl.kind == FUNC_CLASSMETHOD:
505505
vself = builder.primitive_op(type_op, [vself], expr.line)
506506
elif builder.fn_info.is_generator:
507-
# For generator classes, the self target is the 6th value
507+
# For generator classes, the self target is the 7th value
508508
# in the symbol table (which is an ordered dict). This is sort
509509
# of ugly, but we can't search by name since the 'self' parameter
510510
# could be named anything, and it doesn't get added to the
511511
# environment indexes.
512-
self_targ = list(builder.symtables[-1].values())[6]
512+
self_targ = list(builder.symtables[-1].values())[7]
513513
vself = builder.read(self_targ, builder.fn_info.fitem.line)
514514
arg_values.insert(0, vself)
515515
arg_kinds.insert(0, ARG_POS)

mypyc/test-data/run-async.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,5 +1382,20 @@ async def trait_foo(o: TraitBase, x: int) -> int:
13821382
def test_override_trait() -> None:
13831383
assert asyncio.run(trait_foo(DerivedFromTrait(), 7)) == 10
13841384

1385+
class Base5:
1386+
def __init__(self) -> None:
1387+
self._name = "test"
1388+
1389+
async def foo(self, x: int) -> int:
1390+
assert self._name == "test"
1391+
return x + 11
1392+
1393+
class Derived5(Base5):
1394+
async def foo(self, x: int) -> int:
1395+
return await super().foo(x) + 22
1396+
1397+
def test_call_using_super() -> None:
1398+
assert asyncio.run(Derived5().foo(5)) == 38
1399+
13851400
[file asyncio/__init__.pyi]
13861401
def run(x: object) -> object: ...

0 commit comments

Comments
 (0)