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,