unittest.mock: use ParamSpec in patch (#10325)

Fixes #10324
This commit is contained in:
Shantanu
2023-06-20 04:48:49 -07:00
committed by GitHub
parent 7114aecf77
commit 9e86c6026a
3 changed files with 34 additions and 4 deletions

View File

@@ -1,9 +1,12 @@
from __future__ import annotations
import unittest
from collections.abc import Callable
from datetime import datetime, timedelta
from decimal import Decimal
from fractions import Fraction
from typing_extensions import assert_type
from unittest.mock import Mock, patch
case = unittest.TestCase()
@@ -86,3 +89,29 @@ case.assertGreater(datetime(1999, 1, 2), 1) # type: ignore
case.assertGreater(Spam(), Eggs()) # type: ignore
case.assertGreater(Ham(), Bacon()) # type: ignore
case.assertGreater(Bacon(), Ham()) # type: ignore
###
# Tests for mock.patch
###
@patch("sys.exit", new=Mock())
def f(i: int) -> str:
return "asdf"
assert_type(f(1), str)
f("a") # type: ignore
@patch("sys.exit", new=Mock())
class TestXYZ(unittest.TestCase):
attr: int = 5
@staticmethod
def method() -> int:
return 123
assert_type(TestXYZ.attr, int)
assert_type(TestXYZ.method, Callable[[], int])