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.
hauntsaninja
left a comment
There was a problem hiding this comment.
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.
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.patchandunittest.mock.patch.object
Both should only accept
Callable | Nonefornew_callable, currentlyAny | NoneWhen used as context managers, both should return
Tfornew_callable: Callable[..., T], currently they both returnMock | AsyncMockWhen used as decorators to functions:
- When neither
newnornew_callableis given, the function signature should be updated (the mock is passed as last parameter),patchalready has the correct behavior butpatch.objectdoes not- When
new_callableis given, same as above, should update function signature.patchalready has the correct behavior (by chance, explicit new_callable is not actually handled), butpatch.objectdoes not
This PR fixes multiple issues with
unittest.mock.patchandunittest.mock.patch.objectCallable | Nonefornew_callable, currentlyAny | NoneTfornew_callable: Callable[..., T], currently they both returnMock | AsyncMocknewnornew_callableis given, the function signature should be updated (the mock is passed as last parameter),patchalready has the correct behavior butpatch.objectdoes notnew_callableis given, same as above, should update function signature.patchalready has the correct behavior (by chance, explicit new_callable is not actually handled), butpatch.objectdoes notAddresses #14339