diff options
author | Colin Watson <cjwatson@canonical.com> | 2022-02-05 22:34:41 +0000 |
---|---|---|
committer | Stephen Finucane <stephenfinucane@hotmail.com> | 2022-02-08 11:44:04 +0000 |
commit | f959638e24b7e37a8e2413a3f6435f6d273004f3 (patch) | |
tree | f836a1b776e3f1ce8df3a01f6db8828f8927182f | |
parent | 79ccf54c74e48e32dfc1ed1792e27658936e25ce (diff) | |
download | fixtures-git-f959638e24b7e37a8e2413a3f6435f6d273004f3.tar.gz |
Fix MockPatchMultiple.DEFAULT
The change in #52 broke
`TestMockMultiple.test_mock_patch_without_replacement`, since
`MockPatchMultiple.DEFAULT` no longer worked as a class property. Fix
this using a metaclass.
For compatibility, I arranged for it to also work as an instance
property; I suspect this isn't much needed since
`MockPatchMultiple.DEFAULT` is normally used as an argument to the
`MockPatchMultiple` constructor so you wouldn't normally have an
instance in hand, but it's easy enough to provide strict compatibility
here.
-rw-r--r-- | fixtures/_fixtures/mockpatch.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/fixtures/_fixtures/mockpatch.py b/fixtures/_fixtures/mockpatch.py index d616979..f80ecb6 100644 --- a/fixtures/_fixtures/mockpatch.py +++ b/fixtures/_fixtures/mockpatch.py @@ -51,8 +51,14 @@ class MockPatch(_Base): self._get_p = lambda: mock.patch(obj, new, **kwargs) -class MockPatchMultiple(_Base): - """Deal with code around mock.patch.multiple.""" +class _MockPatchMultipleMeta(type): + """Arrange for lazy loading of MockPatchMultiple.DEFAULT.""" + + # For strict backward compatibility, ensure that DEFAULT also works as + # an instance property. + def __new__(cls, name, bases, namespace, **kwargs): + namespace['DEFAULT'] = cls.DEFAULT + return super().__new__(cls, name, bases, namespace, **kwargs) # Default value to trigger a MagicMock to be created for a named # attribute. @@ -60,6 +66,10 @@ class MockPatchMultiple(_Base): def DEFAULT(self): return mock.DEFAULT + +class MockPatchMultiple(_Base, metaclass=_MockPatchMultipleMeta): + """Deal with code around mock.patch.multiple.""" + def __init__(self, obj, **kwargs): """Initialize the mocks |