Skip to content

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

Merged
merged 3 commits into from
Dec 14, 2020
Merged

Add stubs for AsyncMock #4752

merged 3 commits into from
Dec 14, 2020

Conversation

cdce8p
Copy link
Contributor

@cdce8p cdce8p commented Nov 9, 2020

Previously AsyncMock was typed as Any. I had to change the overloads for _patch and _patcher since technically they return either a MagicMock or an AsyncMock depending on the function being patched.

@cdce8p
Copy link
Contributor Author

cdce8p commented Nov 29, 2020

Any updates?

Copy link
Collaborator

@hauntsaninja hauntsaninja left a 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?

@erictraut
Copy link
Contributor

@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.

Copy link
Collaborator

@srittau srittau left a 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]],
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@srittau srittau merged commit e5d3a47 into python:master Dec 14, 2020
@cdce8p cdce8p deleted the async-mock branch December 14, 2020 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants