From a9408fb68a626716a3ef312757eccc63193cf020 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 26 May 2021 11:50:13 -0700 Subject: [PATCH 1/2] mock: fix overloads Fixes #5533. This changes the order and in some cases removes default values from new. --- stdlib/unittest/mock.pyi | 42 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/stdlib/unittest/mock.pyi b/stdlib/unittest/mock.pyi index e95d13978bcf..dc49ff8735a0 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__( + 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,9 +231,7 @@ 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__( self, @@ -231,7 +244,6 @@ class _patcher: new_callable: Optional[Any] = ..., **kwargs: Any, ) -> _patch[_T]: ... - else: @overload def __call__( # type: ignore self, @@ -244,10 +256,12 @@ class _patcher: new_callable: Optional[Any] = ..., **kwargs: Any, ) -> _patch[MagicMock]: ... + if sys.version_info >= (3, 8): @overload - def __call__( + def object( self, target: Any, + attribute: str, new: _T, spec: Optional[Any] = ..., create: bool = ..., @@ -256,7 +270,6 @@ class _patcher: new_callable: Optional[Any] = ..., **kwargs: Any, ) -> _patch[_T]: ... - if sys.version_info >= (3, 8): @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( 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, From db2bfecb248d0ee28b8a7802b1226b0ddd6d0b2a Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 26 May 2021 12:23:00 -0700 Subject: [PATCH 2/2] ignore incompatible overlaps --- stdlib/unittest/mock.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/unittest/mock.pyi b/stdlib/unittest/mock.pyi index dc49ff8735a0..40cb7743362a 100644 --- a/stdlib/unittest/mock.pyi +++ b/stdlib/unittest/mock.pyi @@ -208,7 +208,7 @@ class _patcher: # 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__( + def __call__( # type: ignore self, target: Any, new: _T, @@ -233,7 +233,7 @@ class _patcher: ) -> _patch[Union[MagicMock, AsyncMock]]: ... else: @overload - def __call__( + def __call__( # type: ignore self, target: Any, new: _T, @@ -258,7 +258,7 @@ class _patcher: ) -> _patch[MagicMock]: ... if sys.version_info >= (3, 8): @overload - def object( + def object( # type: ignore self, target: Any, attribute: str, @@ -285,7 +285,7 @@ class _patcher: ) -> _patch[Union[MagicMock, AsyncMock]]: ... else: @overload - def object( + def object( # type: ignore self, target: Any, attribute: str,