Skip to content

mock.patch generic incorrectly inferred from new= argument #5533

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

Closed
PeterJCLaw opened this issue May 26, 2021 · 2 comments · Fixed by #5534
Closed

mock.patch generic incorrectly inferred from new= argument #5533

PeterJCLaw opened this issue May 26, 2021 · 2 comments · Fixed by #5534

Comments

@PeterJCLaw
Copy link
Contributor

To Reproduce

from unittest import mock

x: mock._patch[int] = mock.patch(
    'something',
    new=2,
)

Results in error:

Incompatible types in assignment (expression has type "_patch[MagicMock]", variable has type "_patch[int]")

I've had a quick look at the typeshed (

class _patcher:
TEST_PREFIX: str
dict: Type[_patch_dict]
if sys.version_info >= (3, 8):
@overload
def __call__( # type: ignore
self,
target: Any,
*,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
autospec: Optional[Any] = ...,
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[Union[MagicMock, AsyncMock]]: ...
# This overload also covers the case, where new==DEFAULT. In this case, the return type is _patch[Any].
# Ideally we'd be able to add an overload for it so that the return type is _patch[MagicMock],
# but that's impossible with the current type system.
@overload
def __call__(
self,
target: Any,
new: _T,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
autospec: Optional[Any] = ...,
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[_T]: ...
else:
@overload
def __call__( # type: ignore
self,
target: Any,
*,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
autospec: Optional[Any] = ...,
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[MagicMock]: ...
@overload
def __call__(
self,
target: Any,
new: _T,
spec: Optional[Any] = ...,
create: bool = ...,
spec_set: Optional[Any] = ...,
autospec: Optional[Any] = ...,
new_callable: Optional[Any] = ...,
**kwargs: Any,
) -> _patch[_T]: ...
) and it looks like it's doing the right thing, hence reporting as a mypy bug. I could definitely be convinced otherwise though.

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: none
  • Python version used: 3.7.10
  • Operating system and version: Ubuntu 20.04
@JelleZijlstra
Copy link
Member

I wonder if the order of the two overloads should be flipped. Your code matches the first overload (the new just goes into **kwargs: Any) and also the second, but the second is what we want.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented May 26, 2021

I think Jelle's right since if you pass the arg without keyword it works, opening a PR

@hauntsaninja hauntsaninja transferred this issue from python/mypy May 26, 2021
hauntsaninja pushed a commit to hauntsaninja/typeshed that referenced this issue May 26, 2021
Fixes python#5533. This changes the order and in some cases removes default
values from new.
srittau pushed a commit that referenced this issue May 26, 2021
* mock: fix overloads

Fixes #5533. This changes the order and in some cases removes default
values from new.

* ignore incompatible overlaps

Co-authored-by: hauntsaninja <>
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 a pull request may close this issue.

3 participants