Skip to content

Commit 0d100b9

Browse files
authored
Make type of unitest.mock.Any a subclass of Any (#14708)
1 parent b2777f4 commit 0d100b9

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

stdlib/@tests/test_cases/check_unittest.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from fractions import Fraction
88
from typing import TypedDict, Union
99
from typing_extensions import assert_type
10-
from unittest.mock import AsyncMock, MagicMock, Mock, patch
10+
from unittest.mock import _ANY, ANY, AsyncMock, MagicMock, Mock, patch
1111

1212
case = unittest.TestCase()
1313

@@ -226,3 +226,30 @@ def obj_f_explicit_new_callable(i: int, new_callable_ret: int) -> str:
226226

227227
with patch.object(Decimal, "exp", new_callable=lambda: 42) as obj_explicit_new_callable_enter:
228228
assert_type(obj_explicit_new_callable_enter, int)
229+
230+
231+
###
232+
# Tests for mock.ANY
233+
###
234+
235+
236+
assert_type(ANY, _ANY) # Make sure ANY has runtime type
237+
238+
239+
# Regression tests. See https://github.com/python/typeshed/issues/14701
240+
class TD(TypedDict):
241+
x: str
242+
y: float
243+
244+
245+
td: TD = {"x": "1", "y": ANY}
246+
247+
248+
def test_as_param(x: str) -> None: ...
249+
250+
251+
test_as_param(ANY)
252+
253+
254+
def test_as_return_value(x: int) -> TD:
255+
return {"x": str(x), "y": ANY}

stdlib/unittest/mock.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ class MagicProxy(Base):
508508
def create_mock(self) -> Any: ...
509509
def __get__(self, obj: Any, _type: Any | None = None) -> Any: ...
510510

511-
class _ANY:
511+
# See https://github.com/python/typeshed/issues/14701
512+
class _ANY(Any):
512513
def __eq__(self, other: object) -> Literal[True]: ...
513514
def __ne__(self, other: object) -> Literal[False]: ...
514515
__hash__: ClassVar[None] # type: ignore[assignment]

0 commit comments

Comments
 (0)