Skip to content

Add missing typings to unittest.mock #7431

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
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: 0 additions & 1 deletion pyrightconfig.stricter.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"stdlib/sqlite3/dbapi2.pyi",
"stdlib/_tkinter.pyi",
"stdlib/tkinter",
"stdlib/unittest/mock.pyi",
"stdlib/xml/dom/NodeFilter.pyi",
"stdlib/xml/dom/expatbuilder.pyi",
"stdlib/xml/dom/minidom.pyi",
Expand Down
27 changes: 17 additions & 10 deletions stdlib/unittest/mock.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from _typeshed import Self
from contextlib import _GeneratorContextManager
from typing import Any, Awaitable, Callable, Generic, Iterable, Mapping, Sequence, TypeVar, overload
from typing_extensions import Literal

Expand Down Expand Up @@ -75,11 +76,15 @@ class _Sentinel:
sentinel: Any
DEFAULT: Any

_ArgsKwargs = tuple[tuple[Any, ...], Mapping[str, Any]]
_NameArgsKwargs = tuple[str, tuple[Any, ...], Mapping[str, Any]]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that kwargs is typed as Any in the rest of the stubs, but I wanted to be more strict here.

_CallValue = str | tuple[Any, ...] | Mapping[str, Any] | _ArgsKwargs | _NameArgsKwargs

class _Call(tuple[Any, ...]):
def __new__(
cls: type[Self],
value: Any = ...,
name: Any | None = ...,
value: _CallValue = ...,
name: str | None = ...,
parent: Any | None = ...,
two: bool = ...,
from_kall: bool = ...,
Expand All @@ -88,7 +93,7 @@ class _Call(tuple[Any, ...]):
parent: Any
from_kall: Any
def __init__(
self, value: Any = ..., name: Any | None = ..., parent: Any | None = ..., two: bool = ..., from_kall: bool = ...
self, value: _CallValue = ..., name: str | None = ..., parent: Any | None = ..., two: bool = ..., from_kall: bool = ...
) -> None: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, __other: object) -> bool: ...
Expand All @@ -97,9 +102,9 @@ class _Call(tuple[Any, ...]):
def __getattribute__(self, attr: str) -> Any: ...
if sys.version_info >= (3, 8):
@property
def args(self): ...
def args(self) -> tuple[Any, ...]: ...
@property
def kwargs(self): ...
def kwargs(self) -> Mapping[str, Any]: ...

def call_list(self) -> Any: ...

Expand Down Expand Up @@ -244,7 +249,9 @@ class _patch(Generic[_T]):
@overload
def __call__(self, func: Callable[..., _R]) -> Callable[..., _R]: ...
if sys.version_info >= (3, 8):
def decoration_helper(self, patched, args, keywargs): ...
def decoration_helper(
self, patched: _patch[Any], args: Sequence[Any], keywargs: Any
) -> _GeneratorContextManager[tuple[Sequence[Any], Any]]: ...

def decorate_class(self, klass: _TT) -> _TT: ...
def decorate_callable(self, func: Callable[..., _R]) -> Callable[..., _R]: ...
Expand Down Expand Up @@ -428,14 +435,14 @@ if sys.version_info >= (3, 8):
class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock): ... # type: ignore # argument disparities between base classes

class MagicProxy:
name: Any
name: str
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I should change this, however it doesn't really make much sense to me that name is typed as Any in the module's stubs, when it's clear that it's supposed to be a string (at least from what I could tell from the CPython source).

parent: Any
def __init__(self, name, parent) -> None: ...
def __init__(self, name: str, parent: Any) -> None: ...
if sys.version_info < (3, 8):
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...

def create_mock(self): ...
def __get__(self, obj, _type: Any | None = ...): ...
def create_mock(self) -> Any: ...
def __get__(self, obj: Any, _type: Any | None = ...) -> Any: ...

class _ANY:
def __eq__(self, other: object) -> Literal[True]: ...
Expand Down