facebook / facebook/TestSlide

Type error when using contextlib.asynccontextmanager

未关闭
#193 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
bug help wanted
主要语言
Python
星标
149
派生
62
PR 合并指标
30 天内没有已合并 PR

描述

I have 2 Python file foo.py and test_foo.py.
foo.py:
```
from contextlib import asynccontextmanager
from typing import AsyncGenerator
class Foo:
async def bar()->int:
print("Calling func bar")
return 1
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
return

@asynccontextmanager
async def get_cm() -> AsyncGenerator[Foo, None]:
yield Foo()

async def call_bar()->int:
async with get_cm() as foo:
return await foo.bar()

```
test_foo.py:
```
from testslide.dsl import context
from foo import call_bar,Foo
from testslide import StrictMock

module = "foo"

@context
def foo_test(context):
context.memoize(
"foo_mock", lambda self: StrictMock(template=Foo, default_context_manager=True)
)

@context.before
async def mock_get_cm(self):
self.mock_callable(module, "get_cm").to_return_value(
self.foo_mock
)
self.mock_async_callable(self.foo_mock, "bar").to_return_value(3)
@context.example
async def test_bar(self):
self.assertEqual(await call_bar(),3)

```
When I ran testslide test_foo.py. I hit this type error below.
```
Failures:

1) foo test: test bar
1) TypeError: type of return must be collections.abc.AsyncGenerator; got testslide.strict_mock.FooStrictMock instead:
Defined at /Users/qxue/testslide_test.py:15
File ".pyenv/versions/3.8.2/lib/python3.8/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File ".pyenv/versions/3.8.2/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "testslide_test.py", line 21, in test_bar
self.assertEqual(await call_bar(),3)
File "foo.py", line 20, in call_bar
async with get_cm() as foo:
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。