File tree Expand file tree Collapse file tree 3 files changed +18
-1
lines changed Expand file tree Collapse file tree 3 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ Unreleased
7
7
- Mock support for classmethods on instances
8
8
- Fix namedtuple support for python 3.8 and newer
9
9
- Fix issue with property stubbing using when() leaving stubs in setting up state
10
+ - Allow making Mimics of Generic sub-classes
10
11
11
12
20190405
12
13
========
Original file line number Diff line number Diff line change 20
20
21
21
22
22
import inspect
23
+ from typing import Generic
23
24
24
25
import hamcrest
25
26
@@ -179,9 +180,10 @@ def _get_method(self, key):
179
180
"Mimic() takes a double class as first argument (got %s instead)" & double
180
181
181
182
collab_class = get_class (collab )
183
+ base_classes = tuple (base for base in collab_class .__bases__ if base is not Generic )
182
184
generated_class = type (
183
185
"Mimic_%s_for_%s" % (double .__name__ , collab_class .__name__ ),
184
- (double , collab_class ) + collab_class . __bases__ ,
186
+ (double , collab_class ) + base_classes ,
185
187
dict (_methods = {},
186
188
__getattribute__ = __getattribute__hook ,
187
189
_get_method = _get_method ))
Original file line number Diff line number Diff line change 26
26
import thread
27
27
except ImportError :
28
28
import _thread as thread
29
+ from typing import Generic , TypeVar
29
30
30
31
from unittest import TestCase , skipIf
31
32
51
52
from doublex .matchers import MatcherRequiredError
52
53
from doublex .internal import InvocationContext , Method
53
54
55
+ T = TypeVar ('T' )
56
+
54
57
55
58
class InvocationContextTests (TestCase ):
56
59
def test_order (self ):
@@ -1086,6 +1089,10 @@ class B(A):
1086
1089
def method_b (self ):
1087
1090
return "hi"
1088
1091
1092
+ class GenericSubclass (Generic [T ]):
1093
+ def method_c (self ):
1094
+ return "hello"
1095
+
1089
1096
def test_normal_spy_does_not_inherit_collaborator_superclasses (self ):
1090
1097
spy = Spy (self .B )
1091
1098
assert_that (not isinstance (spy , self .B ))
@@ -1135,6 +1142,13 @@ def test_mimic_mock_works(self):
1135
1142
1136
1143
assert_that (mock , verify ())
1137
1144
1145
+ def test_mimic_generic_subclass_works (self ):
1146
+ stub = Mimic (Stub , self .GenericSubclass )
1147
+ with stub :
1148
+ stub .method_c ().returns ("hello" )
1149
+
1150
+ assert_that (stub .method_c (), is_ ("hello" ))
1151
+
1138
1152
1139
1153
class PropertyTests (TestCase ):
1140
1154
def test_stub_notset_property_is_None (self ):
You can’t perform that action at this time.
0 commit comments