Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff in place of black and reorder-python-imports #406

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
exclude: '^($|.*\.bin)'
repos:
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
args: [--safe, --quiet]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand All @@ -18,13 +13,14 @@ repos:
files: ^(CHANGELOG.rst|README.rst|HOWTORELEASE.rst|changelog/.*)$
language: python
additional_dependencies: [pygments, restructuredtext_lint]
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.14
hooks:
- id: reorder-python-imports
args: ['--application-directories=.:src']
- id: ruff
args: [ --fix ]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
rev: v1.8.0
hooks:
- id: mypy
files: ^(src|tests)
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build-system]
requires = [
"setuptools",
"setuptools-scm[toml]",
]
build-backend = "setuptools.build_meta"

[tool.ruff]
32 changes: 15 additions & 17 deletions src/pytest_mock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ class MockerFixture:
def __init__(self, config: Any) -> None:
self._patches_and_mocks: List[Tuple[Any, unittest.mock.MagicMock]] = []
self.mock_module = mock_module = get_mock_module(config)
self.patch = self._Patcher(
self._patches_and_mocks, mock_module
) # type: MockerFixture._Patcher
self.patch = self._Patcher(self._patches_and_mocks, mock_module) # type: MockerFixture._Patcher
# aliases for convenience
self.Mock = mock_module.Mock
self.MagicMock = mock_module.MagicMock
Expand Down Expand Up @@ -250,7 +248,7 @@ def object(
spec_set: Optional[object] = None,
autospec: Optional[object] = None,
new_callable: object = None,
**kwargs: Any
**kwargs: Any,
) -> MockType:
"""API to mock.patch.object"""
if new is self.DEFAULT:
Expand All @@ -266,7 +264,7 @@ def object(
spec_set=spec_set,
autospec=autospec,
new_callable=new_callable,
**kwargs
**kwargs,
)

def context_manager(
Expand All @@ -279,7 +277,7 @@ def context_manager(
spec_set: Optional[builtins.object] = None,
autospec: Optional[builtins.object] = None,
new_callable: builtins.object = None,
**kwargs: Any
**kwargs: Any,
) -> MockType:
"""This is equivalent to mock.patch.object except that the returned mock
does not issue a warning when used as a context manager."""
Expand All @@ -296,7 +294,7 @@ def context_manager(
spec_set=spec_set,
autospec=autospec,
new_callable=new_callable,
**kwargs
**kwargs,
)

def multiple(
Expand All @@ -307,7 +305,7 @@ def multiple(
spec_set: Optional[builtins.object] = None,
autospec: Optional[builtins.object] = None,
new_callable: Optional[builtins.object] = None,
**kwargs: Any
**kwargs: Any,
) -> Dict[str, MockType]:
"""API to mock.patch.multiple"""
return self._start_patch(
Expand All @@ -319,15 +317,15 @@ def multiple(
spec_set=spec_set,
autospec=autospec,
new_callable=new_callable,
**kwargs
**kwargs,
)

def dict(
self,
in_dict: Union[Mapping[Any, Any], str],
values: Union[Mapping[Any, Any], Iterable[Tuple[Any, Any]]] = (),
clear: bool = False,
**kwargs: Any
**kwargs: Any,
) -> Any:
"""API to mock.patch.dict"""
return self._start_patch(
Expand All @@ -336,7 +334,7 @@ def dict(
in_dict,
values=values,
clear=clear,
**kwargs
**kwargs,
)

@overload
Expand All @@ -349,7 +347,7 @@ def __call__(
spec_set: Optional[builtins.object] = ...,
autospec: Optional[builtins.object] = ...,
new_callable: None = ...,
**kwargs: Any
**kwargs: Any,
) -> MockType:
...

Expand All @@ -363,7 +361,7 @@ def __call__(
spec_set: Optional[builtins.object] = ...,
autospec: Optional[builtins.object] = ...,
new_callable: None = ...,
**kwargs: Any
**kwargs: Any,
) -> _T:
...

Expand All @@ -377,7 +375,7 @@ def __call__(
spec_set: Optional[builtins.object],
autospec: Optional[builtins.object],
new_callable: Callable[[], _T],
**kwargs: Any
**kwargs: Any,
) -> _T:
...

Expand All @@ -392,7 +390,7 @@ def __call__(
autospec: Optional[builtins.object] = ...,
*,
new_callable: Callable[[], _T],
**kwargs: Any
**kwargs: Any,
) -> _T:
...

Expand All @@ -405,7 +403,7 @@ def __call__(
spec_set: Optional[builtins.object] = None,
autospec: Optional[builtins.object] = None,
new_callable: Optional[Callable[[], Any]] = None,
**kwargs: Any
**kwargs: Any,
) -> Any:
"""API to mock.patch"""
if new is self.DEFAULT:
Expand All @@ -420,7 +418,7 @@ def __call__(
spec_set=spec_set,
autospec=autospec,
new_callable=new_callable,
**kwargs
**kwargs,
)


Expand Down
13 changes: 8 additions & 5 deletions tests/test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ def __test_failure_message(self, mocker: MockerFixture, **kwargs: Any) -> None:
msg = "Expected call: {0}()\nNot called"
expected_message = msg.format(expected_name)
stub = mocker.stub(**kwargs)
with pytest.raises(
AssertionError, match=re.escape(expected_message)
) as exc_info:
with pytest.raises(AssertionError, match=re.escape(expected_message)):
stub.assert_called_with()

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

m = mocker.patch.object(dummy_module, "MyContext")
mocker.patch.object(dummy_module, "MyContext")
assert isinstance(my_func(), mocker.MagicMock)


Expand Down
Loading