From 15221a5c8957bdaa55bcc8b7a3c9d4baaea5a531 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 14 Jun 2022 20:43:42 +0100 Subject: [PATCH 1/9] `unittest.case`: Further stub improvements --- stdlib/builtins.pyi | 13 +++---- stdlib/unittest/case.pyi | 73 ++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 56 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 577d5fd99e36..8230732cb0d6 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1321,17 +1321,12 @@ def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ... # We need recursive types to express the type of the second argument to `isinstance` properly, hence the use of `Any` if sys.version_info >= (3, 10): - def isinstance( - __obj: object, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...] - ) -> bool: ... - def issubclass( - __cls: type, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...] - ) -> bool: ... - + _IsInstanceClassInfo: TypeAlias = type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...] else: - def isinstance(__obj: object, __class_or_tuple: type | tuple[type | tuple[Any, ...], ...]) -> bool: ... - def issubclass(__cls: type, __class_or_tuple: type | tuple[type | tuple[Any, ...], ...]) -> bool: ... + _IsInstanceClassInfo: TypeAlias = type | tuple[type | tuple[Any, ...], ...] +def isinstance(__obj: object, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... +def issubclass(__obj: object, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... def len(__obj: Sized) -> int: ... def license() -> None: ... def locals() -> dict[str, Any]: ... diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 15b573edeebb..83731347c2ad 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -1,7 +1,8 @@ import logging import sys import unittest.result -from _typeshed import Self, SupportsDunderGE, SupportsSub +from _typeshed import Self, SupportsDunderGE, SupportsDunderLE, SupportsRichComparison, SupportsSub +from builtins import _IsInstanceClassInfo from collections.abc import Callable, Container, Iterable, Mapping, Sequence, Set as AbstractSet from contextlib import AbstractContextManager from types import TracebackType @@ -19,7 +20,7 @@ from typing import ( TypeVar, overload, ) -from typing_extensions import ParamSpec +from typing_extensions import ParamSpec, TypeAlias from warnings import WarningMessage if sys.version_info >= (3, 9): @@ -77,6 +78,8 @@ class SkipTest(Exception): class _SupportsAbsAndDunderGE(SupportsDunderGE, SupportsAbs[Any], Protocol): ... +_SupportsDunderLeOrDunderGe: TypeAlias = SupportsDunderGE | SupportsDunderLE + class TestCase: failureException: type[BaseException] longMessage: bool @@ -105,18 +108,18 @@ class TestCase: def assertNotEqual(self, first: Any, second: Any, msg: Any = ...) -> None: ... def assertTrue(self, expr: Any, msg: Any = ...) -> None: ... def assertFalse(self, expr: Any, msg: Any = ...) -> None: ... - def assertIs(self, expr1: Any, expr2: Any, msg: Any = ...) -> None: ... - def assertIsNot(self, expr1: Any, expr2: Any, msg: Any = ...) -> None: ... - def assertIsNone(self, obj: Any, msg: Any = ...) -> None: ... - def assertIsNotNone(self, obj: Any, msg: Any = ...) -> None: ... + def assertIs(self, expr1: object, expr2: object, msg: Any = ...) -> None: ... + def assertIsNot(self, expr1: object, expr2: object, msg: Any = ...) -> None: ... + def assertIsNone(self, obj: object, msg: Any = ...) -> None: ... + def assertIsNotNone(self, obj: object, msg: Any = ...) -> None: ... def assertIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = ...) -> None: ... def assertNotIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = ...) -> None: ... - def assertIsInstance(self, obj: Any, cls: type | tuple[type, ...], msg: Any = ...) -> None: ... - def assertNotIsInstance(self, obj: Any, cls: type | tuple[type, ...], msg: Any = ...) -> None: ... - def assertGreater(self, a: Any, b: Any, msg: Any = ...) -> None: ... - def assertGreaterEqual(self, a: Any, b: Any, msg: Any = ...) -> None: ... - def assertLess(self, a: Any, b: Any, msg: Any = ...) -> None: ... - def assertLessEqual(self, a: Any, b: Any, msg: Any = ...) -> None: ... + def assertIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = ...) -> None: ... + def assertNotIsInstance(self, object: Any, cls: _IsInstanceClassInfo, msg: Any = ...) -> None: ... + def assertGreater(self, a: SupportsRichComparison, b: SupportsRichComparison, msg: Any = ...) -> None: ... + def assertGreaterEqual(self, a: _SupportsDunderLeOrDunderGe, b: _SupportsDunderLeOrDunderGe, msg: Any = ...) -> None: ... + def assertLess(self, a: SupportsRichComparison, b: SupportsRichComparison, msg: Any = ...) -> None: ... + def assertLessEqual(self, a: _SupportsDunderLeOrDunderGe, b: _SupportsDunderLeOrDunderGe, msg: Any = ...) -> None: ... # `assertRaises`, `assertRaisesRegex`, and `assertRaisesRegexp` # are not using `ParamSpec` intentionally, # because they might be used with explicitly wrong arg types to raise some error in tests. @@ -249,45 +252,21 @@ class TestCase: def _formatMessage(self, msg: str | None, standardMsg: str) -> str: ... # undocumented def _getAssertEqualityFunc(self, first: Any, second: Any) -> Callable[..., None]: ... # undocumented if sys.version_info < (3, 12): - def failUnlessEqual(self, first: Any, second: Any, msg: Any = ...) -> None: ... - def assertEquals(self, first: Any, second: Any, msg: Any = ...) -> None: ... - def failIfEqual(self, first: Any, second: Any, msg: Any = ...) -> None: ... - def assertNotEquals(self, first: Any, second: Any, msg: Any = ...) -> None: ... - def failUnless(self, expr: bool, msg: Any = ...) -> None: ... - def assert_(self, expr: bool, msg: Any = ...) -> None: ... - def failIf(self, expr: bool, msg: Any = ...) -> None: ... - @overload - def failUnlessRaises( # type: ignore[misc] - self, - exception: type[BaseException] | tuple[type[BaseException], ...], - callable: Callable[_P, object] = ..., - *args: _P.args, - **kwargs: _P.kwargs, - ) -> None: ... - @overload - def failUnlessRaises(self, exception: type[_E] | tuple[type[_E], ...], msg: Any = ...) -> _AssertRaisesContext[_E]: ... + failUnlessEqual = assertEqual + assertEquals = assertEqual + failIfEqual = assertNotEqual + assertNotEquals = assertNotEqual + failUnless = assertTrue + assert_ = assertTrue + failIf = assertFalse + failUnlessRaises = assertRaises failUnlessAlmostEqual = assertAlmostEqual assertAlmostEquals = assertAlmostEqual failIfAlmostEqual = assertNotAlmostEqual assertNotAlmostEquals = assertNotAlmostEqual - def assertRegexpMatches(self, text: AnyStr, regex: AnyStr | Pattern[AnyStr], msg: Any = ...) -> None: ... - def assertNotRegexpMatches(self, text: AnyStr, regex: AnyStr | Pattern[AnyStr], msg: Any = ...) -> None: ... - @overload - def assertRaisesRegexp( # type: ignore[misc] - self, - exception: type[BaseException] | tuple[type[BaseException], ...], - expected_regex: str | bytes | Pattern[str] | Pattern[bytes], - callable: Callable[..., object], - *args: Any, - **kwargs: Any, - ) -> None: ... - @overload - def assertRaisesRegexp( - self, - exception: type[_E] | tuple[type[_E], ...], - expected_regex: str | bytes | Pattern[str] | Pattern[bytes], - msg: Any = ..., - ) -> _AssertRaisesContext[_E]: ... + assertRegexpMatches = assertRegex + assertNotRegexpMatches = assertNotRegex + assertRaisesRegexp = assertRaisesRegex def assertDictContainsSubset( self, subset: Mapping[Any, Any], dictionary: Mapping[Any, Any], msg: object = ... ) -> None: ... From 3ad3eea63ce4905763a24095e2da6fd219b2a278 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 14 Jun 2022 20:52:25 +0100 Subject: [PATCH 2/9] Update builtins.pyi --- stdlib/builtins.pyi | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 8230732cb0d6..72e0236e5273 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -52,6 +52,7 @@ from typing import ( # noqa: Y027 SupportsInt, SupportsRound, TypeVar, + Union, overload, ) from typing_extensions import Literal, LiteralString, SupportsIndex, TypeAlias, TypeGuard, final @@ -1321,12 +1322,12 @@ def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ... # We need recursive types to express the type of the second argument to `isinstance` properly, hence the use of `Any` if sys.version_info >= (3, 10): - _IsInstanceClassInfo: TypeAlias = type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...] + _IsInstanceClassInfo: TypeAlias = Union[type, types.UnionType, tuple[type | types.UnionType | tuple[Any, ...], ...]] else: _IsInstanceClassInfo: TypeAlias = type | tuple[type | tuple[Any, ...], ...] def isinstance(__obj: object, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... -def issubclass(__obj: object, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... +def issubclass(__cls: type, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... def len(__obj: Sized) -> int: ... def license() -> None: ... def locals() -> dict[str, Any]: ... From d9ece6e4bd530b2455d3840ac3600d2fcac180f0 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 14 Jun 2022 20:53:57 +0100 Subject: [PATCH 3/9] Update stdlib/unittest/case.pyi --- stdlib/unittest/case.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 83731347c2ad..f891e8684bc4 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -115,7 +115,7 @@ class TestCase: def assertIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = ...) -> None: ... def assertNotIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = ...) -> None: ... def assertIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = ...) -> None: ... - def assertNotIsInstance(self, object: Any, cls: _IsInstanceClassInfo, msg: Any = ...) -> None: ... + def assertNotIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = ...) -> None: ... def assertGreater(self, a: SupportsRichComparison, b: SupportsRichComparison, msg: Any = ...) -> None: ... def assertGreaterEqual(self, a: _SupportsDunderLeOrDunderGe, b: _SupportsDunderLeOrDunderGe, msg: Any = ...) -> None: ... def assertLess(self, a: SupportsRichComparison, b: SupportsRichComparison, msg: Any = ...) -> None: ... From edf54ecce17464ea7e7f2de4f47f759e4e69de3b Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 14 Jun 2022 20:55:26 +0100 Subject: [PATCH 4/9] Update stdlib/builtins.pyi --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 72e0236e5273..cd36521dd468 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1324,7 +1324,7 @@ def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ... if sys.version_info >= (3, 10): _IsInstanceClassInfo: TypeAlias = Union[type, types.UnionType, tuple[type | types.UnionType | tuple[Any, ...], ...]] else: - _IsInstanceClassInfo: TypeAlias = type | tuple[type | tuple[Any, ...], ...] + _IsInstanceClassInfo: TypeAlias = Union[type, tuple[type | tuple[Any, ...], ...]] def isinstance(__obj: object, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... def issubclass(__cls: type, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... From 9b4d3e81880dca7ddde0e9e64f7563396494681c Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 14 Jun 2022 21:03:07 +0100 Subject: [PATCH 5/9] Give up on an alias in `builtins`, pytype won't let me --- stdlib/builtins.pyi | 14 +++++++++----- stdlib/unittest/case.pyi | 9 +++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index cd36521dd468..577d5fd99e36 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -52,7 +52,6 @@ from typing import ( # noqa: Y027 SupportsInt, SupportsRound, TypeVar, - Union, overload, ) from typing_extensions import Literal, LiteralString, SupportsIndex, TypeAlias, TypeGuard, final @@ -1322,12 +1321,17 @@ def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ... # We need recursive types to express the type of the second argument to `isinstance` properly, hence the use of `Any` if sys.version_info >= (3, 10): - _IsInstanceClassInfo: TypeAlias = Union[type, types.UnionType, tuple[type | types.UnionType | tuple[Any, ...], ...]] + def isinstance( + __obj: object, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...] + ) -> bool: ... + def issubclass( + __cls: type, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...] + ) -> bool: ... + else: - _IsInstanceClassInfo: TypeAlias = Union[type, tuple[type | tuple[Any, ...], ...]] + def isinstance(__obj: object, __class_or_tuple: type | tuple[type | tuple[Any, ...], ...]) -> bool: ... + def issubclass(__cls: type, __class_or_tuple: type | tuple[type | tuple[Any, ...], ...]) -> bool: ... -def isinstance(__obj: object, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... -def issubclass(__cls: type, __class_or_tuple: _IsInstanceClassInfo) -> bool: ... def len(__obj: Sized) -> int: ... def license() -> None: ... def locals() -> dict[str, Any]: ... diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index f891e8684bc4..b4121c095ab0 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -2,10 +2,9 @@ import logging import sys import unittest.result from _typeshed import Self, SupportsDunderGE, SupportsDunderLE, SupportsRichComparison, SupportsSub -from builtins import _IsInstanceClassInfo from collections.abc import Callable, Container, Iterable, Mapping, Sequence, Set as AbstractSet from contextlib import AbstractContextManager -from types import TracebackType +from types import TracebackType, UnionType from typing import ( Any, AnyStr, @@ -18,6 +17,7 @@ from typing import ( SupportsAbs, SupportsRound, TypeVar, + Union, overload, ) from typing_extensions import ParamSpec, TypeAlias @@ -80,6 +80,11 @@ class _SupportsAbsAndDunderGE(SupportsDunderGE, SupportsAbs[Any], Protocol): ... _SupportsDunderLeOrDunderGe: TypeAlias = SupportsDunderGE | SupportsDunderLE +if sys.version_info >= (3, 10): + _IsInstanceClassInfo: TypeAlias = Union[type, UnionType, tuple[type | UnionType | tuple[Any, ...], ...]] +else: + _IsInstanceClassInfo: TypeAlias = Union[type, tuple[type | tuple[Any, ...], ...]] + class TestCase: failureException: type[BaseException] longMessage: bool From 62d7447485d4d2b37733c1e468b57aa93a5159a6 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 14 Jun 2022 21:05:07 +0100 Subject: [PATCH 6/9] Update case.pyi --- stdlib/unittest/case.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index b4121c095ab0..d2c1af189cec 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -4,7 +4,7 @@ import unittest.result from _typeshed import Self, SupportsDunderGE, SupportsDunderLE, SupportsRichComparison, SupportsSub from collections.abc import Callable, Container, Iterable, Mapping, Sequence, Set as AbstractSet from contextlib import AbstractContextManager -from types import TracebackType, UnionType +from types import TracebackType from typing import ( Any, AnyStr, @@ -26,6 +26,9 @@ from warnings import WarningMessage if sys.version_info >= (3, 9): from types import GenericAlias +if sys.version_info >= (3, 10): + from types import UnionType + _T = TypeVar("_T") _S = TypeVar("_S", bound=SupportsSub[Any, Any]) _E = TypeVar("_E", bound=BaseException) From 52b33f806f51b3a8b2815d9c17260a6a20d427e1 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 20 Jun 2022 17:31:39 +0100 Subject: [PATCH 7/9] Address review --- stdlib/_typeshed/__init__.pyi | 20 ++++++------- stdlib/unittest/case.pyi | 24 +++++++++++----- test_cases/stdlib/test_unittest.py | 46 ++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 162c40522224..d8a5b03ac924 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -50,21 +50,21 @@ class SupportsAnext(Protocol[_T_co]): # Comparison protocols -class SupportsDunderLT(Protocol): - def __lt__(self, __other: Any) -> bool: ... +class SupportsDunderLT(Protocol[_T_contra]): + def __lt__(self, __other: _T_contra) -> bool: ... -class SupportsDunderGT(Protocol): - def __gt__(self, __other: Any) -> bool: ... +class SupportsDunderGT(Protocol[_T_contra]): + def __gt__(self, __other: _T_contra) -> bool: ... -class SupportsDunderLE(Protocol): - def __le__(self, __other: Any) -> bool: ... +class SupportsDunderLE(Protocol[_T_contra]): + def __le__(self, __other: _T_contra) -> bool: ... -class SupportsDunderGE(Protocol): - def __ge__(self, __other: Any) -> bool: ... +class SupportsDunderGE(Protocol[_T_contra]): + def __ge__(self, __other: _T_contra) -> bool: ... -class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderLE, SupportsDunderGE, Protocol): ... +class SupportsAllComparisons(SupportsDunderLT[Any], SupportsDunderGT[Any], SupportsDunderLE[Any], SupportsDunderGE[Any], Protocol): ... -SupportsRichComparison: TypeAlias = SupportsDunderLT | SupportsDunderGT +SupportsRichComparison: TypeAlias = SupportsDunderLT[Any] | SupportsDunderGT[Any] SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001 # Dunder protocols diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index d2c1af189cec..e3ab55d0276b 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -1,7 +1,7 @@ import logging import sys import unittest.result -from _typeshed import Self, SupportsDunderGE, SupportsDunderLE, SupportsRichComparison, SupportsSub +from _typeshed import Self, SupportsDunderGE, SupportsDunderGT, SupportsDunderLE, SupportsDunderLT, SupportsSub from collections.abc import Callable, Container, Iterable, Mapping, Sequence, Set as AbstractSet from contextlib import AbstractContextManager from types import TracebackType @@ -81,8 +81,6 @@ class SkipTest(Exception): class _SupportsAbsAndDunderGE(SupportsDunderGE, SupportsAbs[Any], Protocol): ... -_SupportsDunderLeOrDunderGe: TypeAlias = SupportsDunderGE | SupportsDunderLE - if sys.version_info >= (3, 10): _IsInstanceClassInfo: TypeAlias = Union[type, UnionType, tuple[type | UnionType | tuple[Any, ...], ...]] else: @@ -124,10 +122,22 @@ class TestCase: def assertNotIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = ...) -> None: ... def assertIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = ...) -> None: ... def assertNotIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = ...) -> None: ... - def assertGreater(self, a: SupportsRichComparison, b: SupportsRichComparison, msg: Any = ...) -> None: ... - def assertGreaterEqual(self, a: _SupportsDunderLeOrDunderGe, b: _SupportsDunderLeOrDunderGe, msg: Any = ...) -> None: ... - def assertLess(self, a: SupportsRichComparison, b: SupportsRichComparison, msg: Any = ...) -> None: ... - def assertLessEqual(self, a: _SupportsDunderLeOrDunderGe, b: _SupportsDunderLeOrDunderGe, msg: Any = ...) -> None: ... + @overload + def assertGreater(self, a: SupportsDunderGT[_T], b: _T, msg: Any = ...) -> None: ... + @overload + def assertGreater(self, a: _T, b: SupportsDunderLT[_T], msg: Any = ...) -> None: ... + @overload + def assertGreaterEqual(self, a: SupportsDunderGE[_T], b: _T, msg: Any = ...) -> None: ... + @overload + def assertGreaterEqual(self, a: _T, b: SupportsDunderLE[_T], msg: Any = ...) -> None: ... + @overload + def assertLess(self, a: SupportsDunderLT[_T], b: _T, msg: Any = ...) -> None: ... + @overload + def assertLess(self, a: _T, b: SupportsDunderGT[_T], msg: Any = ...) -> None: ... + @overload + def assertLessEqual(self, a: SupportsDunderLT[_T], b: _T, msg: Any = ...) -> None: ... + @overload + def assertLessEqual(self, a: _T, b: SupportsDunderGT[_T], msg: Any = ...) -> None: ... # `assertRaises`, `assertRaisesRegex`, and `assertRaisesRegexp` # are not using `ParamSpec` intentionally, # because they might be used with explicitly wrong arg types to raise some error in tests. diff --git a/test_cases/stdlib/test_unittest.py b/test_cases/stdlib/test_unittest.py index 086a1aef6211..9734a0bbf795 100644 --- a/test_cases/stdlib/test_unittest.py +++ b/test_cases/stdlib/test_unittest.py @@ -7,6 +7,10 @@ case = unittest.TestCase() +### +# Tests for assertAlmostEqual +### + case.assertAlmostEqual(2.4, 2.41) case.assertAlmostEqual(Fraction(49, 50), Fraction(48, 50)) case.assertAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1), delta=timedelta(hours=1)) @@ -20,6 +24,10 @@ case.assertAlmostEqual("foo", "bar") # type: ignore[call-overload] case.assertAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1)) # type: ignore[arg-type] +### +# Tests for assertNotAlmostEqual +### + case.assertNotAlmostEqual(Fraction(49, 50), Fraction(48, 50)) case.assertNotAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1), delta=timedelta(hours=1)) case.assertNotAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1), None, "foo", timedelta(hours=1)) @@ -27,3 +35,41 @@ case.assertNotAlmostEqual(2.4, 2.41, places=9, delta=0.02) # type: ignore[call-overload] case.assertNotAlmostEqual("foo", "bar") # type: ignore[call-overload] case.assertNotAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1)) # type: ignore[arg-type] + +### +# Tests for assertGreater +### + +class Spam: + def __lt__(self, other: object) -> bool: + return True + +class Eggs: + def __gt__(self, other: object) -> bool: + return True + +class Ham: + def __lt__(self, other: "Ham") -> bool: + if not isinstance(other, Ham): + return NotImplemented + return True + +class Bacon: + def __gt__(self, other: "Bacon") -> bool: + if not isinstance(other, Bacon): + return NotImplemented + return True + +case.assertGreater(5.8, 3) +case.assertGreater(Decimal('4.5'), Fraction(3, 2)) +case.assertGreater(Fraction(3, 2), 0.9) +case.assertGreater(Eggs(), object()) +case.assertGreater(object(), Spam()) +case.assertGreater(Ham(), Ham()) +case.assertGreater(Bacon(), Bacon()) + +case.assertGreater(object(), object()) # type: ignore +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 From 04e3554fab73a172bf15d9c0a6d5f873c2c5d5f3 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 20 Jun 2022 17:35:32 +0100 Subject: [PATCH 8/9] Black --- stdlib/_typeshed/__init__.pyi | 4 +++- test_cases/stdlib/test_unittest.py | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index d8a5b03ac924..bdaf88feb2db 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -62,7 +62,9 @@ class SupportsDunderLE(Protocol[_T_contra]): class SupportsDunderGE(Protocol[_T_contra]): def __ge__(self, __other: _T_contra) -> bool: ... -class SupportsAllComparisons(SupportsDunderLT[Any], SupportsDunderGT[Any], SupportsDunderLE[Any], SupportsDunderGE[Any], Protocol): ... +class SupportsAllComparisons( + SupportsDunderLT[Any], SupportsDunderGT[Any], SupportsDunderLE[Any], SupportsDunderGE[Any], Protocol +): ... SupportsRichComparison: TypeAlias = SupportsDunderLT[Any] | SupportsDunderGT[Any] SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001 diff --git a/test_cases/stdlib/test_unittest.py b/test_cases/stdlib/test_unittest.py index 555f0fcb49f4..81098cb3c9ae 100644 --- a/test_cases/stdlib/test_unittest.py +++ b/test_cases/stdlib/test_unittest.py @@ -40,28 +40,33 @@ # Tests for assertGreater ### + class Spam: def __lt__(self, other: object) -> bool: return True + class Eggs: def __gt__(self, other: object) -> bool: return True + class Ham: def __lt__(self, other: "Ham") -> bool: if not isinstance(other, Ham): return NotImplemented return True + class Bacon: def __gt__(self, other: "Bacon") -> bool: if not isinstance(other, Bacon): return NotImplemented return True + case.assertGreater(5.8, 3) -case.assertGreater(Decimal('4.5'), Fraction(3, 2)) +case.assertGreater(Decimal("4.5"), Fraction(3, 2)) case.assertGreater(Fraction(3, 2), 0.9) case.assertGreater(Eggs(), object()) case.assertGreater(object(), Spam()) From 897c65c9e34532ba0ea885c309a55c440ce03aa3 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Mon, 20 Jun 2022 17:37:25 +0100 Subject: [PATCH 9/9] Make pyright happy --- stdlib/unittest/case.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index e3ab55d0276b..6a162f6df1f4 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -79,7 +79,7 @@ def skipUnless(condition: object, reason: str) -> Callable[[_FT], _FT]: ... class SkipTest(Exception): def __init__(self, reason: str) -> None: ... -class _SupportsAbsAndDunderGE(SupportsDunderGE, SupportsAbs[Any], Protocol): ... +class _SupportsAbsAndDunderGE(SupportsDunderGE[Any], SupportsAbs[Any], Protocol): ... if sys.version_info >= (3, 10): _IsInstanceClassInfo: TypeAlias = Union[type, UnionType, tuple[type | UnionType | tuple[Any, ...], ...]]