Skip to content

Commit e14f305

Browse files
authored
Merge pull request #127 from nicoddemus/black-pre-commit
Use pre-commit and black
2 parents 970075f + 6951073 commit e14f305

File tree

9 files changed

+295
-224
lines changed

9 files changed

+295
-224
lines changed

.pre-commit-config.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
exclude: '^($|.*\.bin)'
2+
repos:
3+
- repo: https://github.com/ambv/black
4+
rev: 18.6b4
5+
hooks:
6+
- id: black
7+
args: [--safe, --quiet]
8+
language_version: python3.6
9+
- repo: https://github.com/pre-commit/pre-commit-hooks
10+
rev: v1.3.0
11+
hooks:
12+
- id: trailing-whitespace
13+
- id: end-of-file-fixer
14+
- repo: local
15+
hooks:
16+
- id: rst
17+
name: rst
18+
entry: rst-lint --encoding utf-8
19+
files: ^(CHANGELOG.rst|README.rst|HOWTORELEASE.rst|changelog/.*)$
20+
language: python
21+
additional_dependencies: [pygments, restructuredtext_lint]
22+
python_version: python3.6

CHANGELOG.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,3 @@ First release.
331331
.. _@rouge8: https://github.com/rouge8
332332
.. _@The-Compiler: https://github.com/The-Compiler
333333
.. _@tigarmo: https://github.com/tigarmo
334-
335-

HOWTORELEASE.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Here are the steps on how to make a new release.
2+
3+
1. Create a ``release-VERSION`` branch from ``upstream/master``.
4+
2. Update ``CHANGELOG.rst``.
5+
3. Push a branch with the changes.
6+
4. Once all builds pass, push a tag to ``upstream``.
7+
5. Merge the PR.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
SOFTWARE.

README.rst

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ of a test:
1010
.. code-block:: python
1111
1212
import os
13-
13+
1414
class UnixFS:
15-
15+
1616
@staticmethod
1717
def rm(filename):
1818
os.remove(filename)
19-
19+
2020
def test_unix_fs(mocker):
2121
mocker.patch('os.remove')
2222
UnixFS.rm('file')
2323
os.remove.assert_called_once_with('file')
2424
2525
26-
.. Using PNG badges because PyPI doesn't support SVG
2726
28-
|python| |version| |anaconda| |ci| |appveyor| |coverage|
27+
|python| |version| |anaconda| |ci| |appveyor| |coverage| |black|
2928

3029
.. |version| image:: http://img.shields.io/pypi/v/pytest-mock.svg
3130
:target: https://pypi.python.org/pypi/pytest-mock
@@ -44,13 +43,16 @@ of a test:
4443

4544
.. |python| image:: https://img.shields.io/pypi/pyversions/pytest-mock.svg
4645
:target: https://pypi.python.org/pypi/pytest-mock/
47-
48-
46+
47+
.. |black| image:: https://img.shields.io/badge/code%20style-black-000000.svg
48+
:target: https://github.com/ambv/black
49+
50+
4951
.. image:: http://www.opensourcecitizen.org/badge?url=github.com/pytest-dev/pytest-mock
5052
:target: http://www.opensourcecitizen.org/project?url=github.com/pytest-dev/pytest-mock
5153

5254
If you found this library useful, donate some CPU cycles to its
53-
development efforts by clicking above. Thank you! 😇
55+
development efforts by clicking above. Thank you! 😇
5456

5557
Usage
5658
=====
@@ -160,8 +162,8 @@ diff::
160162
E Right contains more items:
161163
E {'bar': 4}
162164
E Use -v to get the full diff
163-
164-
165+
166+
165167
test_foo.py:6: AssertionError
166168
========================== 1 failed in 0.03 seconds ===========================
167169

@@ -322,6 +324,26 @@ temporarily:
322324
return my_cm
323325
324326
327+
Contributing
328+
============
329+
330+
Contributions are welcome! After cloning the repository, create a virtual env
331+
and install ``pytest-mock`` in editable mode with ``dev`` extras:
332+
333+
.. code-block:: console
334+
335+
$ pip install --editable .[dev]
336+
$ pre-commit install
337+
338+
Tests are run with ``tox``, you can run the baseline environments before submitting a PR:
339+
340+
.. code-block:: console
341+
342+
$ tox -e py27,py36,linting
343+
344+
Style checks and formatting are done automatically during commit courtesy of
345+
`pre-commit <https://pre-commit.com>`_.
346+
325347
License
326348
=======
327349

pytest_mock.py

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ def _get_mock_module(config):
2222
"unittest.mock" for Python 3, but the user can force to always use "mock" on Python 3 using
2323
the mock_use_standalone_module ini option.
2424
"""
25-
if not hasattr(_get_mock_module, '_module'):
26-
use_standalone_module = parse_ini_boolean(config.getini('mock_use_standalone_module'))
25+
if not hasattr(_get_mock_module, "_module"):
26+
use_standalone_module = parse_ini_boolean(
27+
config.getini("mock_use_standalone_module")
28+
)
2729
if sys.version_info[0] == 2 or use_standalone_module:
2830
import mock
31+
2932
_get_mock_module._module = mock
3033
else:
3134
import unittest.mock
35+
3236
_get_mock_module._module = unittest.mock
3337

3438
return _get_mock_module._module
@@ -100,8 +104,7 @@ def spy(self, obj, name):
100104
if isinstance(value, (classmethod, staticmethod)):
101105
autospec = False
102106

103-
result = self.patch.object(obj, name, side_effect=method,
104-
autospec=autospec)
107+
result = self.patch.object(obj, name, side_effect=method, autospec=autospec)
105108
return result
106109

107110
def stub(self, name=None):
@@ -134,7 +137,7 @@ def _start_patch(self, mock_func, *args, **kwargs):
134137
p = mock_func(*args, **kwargs)
135138
mocked = p.start()
136139
self._patches.append(p)
137-
if hasattr(mocked, 'reset_mock'):
140+
if hasattr(mocked, "reset_mock"):
138141
self._mocks.append(mocked)
139142
return mocked
140143

@@ -144,8 +147,7 @@ def object(self, *args, **kwargs):
144147

145148
def multiple(self, *args, **kwargs):
146149
"""API to mock.patch.multiple"""
147-
return self._start_patch(self.mock_module.patch.multiple, *args,
148-
**kwargs)
150+
return self._start_patch(self.mock_module.patch.multiple, *args, **kwargs)
149151

150152
def dict(self, *args, **kwargs):
151153
"""API to mock.patch.dict"""
@@ -173,8 +175,10 @@ def mock(mocker):
173175
Same as "mocker", but kept only for backward compatibility.
174176
"""
175177
import warnings
176-
warnings.warn('"mock" fixture has been deprecated, use "mocker" instead',
177-
DeprecationWarning)
178+
179+
warnings.warn(
180+
'"mock" fixture has been deprecated, use "mocker" instead', DeprecationWarning
181+
)
178182
return mocker
179183

180184

@@ -188,67 +192,60 @@ def assert_wrapper(__wrapped_mock_method__, *args, **kwargs):
188192
__wrapped_mock_method__(*args, **kwargs)
189193
return
190194
except AssertionError as e:
191-
if getattr(e, '_mock_introspection_applied', 0):
195+
if getattr(e, "_mock_introspection_applied", 0):
192196
msg = text_type(e)
193197
else:
194198
__mock_self = args[0]
195199
msg = text_type(e)
196200
if __mock_self.call_args is not None:
197201
actual_args, actual_kwargs = __mock_self.call_args
198-
msg += '\n\npytest introspection follows:\n'
202+
msg += "\n\npytest introspection follows:\n"
199203
try:
200204
assert actual_args == args[1:]
201205
except AssertionError as e:
202-
msg += '\nArgs:\n' + text_type(e)
206+
msg += "\nArgs:\n" + text_type(e)
203207
try:
204208
assert actual_kwargs == kwargs
205209
except AssertionError as e:
206-
msg += '\nKwargs:\n' + text_type(e)
210+
msg += "\nKwargs:\n" + text_type(e)
207211
e = AssertionError(msg)
208212
e._mock_introspection_applied = True
209213
raise e
210214

211215

212216
def wrap_assert_not_called(*args, **kwargs):
213217
__tracebackhide__ = True
214-
assert_wrapper(_mock_module_originals["assert_not_called"],
215-
*args, **kwargs)
218+
assert_wrapper(_mock_module_originals["assert_not_called"], *args, **kwargs)
216219

217220

218221
def wrap_assert_called_with(*args, **kwargs):
219222
__tracebackhide__ = True
220-
assert_wrapper(_mock_module_originals["assert_called_with"],
221-
*args, **kwargs)
223+
assert_wrapper(_mock_module_originals["assert_called_with"], *args, **kwargs)
222224

223225

224226
def wrap_assert_called_once(*args, **kwargs):
225227
__tracebackhide__ = True
226-
assert_wrapper(_mock_module_originals["assert_called_once"],
227-
*args, **kwargs)
228+
assert_wrapper(_mock_module_originals["assert_called_once"], *args, **kwargs)
228229

229230

230231
def wrap_assert_called_once_with(*args, **kwargs):
231232
__tracebackhide__ = True
232-
assert_wrapper(_mock_module_originals["assert_called_once_with"],
233-
*args, **kwargs)
233+
assert_wrapper(_mock_module_originals["assert_called_once_with"], *args, **kwargs)
234234

235235

236236
def wrap_assert_has_calls(*args, **kwargs):
237237
__tracebackhide__ = True
238-
assert_wrapper(_mock_module_originals["assert_has_calls"],
239-
*args, **kwargs)
238+
assert_wrapper(_mock_module_originals["assert_has_calls"], *args, **kwargs)
240239

241240

242241
def wrap_assert_any_call(*args, **kwargs):
243242
__tracebackhide__ = True
244-
assert_wrapper(_mock_module_originals["assert_any_call"],
245-
*args, **kwargs)
243+
assert_wrapper(_mock_module_originals["assert_any_call"], *args, **kwargs)
246244

247245

248246
def wrap_assert_called(*args, **kwargs):
249247
__tracebackhide__ = True
250-
assert_wrapper(_mock_module_originals["assert_called"],
251-
*args, **kwargs)
248+
assert_wrapper(_mock_module_originals["assert_called"], *args, **kwargs)
252249

253250

254251
def wrap_assert_methods(config):
@@ -263,26 +260,25 @@ def wrap_assert_methods(config):
263260
mock_module = _get_mock_module(config)
264261

265262
wrappers = {
266-
'assert_called': wrap_assert_called,
267-
'assert_called_once': wrap_assert_called_once,
268-
'assert_called_with': wrap_assert_called_with,
269-
'assert_called_once_with': wrap_assert_called_once_with,
270-
'assert_any_call': wrap_assert_any_call,
271-
'assert_has_calls': wrap_assert_has_calls,
272-
'assert_not_called': wrap_assert_not_called,
263+
"assert_called": wrap_assert_called,
264+
"assert_called_once": wrap_assert_called_once,
265+
"assert_called_with": wrap_assert_called_with,
266+
"assert_called_once_with": wrap_assert_called_once_with,
267+
"assert_any_call": wrap_assert_any_call,
268+
"assert_has_calls": wrap_assert_has_calls,
269+
"assert_not_called": wrap_assert_not_called,
273270
}
274271
for method, wrapper in wrappers.items():
275272
try:
276273
original = getattr(mock_module.NonCallableMock, method)
277274
except AttributeError: # pragma: no cover
278275
continue
279276
_mock_module_originals[method] = original
280-
patcher = mock_module.patch.object(
281-
mock_module.NonCallableMock, method, wrapper)
277+
patcher = mock_module.patch.object(mock_module.NonCallableMock, method, wrapper)
282278
patcher.start()
283279
_mock_module_patches.append(patcher)
284280

285-
if hasattr(config, 'add_cleanup'):
281+
if hasattr(config, "add_cleanup"):
286282
add_cleanup = config.add_cleanup
287283
else:
288284
# pytest 2.7 compatibility
@@ -298,26 +294,33 @@ def unwrap_assert_methods():
298294

299295

300296
def pytest_addoption(parser):
301-
parser.addini('mock_traceback_monkeypatch',
302-
'Monkeypatch the mock library to improve reporting of the '
303-
'assert_called_... methods',
304-
default=True)
305-
parser.addini('mock_use_standalone_module',
306-
'Use standalone "mock" (from PyPI) instead of builtin "unittest.mock" '
307-
'on Python 3',
308-
default=False)
297+
parser.addini(
298+
"mock_traceback_monkeypatch",
299+
"Monkeypatch the mock library to improve reporting of the "
300+
"assert_called_... methods",
301+
default=True,
302+
)
303+
parser.addini(
304+
"mock_use_standalone_module",
305+
'Use standalone "mock" (from PyPI) instead of builtin "unittest.mock" '
306+
"on Python 3",
307+
default=False,
308+
)
309309

310310

311311
def parse_ini_boolean(value):
312312
if value in (True, False):
313313
return value
314314
try:
315-
return {'true': True, 'false': False}[value.lower()]
315+
return {"true": True, "false": False}[value.lower()]
316316
except KeyError:
317-
raise ValueError('unknown string for bool: %r' % value)
317+
raise ValueError("unknown string for bool: %r" % value)
318318

319319

320320
def pytest_configure(config):
321-
tb = config.getoption('--tb')
322-
if parse_ini_boolean(config.getini('mock_traceback_monkeypatch')) and tb != 'native':
321+
tb = config.getoption("--tb")
322+
if (
323+
parse_ini_boolean(config.getini("mock_traceback_monkeypatch"))
324+
and tb != "native"
325+
):
323326
wrap_assert_methods(config)

0 commit comments

Comments
 (0)