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

Full type annotations #199

Merged
merged 12 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
[mypy]
disallow_any_generics = True
disallow_incomplete_defs = True
disallow_subclassing_any = True
no_implicit_optional = True
pretty = True
show_error_codes = True
strict_equality = True
warn_redundant_casts = True
warn_return_any = True
warn_unreachable = True
warn_unused_configs = True
warn_unused_ignores = True
23 changes: 11 additions & 12 deletions src/pytest_mock/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any
from typing import Dict
from typing import List

import asyncio
import functools
Expand Down Expand Up @@ -36,9 +37,9 @@ class MockFixture:
ensuring that they are uninstalled at the end of each test.
"""

def __init__(self, config):
self._patches = [] # list of mock._patch objects
self._mocks = [] # list of MagicMock objects
def __init__(self, config: Any) -> None:
self._patches = [] # type: List[Any]
self._mocks = [] # type: List[Any]
self.mock_module = mock_module = _get_mock_module(config)
self.patch = self._Patcher(self._patches, self._mocks, mock_module)
# aliases for convenience
Expand All @@ -55,14 +56,12 @@ def __init__(self, config):
self.sentinel = mock_module.sentinel
self.mock_open = mock_module.mock_open

def resetall(self):
"""
Call reset_mock() on all patchers started by this fixture.
"""
def resetall(self) -> None:
"""Call reset_mock() on all patchers started by this fixture."""
for m in self._mocks:
m.reset_mock()

def stopall(self):
def stopall(self) -> None:
"""
Stop all patchers started by this fixture. Can be safely called multiple
times.
Expand All @@ -72,9 +71,9 @@ def stopall(self):
self._patches[:] = []
self._mocks[:] = []

def spy(self, obj, name):
def spy(self, obj: Any, name: str) -> Any:
staticdev marked this conversation as resolved.
Show resolved Hide resolved
"""
Creates a spy of method. It will run method normally, but it is now
Create a spy of method. It will run method normally, but it is now
possible to use `mock` call features with it, like call count.

:param object obj: An object.
Expand Down Expand Up @@ -134,7 +133,7 @@ async def async_wrapper(*args, **kwargs):

def stub(self, name=None):
staticdev marked this conversation as resolved.
Show resolved Hide resolved
"""
Creates a stub method. It accepts any arguments. Ideal to register to
Create a stub method. It accepts any arguments. Ideal to register to
callbacks in tests.

:param name: the constructed stub's name as used in repr
Expand Down Expand Up @@ -192,7 +191,7 @@ def __call__(self, *args, **kwargs):

def _mocker(pytestconfig):
"""
return an object that has the same interface to the `mock` module, but
Return an object that has the same interface to the `mock` module, but
takes care of automatically undoing all patches after each test method.
"""
result = MockFixture(pytestconfig)
Expand Down