Skip to content

Fix stubtest for third-party mock package #6685

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

Merged
merged 1 commit into from
Dec 25, 2021
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
1 change: 1 addition & 0 deletions pyrightconfig.stricter.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"stubs/jsonschema",
"stubs/ldap3",
"stubs/Markdown",
"stubs/mock",
"stubs/mysqlclient",
"stubs/oauthlib",
"stubs/Pillow",
Expand Down
9 changes: 9 additions & 0 deletions stubs/mock/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@ mock.NonCallableMock.__new__
mock.patch
mock.mock.NonCallableMock.__new__
mock.mock.patch

# Methods with a first argument that's not called "self"
mock.mock.AsyncMockMixin.assert_any_await
mock.mock.AsyncMockMixin.assert_awaited
mock.mock.AsyncMockMixin.assert_awaited_once
mock.mock.AsyncMockMixin.assert_awaited_once_with
mock.mock.AsyncMockMixin.assert_awaited_with
mock.mock.AsyncMockMixin.assert_has_awaits
mock.mock.AsyncMockMixin.assert_not_awaited
63 changes: 15 additions & 48 deletions stubs/mock/mock/mock.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections.abc import Callable, Mapping, Sequence
from typing import Any, Generic, List, Tuple, Type, TypeVar, overload
from typing import Any, Generic, Type, TypeVar, overload

_F = TypeVar("_F", bound=Callable[..., Any])
_T = TypeVar("_T")
_TT = TypeVar("_TT", bound=Type[Any])
_R = TypeVar("_R")

__all__ = [
__all__ = (
"Mock",
"MagicMock",
"patch",
Expand All @@ -22,25 +22,15 @@ __all__ = [
"mock_open",
"PropertyMock",
"seal",
]
)
__version__: str

FILTER_DIR: Any

class _slotted: ...

class _SentinelObject:
name: Any
def __init__(self, name: Any) -> None: ...

class _Sentinel:
def __init__(self) -> None: ...
def __getattr__(self, name: str) -> Any: ...

sentinel: Any
DEFAULT: Any

class _Call(Tuple[Any, ...]):
class _Call(tuple[Any, ...]):
def __new__(
cls, value: Any = ..., name: Any | None = ..., parent: Any | None = ..., two: bool = ..., from_kall: bool = ...
) -> Any: ...
Expand All @@ -53,22 +43,18 @@ class _Call(Tuple[Any, ...]):
def __eq__(self, other: Any) -> bool: ...
__ne__: Any
def __call__(self, *args: Any, **kwargs: Any) -> _Call: ...
def __getattr__(self, attr: Any) -> Any: ...
def count(self, *args: Any, **kwargs: Any) -> Any: ...
def index(self, *args: Any, **kwargs: Any) -> Any: ...
def call_list(self) -> Any: ...
def __getattr__(self, attr: str) -> Any: ...
@property
def args(self): ...
@property
def kwargs(self): ...
def call_list(self) -> _CallList: ...

call: _Call

class _CallList(List[_Call]):
class _CallList(list[_Call]):
def __contains__(self, value: Any) -> bool: ...

class _MockIter:
obj: Any
def __init__(self, obj: Any) -> None: ...
def __iter__(self) -> Any: ...
def __next__(self) -> Any: ...

class Base:
def __init__(self, *args: Any, **kwargs: Any) -> None: ...

Expand Down Expand Up @@ -113,7 +99,7 @@ class NonCallableMock(Base, Any):
call_args_list: _CallList
mock_calls: _CallList
def _format_mock_call_signature(self, args: Any, kwargs: Any) -> str: ...
def _call_matcher(self, _call: Tuple[_Call, ...]) -> _Call: ...
def _call_matcher(self, _call: tuple[_Call, ...]) -> _Call: ...
def _get_child_mock(self, **kw: Any) -> NonCallableMock: ...

class CallableMixin(Base):
Expand Down Expand Up @@ -149,23 +135,6 @@ class _patch(Generic[_T]):
autospec: Any
kwargs: Mapping[str, Any]
additional_patchers: Any
@overload
def __init__(
self: _patch[MagicMock | AsyncMock],
getter: Callable[[], Any],
attribute: str,
*,
spec: Any | None,
create: bool,
spec_set: Any | None,
autospec: Any | None,
new_callable: Any | None,
kwargs: Mapping[str, Any],
) -> None: ...
# This overload also covers the case, where new==DEFAULT. In this case, self is _patch[Any].
# Ideally we'd be able to add an overload for it so that self is _patch[MagicMock],
# but that's impossible with the current type system.
@overload
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the first overload removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new is a mandatory argument.

def __init__(
self: _patch[_T],
getter: Callable[[], Any],
Expand Down Expand Up @@ -284,7 +253,6 @@ class MagicMock(MagicMixin, Mock):

class AsyncMockMixin(Base):
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
async def _execute_mock_call(self, *args: Any, **kwargs: Any) -> Any: ...
def assert_awaited(self) -> None: ...
def assert_awaited_once(self) -> None: ...
def assert_awaited_with(self, *args: Any, **kwargs: Any) -> None: ...
Expand All @@ -302,11 +270,10 @@ class AsyncMagicMixin(MagicMixin):

class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock): ...

class MagicProxy:
name: Any
class MagicProxy(Base):
name: str
parent: Any
def __init__(self, name: Any, parent: Any) -> None: ...
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
def __init__(self, name: str, parent) -> None: ...
def create_mock(self) -> Any: ...
def __get__(self, obj: Any, _type: Any | None = ...) -> Any: ...

Expand Down