Skip to content

Commit e6aad18

Browse files
authored
Merge pull request #406 from nicoddemus/use-ruff
Use ruff in place of black and reorder-python-imports
2 parents 3d48ff9 + c23528a commit e6aad18

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
exclude: '^($|.*\.bin)'
22
repos:
3-
- repo: https://github.com/psf/black
4-
rev: 23.11.0
5-
hooks:
6-
- id: black
7-
args: [--safe, --quiet]
83
- repo: https://github.com/pre-commit/pre-commit-hooks
94
rev: v4.5.0
105
hooks:
@@ -18,13 +13,14 @@ repos:
1813
files: ^(CHANGELOG.rst|README.rst|HOWTORELEASE.rst|changelog/.*)$
1914
language: python
2015
additional_dependencies: [pygments, restructuredtext_lint]
21-
- repo: https://github.com/asottile/reorder-python-imports
22-
rev: v3.12.0
16+
- repo: https://github.com/astral-sh/ruff-pre-commit
17+
rev: v0.1.14
2318
hooks:
24-
- id: reorder-python-imports
25-
args: ['--application-directories=.:src']
19+
- id: ruff
20+
args: [ --fix ]
21+
- id: ruff-format
2622
- repo: https://github.com/pre-commit/mirrors-mypy
27-
rev: v1.7.1
23+
rev: v1.8.0
2824
hooks:
2925
- id: mypy
3026
files: ^(src|tests)

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[build-system]
2+
requires = [
3+
"setuptools",
4+
"setuptools-scm[toml]",
5+
]
6+
build-backend = "setuptools.build_meta"
7+
8+
[tool.ruff]

src/pytest_mock/plugin.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ class MockerFixture:
5252
def __init__(self, config: Any) -> None:
5353
self._patches_and_mocks: List[Tuple[Any, unittest.mock.MagicMock]] = []
5454
self.mock_module = mock_module = get_mock_module(config)
55-
self.patch = self._Patcher(
56-
self._patches_and_mocks, mock_module
57-
) # type: MockerFixture._Patcher
55+
self.patch = self._Patcher(self._patches_and_mocks, mock_module) # type: MockerFixture._Patcher
5856
# aliases for convenience
5957
self.Mock = mock_module.Mock
6058
self.MagicMock = mock_module.MagicMock
@@ -250,7 +248,7 @@ def object(
250248
spec_set: Optional[object] = None,
251249
autospec: Optional[object] = None,
252250
new_callable: object = None,
253-
**kwargs: Any
251+
**kwargs: Any,
254252
) -> MockType:
255253
"""API to mock.patch.object"""
256254
if new is self.DEFAULT:
@@ -266,7 +264,7 @@ def object(
266264
spec_set=spec_set,
267265
autospec=autospec,
268266
new_callable=new_callable,
269-
**kwargs
267+
**kwargs,
270268
)
271269

272270
def context_manager(
@@ -279,7 +277,7 @@ def context_manager(
279277
spec_set: Optional[builtins.object] = None,
280278
autospec: Optional[builtins.object] = None,
281279
new_callable: builtins.object = None,
282-
**kwargs: Any
280+
**kwargs: Any,
283281
) -> MockType:
284282
"""This is equivalent to mock.patch.object except that the returned mock
285283
does not issue a warning when used as a context manager."""
@@ -296,7 +294,7 @@ def context_manager(
296294
spec_set=spec_set,
297295
autospec=autospec,
298296
new_callable=new_callable,
299-
**kwargs
297+
**kwargs,
300298
)
301299

302300
def multiple(
@@ -307,7 +305,7 @@ def multiple(
307305
spec_set: Optional[builtins.object] = None,
308306
autospec: Optional[builtins.object] = None,
309307
new_callable: Optional[builtins.object] = None,
310-
**kwargs: Any
308+
**kwargs: Any,
311309
) -> Dict[str, MockType]:
312310
"""API to mock.patch.multiple"""
313311
return self._start_patch(
@@ -319,15 +317,15 @@ def multiple(
319317
spec_set=spec_set,
320318
autospec=autospec,
321319
new_callable=new_callable,
322-
**kwargs
320+
**kwargs,
323321
)
324322

325323
def dict(
326324
self,
327325
in_dict: Union[Mapping[Any, Any], str],
328326
values: Union[Mapping[Any, Any], Iterable[Tuple[Any, Any]]] = (),
329327
clear: bool = False,
330-
**kwargs: Any
328+
**kwargs: Any,
331329
) -> Any:
332330
"""API to mock.patch.dict"""
333331
return self._start_patch(
@@ -336,7 +334,7 @@ def dict(
336334
in_dict,
337335
values=values,
338336
clear=clear,
339-
**kwargs
337+
**kwargs,
340338
)
341339

342340
@overload
@@ -349,7 +347,7 @@ def __call__(
349347
spec_set: Optional[builtins.object] = ...,
350348
autospec: Optional[builtins.object] = ...,
351349
new_callable: None = ...,
352-
**kwargs: Any
350+
**kwargs: Any,
353351
) -> MockType:
354352
...
355353

@@ -363,7 +361,7 @@ def __call__(
363361
spec_set: Optional[builtins.object] = ...,
364362
autospec: Optional[builtins.object] = ...,
365363
new_callable: None = ...,
366-
**kwargs: Any
364+
**kwargs: Any,
367365
) -> _T:
368366
...
369367

@@ -377,7 +375,7 @@ def __call__(
377375
spec_set: Optional[builtins.object],
378376
autospec: Optional[builtins.object],
379377
new_callable: Callable[[], _T],
380-
**kwargs: Any
378+
**kwargs: Any,
381379
) -> _T:
382380
...
383381

@@ -392,7 +390,7 @@ def __call__(
392390
autospec: Optional[builtins.object] = ...,
393391
*,
394392
new_callable: Callable[[], _T],
395-
**kwargs: Any
393+
**kwargs: Any,
396394
) -> _T:
397395
...
398396

@@ -405,7 +403,7 @@ def __call__(
405403
spec_set: Optional[builtins.object] = None,
406404
autospec: Optional[builtins.object] = None,
407405
new_callable: Optional[Callable[[], Any]] = None,
408-
**kwargs: Any
406+
**kwargs: Any,
409407
) -> Any:
410408
"""API to mock.patch"""
411409
if new is self.DEFAULT:
@@ -420,7 +418,7 @@ def __call__(
420418
spec_set=spec_set,
421419
autospec=autospec,
422420
new_callable=new_callable,
423-
**kwargs
421+
**kwargs,
424422
)
425423

426424

tests/test_pytest_mock.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,7 @@ def __test_failure_message(self, mocker: MockerFixture, **kwargs: Any) -> None:
250250
msg = "Expected call: {0}()\nNot called"
251251
expected_message = msg.format(expected_name)
252252
stub = mocker.stub(**kwargs)
253-
with pytest.raises(
254-
AssertionError, match=re.escape(expected_message)
255-
) as exc_info:
253+
with pytest.raises(AssertionError, match=re.escape(expected_message)):
256254
stub.assert_called_with()
257255

258256
def test_failure_message_with_no_name(self, mocker: MagicMock) -> None:
@@ -561,7 +559,12 @@ def assert_argument_introspection(left: Any, right: Any) -> Generator[None, None
561559
# NOTE: we assert with either verbose or not, depending on how our own
562560
# test was run by examining sys.argv
563561
verbose = any(a.startswith("-v") for a in sys.argv)
564-
expected = "\n ".join(util._compare_eq_iterable(left, right, verbose))
562+
if int(pytest.version_tuple[0]) < 8:
563+
expected = "\n ".join(util._compare_eq_iterable(left, right, verbose)) # type:ignore[arg-type]
564+
else:
565+
expected = "\n ".join(
566+
util._compare_eq_iterable(left, right, lambda t, *_: t, verbose)
567+
)
565568
assert expected in str(e)
566569
else:
567570
raise AssertionError("DID NOT RAISE")
@@ -1042,7 +1045,7 @@ def my_func():
10421045
with dummy_module.MyContext() as v:
10431046
return v
10441047

1045-
m = mocker.patch.object(dummy_module, "MyContext")
1048+
mocker.patch.object(dummy_module, "MyContext")
10461049
assert isinstance(my_func(), mocker.MagicMock)
10471050

10481051

0 commit comments

Comments
 (0)