async callable not awaited in side_effect for AsyncMock
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
In the AsyncMockMixin._execute_mock_call, the following test is done:
if effect is not None:
if _is_exception(effect):
raise effect
elif not _callable(effect):
try:
result = next(effect)
except StopIteration:
# It is impossible to propagate a StopIteration
# through coroutines because of PEP 479
raise StopAsyncIteration
if _is_exception(result):
raise result
elif iscoroutinefunction(effect):
result = await effect(*args, **kwargs)
else:
result = effect(*args, **kwargs)
However elif iscoroutinefunction(effect) will return False for an awaitable that was defined this way:
class AsyncCallable:
async def __call__(self, *args, **kwargs):
# await stuff
pass
Note that __call__ is awaitable here but not detected as such.
iscoroutinefunction(effect.__call__) would return True in such a case however, so the fix might be as simple as:
elif iscoroutinefunction(effect) or iscoroutinefunction(getattr(effect, '__call__', None)):
In the meantime, the workaround is to add markcoroutinefunction(self) to the class.
class AsyncCallable:
def __init__(self, *args, **kwargs):
# init code
markcoroutinefunction(self) # workaround for iscoroutinefunction check in side_effect to return True
...
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
- gh-156465
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong Lib/unittest/mock.py tại AsyncMockMixin._execute_mock_call và xem xét PR được liên kết gh-156465. Xác nhận hành vi đối với một đối tượng có phương thức call bất đồng bộ được dùng làm AsyncMock.side_effect, sau đó thêm hoặc cập nhật phạm vi kiểm thử hồi quy để callable đó được await đúng cách.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- testing-qa
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 35/100