From 74a8900166bb6b5f545fe8ccaf6772884173af77 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Wed, 26 May 2021 13:16:51 -0700 Subject: [PATCH] mock: fix overloads (#5534) * 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 <> --- stdlib/unittest/mock.pyi | 66 ++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/stdlib/unittest/mock.pyi b/stdlib/unittest/mock.pyi index e95d13978..40cb77433 100644 --- a/stdlib/unittest/mock.pyi +++ b/stdlib/unittest/mock.pyi @@ -204,6 +204,21 @@ class _patcher: TEST_PREFIX: str dict: Type[_patch_dict] if sys.version_info >= (3, 8): + # 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__( # type: ignore + 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]: ... @overload def __call__( # type: ignore self, @@ -216,11 +231,9 @@ class _patcher: 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. + else: @overload - def __call__( + def __call__( # type: ignore self, target: Any, new: _T, @@ -231,7 +244,6 @@ class _patcher: new_callable: Optional[Any] = ..., **kwargs: Any, ) -> _patch[_T]: ... - else: @overload def __call__( # type: ignore self, @@ -244,19 +256,20 @@ class _patcher: 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]: ... if sys.version_info >= (3, 8): + @overload + def object( # type: ignore + self, + target: Any, + attribute: str, + new: _T, + spec: Optional[Any] = ..., + create: bool = ..., + spec_set: Optional[Any] = ..., + autospec: Optional[Any] = ..., + new_callable: Optional[Any] = ..., + **kwargs: Any, + ) -> _patch[_T]: ... @overload def object( # type: ignore self, @@ -270,12 +283,13 @@ class _patcher: new_callable: Optional[Any] = ..., **kwargs: Any, ) -> _patch[Union[MagicMock, AsyncMock]]: ... + else: @overload - def object( + def object( # type: ignore self, target: Any, attribute: str, - new: _T = ..., + new: _T, spec: Optional[Any] = ..., create: bool = ..., spec_set: Optional[Any] = ..., @@ -283,7 +297,6 @@ class _patcher: new_callable: Optional[Any] = ..., **kwargs: Any, ) -> _patch[_T]: ... - else: @overload def object( # type: ignore self, @@ -297,19 +310,6 @@ class _patcher: new_callable: Optional[Any] = ..., **kwargs: Any, ) -> _patch[MagicMock]: ... - @overload - def object( - self, - target: Any, - attribute: str, - new: _T = ..., - spec: Optional[Any] = ..., - create: bool = ..., - spec_set: Optional[Any] = ..., - autospec: Optional[Any] = ..., - new_callable: Optional[Any] = ..., - **kwargs: Any, - ) -> _patch[_T]: ... def multiple( self, target: Any,