Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypyc/irbuild/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,10 @@ def adjust_generator_classes_of_methods(mapper: Mapper) -> None:
if subcls is None:
# Override could be of a different type, so we can't make assumptions.
precise_ret_type = False
elif class_ir.is_trait:
# Give up on traits. We could possibly have an abstract base class
# for generator return types to make this use precise types.
precise_ret_type = False
else:
for s in subcls:
if name in s.method_decls:
Expand Down
19 changes: 19 additions & 0 deletions mypyc/test-data/run-async.test
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,8 @@ from typing import final, Coroutine, Any, TypeVar

import asyncio

from mypy_extensions import trait

class Base1:
async def foo(self) -> int:
return 1
Expand Down Expand Up @@ -1363,5 +1365,22 @@ def test_override_non_async() -> None:
assert asyncio.run(base3_foo(Base3())) == 7
assert asyncio.run(base3_foo(Derived3())) == 8

class Base4: pass

@trait
class TraitBase:
async def foo(self, value: int) -> int:
raise NotImplementedError()

class DerivedFromTrait(Base4, TraitBase):
async def foo(self, value: int) -> int:
return value + 3

async def trait_foo(o: TraitBase, x: int) -> int:
return await o.foo(x)

def test_override_trait() -> None:
assert asyncio.run(trait_foo(DerivedFromTrait(), 7)) == 10

[file asyncio/__init__.pyi]
def run(x: object) -> object: ...