From 0d100b9110f1b30529ba4d1be26d1eb09ae5d42c Mon Sep 17 00:00:00 2001 From: Semyon Moroz Date: Sun, 14 Sep 2025 22:25:41 +0400 Subject: [PATCH] Make type of `unitest.mock.Any` a subclass of `Any` (#14708) --- stdlib/@tests/test_cases/check_unittest.py | 29 +++++++++++++++++++++- stdlib/unittest/mock.pyi | 3 ++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/stdlib/@tests/test_cases/check_unittest.py b/stdlib/@tests/test_cases/check_unittest.py index d717e99a3..bbc2a2ba4 100644 --- a/stdlib/@tests/test_cases/check_unittest.py +++ b/stdlib/@tests/test_cases/check_unittest.py @@ -7,7 +7,7 @@ from decimal import Decimal from fractions import Fraction from typing import TypedDict, Union from typing_extensions import assert_type -from unittest.mock import AsyncMock, MagicMock, Mock, patch +from unittest.mock import _ANY, ANY, AsyncMock, MagicMock, Mock, patch case = unittest.TestCase() @@ -226,3 +226,30 @@ with patch.object(Decimal, "exp", new=42) as obj_explicit_new_enter: with patch.object(Decimal, "exp", new_callable=lambda: 42) as obj_explicit_new_callable_enter: assert_type(obj_explicit_new_callable_enter, int) + + +### +# Tests for mock.ANY +### + + +assert_type(ANY, _ANY) # Make sure ANY has runtime type + + +# Regression tests. See https://github.com/python/typeshed/issues/14701 +class TD(TypedDict): + x: str + y: float + + +td: TD = {"x": "1", "y": ANY} + + +def test_as_param(x: str) -> None: ... + + +test_as_param(ANY) + + +def test_as_return_value(x: int) -> TD: + return {"x": str(x), "y": ANY} diff --git a/stdlib/unittest/mock.pyi b/stdlib/unittest/mock.pyi index f4b59e7ca..f3e58bcd1 100644 --- a/stdlib/unittest/mock.pyi +++ b/stdlib/unittest/mock.pyi @@ -508,7 +508,8 @@ class MagicProxy(Base): def create_mock(self) -> Any: ... def __get__(self, obj: Any, _type: Any | None = None) -> Any: ... -class _ANY: +# See https://github.com/python/typeshed/issues/14701 +class _ANY(Any): def __eq__(self, other: object) -> Literal[True]: ... def __ne__(self, other: object) -> Literal[False]: ... __hash__: ClassVar[None] # type: ignore[assignment]