-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix unittest.mock.patch and unittest.mock.patch.object when new_callable is not None #14358
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
base: main
Are you sure you want to change the base?
Fix unittest.mock.patch and unittest.mock.patch.object when new_callable is not None #14358
Conversation
This comment has been minimized.
This comment has been minimized.
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.
Would you be interested in adding a few test cases to https://github.com/python/typeshed/blob/main/stdlib/%40tests/test_cases/check_unittest.py ? (the unittest.mock stubs can get a little tricky)
sure! |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
I've updated the original PR description to explain what this PR is fixing |
@@ -5,9 +5,9 @@ | |||
from datetime import datetime, timedelta |
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.
Test result with old implementation
check_unittest.py:190: error: Expression is of type "MagicMock | AsyncMock", not "int" [assert-type]
check_unittest.py:213: error: Missing positional argument "mock" in call to "obj_f_default_new" [call-arg]
check_unittest.py:214: error: Missing positional argument "mock" in call to "obj_f_default_new" [call-arg]
check_unittest.py:214: error: Argument 1 to "obj_f_default_new" has incompatible type "str"; expected "int" [arg-type]
check_unittest.py:217: error: Missing positional argument "new_callable_ret" in call to "obj_f_explicit_new_callable" [call-arg]
check_unittest.py:218: error: Missing positional argument "new_callable_ret" in call to "obj_f_explicit_new_callable" [call-arg]
check_unittest.py:218: error: Argument 1 to "obj_f_explicit_new_callable" has incompatible type "str"; expected "int" [arg-type]
check_unittest.py:228: error: Expression is of type "MagicMock | AsyncMock", not "int" [assert-type]
This agrees with the upated PR description:
This PR fixes multiple issues with
unittest.mock.patch
andunittest.mock.patch.object
Both should only accept
Callable | None
fornew_callable
, currentlyAny | None
When used as context managers, both should return
T
fornew_callable: Callable[..., T]
, currently they both returnMock | AsyncMock
When used as decorators to functions:
- When neither
new
nornew_callable
is given, the function signature should be updated (the mock is passed as last parameter),patch
already has the correct behavior butpatch.object
does not- When
new_callable
is given, same as above, should update function signature.patch
already has the correct behavior (by chance, explicit new_callable is not actually handled), butpatch.object
does not
This PR fixes multiple issues with
unittest.mock.patch
andunittest.mock.patch.object
Callable | None
fornew_callable
, currentlyAny | None
T
fornew_callable: Callable[..., T]
, currently they both returnMock | AsyncMock
new
nornew_callable
is given, the function signature should be updated (the mock is passed as last parameter),patch
already has the correct behavior butpatch.object
does notnew_callable
is given, same as above, should update function signature.patch
already has the correct behavior (by chance, explicit new_callable is not actually handled), butpatch.object
does notAddresses #14339