-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add stubs for AsyncMock #4752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add stubs for AsyncMock #4752
Conversation
Any updates? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also worried about the Union type here (and maybe the typevar should be covariant?). If this boils down to a python/typing#566 situation, we typically don't merge. But maybe we can get away with it this time because there's very little in MagicMock that isn't in AsyncMock and everything seems to inherit from Any and have __getattr__
anyway, so maybe it still works out?
I have a hard time reasoning about this stub: the inheritance from Any and the patch _patcher _patch dance doesn't help. Our track record isn't great here either, IIRC the last couple PRs merged to unittest.mock had unforeseen issues. This would be a good candidate for #1339 style tests.
@erictraut Maybe you could check to see if this causes regressions?
@hauntsaninja, for what it's worth, I applied the changes in this PR and ran pyright over my team's code base (250K lines of Python including a bunch of unit tests that make use of the mock module). I didn't observe any signs of regressions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry for the late reply. LGTM, one optional remark below.
if sys.version_info >= (3, 8): | ||
@overload | ||
def __init__( | ||
self: _patch[Union[MagicMock, AsyncMock]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking over it again, this overload seems wrong to me, even before the PR. This defines the default type of a patch, if new
is not specified. This can never happen as it is a required argument. We should probably just get rid of all overloads and version checks for the _patch.__init__()
constructor and just keep the second overload (where new: _T
).
If you want to, you can do that, otherwise I'd do it in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@srittau Thanks for the feedback. I think I know what you mean. However, since I'm relatively new to typing stubs it might be faster if you do it in a separate PR instead of multiple back and forth if that's ok with you.
Previously
AsyncMock
was typed asAny
. I had to change the overloads for_patch
and_patcher
since technically they return either aMagicMock
or anAsyncMock
depending on the function being patched.