77from fractions import Fraction
88from typing import TypedDict
99from typing_extensions import assert_type
10- from unittest .mock import MagicMock , Mock , patch , AsyncMock
10+ from unittest .mock import AsyncMock , MagicMock , Mock , patch
1111
1212case = unittest .TestCase ()
1313
@@ -153,17 +153,20 @@ def f_default_new(i: int, mock: MagicMock) -> str:
153153def f_explicit_new (i : int ) -> str :
154154 return "asdf"
155155
156+
156157@patch ("sys.exit" , new_callable = lambda : 42 )
157158def f_explicit_new_callable (i : int , new_callable_ret : int ) -> str :
158159 return "asdf"
159160
161+
160162assert_type (f_default_new (1 ), str )
161163f_default_new ("a" ) # Not an error due to ParamSpec limitations
162164assert_type (f_explicit_new (1 ), str )
163165f_explicit_new ("a" ) # type: ignore[arg-type]
164166assert_type (f_explicit_new_callable (1 ), str )
165167f_explicit_new_callable ("a" ) # Same as default new
166168
169+
167170@patch ("sys.exit" , new = Mock ())
168171class TestXYZ (unittest .TestCase ):
169172 attr : int = 5
@@ -191,6 +194,7 @@ def method() -> int:
191194# Tests for mock.patch.object
192195###
193196
197+
194198@patch .object (Decimal , "exp" )
195199def obj_f_default_new (i : int , mock : MagicMock ) -> str :
196200 return "asdf"
@@ -200,10 +204,12 @@ def obj_f_default_new(i: int, mock: MagicMock) -> str:
200204def obj_f_explicit_new (i : int ) -> str :
201205 return "asdf"
202206
207+
203208@patch .object (Decimal , "exp" , new_callable = lambda : 42 )
204209def obj_f_explicit_new_callable (i : int , new_callable_ret : int ) -> str :
205210 return "asdf"
206211
212+
207213assert_type (obj_f_default_new (1 ), str )
208214obj_f_default_new ("a" ) # Not an error due to ParamSpec limitations
209215assert_type (obj_f_explicit_new (1 ), str )
@@ -212,7 +218,6 @@ def obj_f_explicit_new_callable(i: int, new_callable_ret: int) -> str:
212218obj_f_explicit_new_callable ("a" ) # Same as default new
213219
214220
215-
216221with patch .object (Decimal , "exp" ) as obj_default_new_enter :
217222 assert_type (obj_default_new_enter , MagicMock | AsyncMock )
218223
0 commit comments