From ef1d7853ee8aeb1146ce3874456c6e926409c815 Mon Sep 17 00:00:00 2001 From: Rodrigo Castro Date: Fri, 20 Mar 2020 10:37:18 -0300 Subject: [PATCH] stdlib.3.unitest.mock.NonCallableMock signatures (#3846) --- stdlib/3/unittest/mock.pyi | 88 +++++++++++++++++++++++++++++--------- third_party/2and3/mock.pyi | 88 +++++++++++++++++++++++++++++--------- 2 files changed, 134 insertions(+), 42 deletions(-) diff --git a/stdlib/3/unittest/mock.pyi b/stdlib/3/unittest/mock.pyi index 396d806b1..d8ed01ea0 100644 --- a/stdlib/3/unittest/mock.pyi +++ b/stdlib/3/unittest/mock.pyi @@ -20,7 +20,24 @@ class _Sentinel: sentinel: Any DEFAULT: Any -class _CallList(List[_T]): +class _Call(Tuple[Any, ...]): + def __new__(cls, value: Any = ..., name: Optional[Any] = ..., parent: Optional[Any] = ..., two: bool = ..., from_kall: bool = ...) -> Any: ... + name: Any + parent: Any + from_kall: Any + def __init__(self, value: Any = ..., name: Optional[Any] = ..., parent: Optional[Any] = ..., two: bool = ..., from_kall: bool = ...) -> None: ... + def __eq__(self, other: Any) -> bool: ... + __ne__: Any + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... + 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: ... + +call: Any + + +class _CallList(List[_Call]): def __contains__(self, value: Any) -> bool: ... class _MockIter: @@ -32,10 +49,55 @@ class _MockIter: class Base: def __init__(self, *args: Any, **kwargs: Any) -> None: ... -# TODO: Defining this and other mock classes as classes in this stub causes -# many false positives with mypy and production code. See if we can -# improve mypy somehow and use a class with an "Any" base class. -NonCallableMock = Any + +class NonCallableMock(Base, Any): # type: ignore + def __getattr__(self, name: str) -> Any: ... + if sys.version_info >= (3, 8): + def _calls_repr(self, prefix: str = ...) -> str: ... + + def assert_called_with(self, *args: Any, **kwargs: Any) -> None: ... + def assert_not_called(self) -> None: ... + def assert_called_once_with(self, *args: Any, **kwargs: Any) -> None: ... + + def _format_mock_failure_message(self, args: Any, kwargs: Any, action: str = ...) -> str: ... + elif sys.version_info >= (3, 5): + def assert_called_with(_mock_self, *args: Any, **kwargs: Any) -> None: ... + def assert_not_called(_mock_self) -> None: ... + def assert_called_once_with(_mock_self, *args: Any, **kwargs: Any) -> None: ... + + def _format_mock_failure_message(self, args: Any, kwargs: Any) -> str: ... + + if sys.version_info >= (3, 8): + def assert_called_once(self) -> None: ... + elif sys.version_info >= (3, 6): + def assert_called_once(_mock_self) -> None: ... + + if sys.version_info >= (3, 6): + def reset_mock(self, visited: Any = ..., *, return_value: bool = ..., side_effect: bool = ...) -> None: ... + elif sys.version_info >= (3, 5): + def reset_mock(self, visited: Any = ...) -> None: ... + + if sys.version_info >= (3, 7): + def _extract_mock_name(self) -> str: ... + def _get_call_signature_from_name(self, name: str) -> Any: ... + + def assert_any_call(self, *args: Any, **kwargs: Any) -> None: ... + def assert_has_calls(self, calls: _CallList, any_order: bool = ...) -> None: ... + def mock_add_spec(self, spec: Any, spec_set: bool = ...) -> None: ... + def _mock_add_spec(self, spec: Any, spec_set, _spec_as_instance: bool = ..., _eat_self: bool = ...) -> None: ... + def attach_mock(self, mock: NonCallableMock, attribute: str) -> None: ... + def configure_mock(self, **kwargs: Any) -> None: ... + return_value: Any + side_effect: Any + called: bool + call_count: int + call_args: 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 _get_child_mock(self, **kw: Any) -> NonCallableMock: ... + class CallableMixin(Base): side_effect: Any @@ -115,22 +177,6 @@ class _ANY: ANY: Any -class _Call(Tuple[Any, ...]): - def __new__(cls, value: Any = ..., name: Optional[Any] = ..., parent: Optional[Any] = ..., two: bool = ..., from_kall: bool = ...) -> Any: ... - name: Any - parent: Any - from_kall: Any - def __init__(self, value: Any = ..., name: Optional[Any] = ..., parent: Optional[Any] = ..., two: bool = ..., from_kall: bool = ...) -> None: ... - def __eq__(self, other: Any) -> bool: ... - __ne__: Any - def __call__(self, *args: Any, **kwargs: Any) -> Any: ... - 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: ... - -call: Any - def create_autospec(spec: Any, spec_set: Any = ..., instance: Any = ..., _parent: Optional[Any] = ..., _name: Optional[Any] = ..., **kwargs: Any) -> Any: ... class _SpecState: diff --git a/third_party/2and3/mock.pyi b/third_party/2and3/mock.pyi index 396d806b1..d8ed01ea0 100644 --- a/third_party/2and3/mock.pyi +++ b/third_party/2and3/mock.pyi @@ -20,7 +20,24 @@ class _Sentinel: sentinel: Any DEFAULT: Any -class _CallList(List[_T]): +class _Call(Tuple[Any, ...]): + def __new__(cls, value: Any = ..., name: Optional[Any] = ..., parent: Optional[Any] = ..., two: bool = ..., from_kall: bool = ...) -> Any: ... + name: Any + parent: Any + from_kall: Any + def __init__(self, value: Any = ..., name: Optional[Any] = ..., parent: Optional[Any] = ..., two: bool = ..., from_kall: bool = ...) -> None: ... + def __eq__(self, other: Any) -> bool: ... + __ne__: Any + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... + 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: ... + +call: Any + + +class _CallList(List[_Call]): def __contains__(self, value: Any) -> bool: ... class _MockIter: @@ -32,10 +49,55 @@ class _MockIter: class Base: def __init__(self, *args: Any, **kwargs: Any) -> None: ... -# TODO: Defining this and other mock classes as classes in this stub causes -# many false positives with mypy and production code. See if we can -# improve mypy somehow and use a class with an "Any" base class. -NonCallableMock = Any + +class NonCallableMock(Base, Any): # type: ignore + def __getattr__(self, name: str) -> Any: ... + if sys.version_info >= (3, 8): + def _calls_repr(self, prefix: str = ...) -> str: ... + + def assert_called_with(self, *args: Any, **kwargs: Any) -> None: ... + def assert_not_called(self) -> None: ... + def assert_called_once_with(self, *args: Any, **kwargs: Any) -> None: ... + + def _format_mock_failure_message(self, args: Any, kwargs: Any, action: str = ...) -> str: ... + elif sys.version_info >= (3, 5): + def assert_called_with(_mock_self, *args: Any, **kwargs: Any) -> None: ... + def assert_not_called(_mock_self) -> None: ... + def assert_called_once_with(_mock_self, *args: Any, **kwargs: Any) -> None: ... + + def _format_mock_failure_message(self, args: Any, kwargs: Any) -> str: ... + + if sys.version_info >= (3, 8): + def assert_called_once(self) -> None: ... + elif sys.version_info >= (3, 6): + def assert_called_once(_mock_self) -> None: ... + + if sys.version_info >= (3, 6): + def reset_mock(self, visited: Any = ..., *, return_value: bool = ..., side_effect: bool = ...) -> None: ... + elif sys.version_info >= (3, 5): + def reset_mock(self, visited: Any = ...) -> None: ... + + if sys.version_info >= (3, 7): + def _extract_mock_name(self) -> str: ... + def _get_call_signature_from_name(self, name: str) -> Any: ... + + def assert_any_call(self, *args: Any, **kwargs: Any) -> None: ... + def assert_has_calls(self, calls: _CallList, any_order: bool = ...) -> None: ... + def mock_add_spec(self, spec: Any, spec_set: bool = ...) -> None: ... + def _mock_add_spec(self, spec: Any, spec_set, _spec_as_instance: bool = ..., _eat_self: bool = ...) -> None: ... + def attach_mock(self, mock: NonCallableMock, attribute: str) -> None: ... + def configure_mock(self, **kwargs: Any) -> None: ... + return_value: Any + side_effect: Any + called: bool + call_count: int + call_args: 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 _get_child_mock(self, **kw: Any) -> NonCallableMock: ... + class CallableMixin(Base): side_effect: Any @@ -115,22 +177,6 @@ class _ANY: ANY: Any -class _Call(Tuple[Any, ...]): - def __new__(cls, value: Any = ..., name: Optional[Any] = ..., parent: Optional[Any] = ..., two: bool = ..., from_kall: bool = ...) -> Any: ... - name: Any - parent: Any - from_kall: Any - def __init__(self, value: Any = ..., name: Optional[Any] = ..., parent: Optional[Any] = ..., two: bool = ..., from_kall: bool = ...) -> None: ... - def __eq__(self, other: Any) -> bool: ... - __ne__: Any - def __call__(self, *args: Any, **kwargs: Any) -> Any: ... - 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: ... - -call: Any - def create_autospec(spec: Any, spec_set: Any = ..., instance: Any = ..., _parent: Optional[Any] = ..., _name: Optional[Any] = ..., **kwargs: Any) -> Any: ... class _SpecState: