Skip to content

Commit 0ccbe65

Browse files
authored
Merge pull request #25 from davidvilla/fix/mimic-generic
Support making Mimics of Generic sub-classes
2 parents ac255cc + a64669c commit 0ccbe65

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Unreleased
77
- Mock support for classmethods on instances
88
- Fix namedtuple support for python 3.8 and newer
99
- Fix issue with property stubbing using when() leaving stubs in setting up state
10+
- Allow making Mimics of Generic sub-classes
1011

1112
20190405
1213
========

doublex/doubles.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
import inspect
23+
from typing import Generic
2324

2425
import hamcrest
2526

@@ -179,9 +180,10 @@ def _get_method(self, key):
179180
"Mimic() takes a double class as first argument (got %s instead)" & double
180181

181182
collab_class = get_class(collab)
183+
base_classes = tuple(base for base in collab_class.__bases__ if base is not Generic)
182184
generated_class = type(
183185
"Mimic_%s_for_%s" % (double.__name__, collab_class.__name__),
184-
(double, collab_class) + collab_class.__bases__,
186+
(double, collab_class) + base_classes,
185187
dict(_methods = {},
186188
__getattribute__ = __getattribute__hook,
187189
_get_method = _get_method))

doublex/test/unit_tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import thread
2727
except ImportError:
2828
import _thread as thread
29+
from typing import Generic, TypeVar
2930

3031
from unittest import TestCase, skipIf
3132

@@ -51,6 +52,8 @@
5152
from doublex.matchers import MatcherRequiredError
5253
from doublex.internal import InvocationContext, Method
5354

55+
T = TypeVar('T')
56+
5457

5558
class InvocationContextTests(TestCase):
5659
def test_order(self):
@@ -1086,6 +1089,10 @@ class B(A):
10861089
def method_b(self):
10871090
return "hi"
10881091

1092+
class GenericSubclass(Generic[T]):
1093+
def method_c(self):
1094+
return "hello"
1095+
10891096
def test_normal_spy_does_not_inherit_collaborator_superclasses(self):
10901097
spy = Spy(self.B)
10911098
assert_that(not isinstance(spy, self.B))
@@ -1135,6 +1142,13 @@ def test_mimic_mock_works(self):
11351142

11361143
assert_that(mock, verify())
11371144

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+
11381152

11391153
class PropertyTests(TestCase):
11401154
def test_stub_notset_property_is_None(self):

0 commit comments

Comments
 (0)