Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest.case: Further stub improvements #8077

Merged
merged 10 commits into from
Jun 20, 2022
Merged
14 changes: 5 additions & 9 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ from typing import ( # noqa: Y027
SupportsInt,
SupportsRound,
TypeVar,
Union,
overload,
)
from typing_extensions import Literal, LiteralString, SupportsIndex, TypeAlias, TypeGuard, final
Expand Down Expand Up @@ -1321,17 +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):
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 = Union[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, ...], ...]
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved

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]: ...
Expand Down
73 changes: 26 additions & 47 deletions stdlib/unittest/case.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -77,6 +78,8 @@ class SkipTest(Exception):

class _SupportsAbsAndDunderGE(SupportsDunderGE, SupportsAbs[Any], Protocol): ...

_SupportsDunderLeOrDunderGe: TypeAlias = SupportsDunderGE | SupportsDunderLE

class TestCase:
failureException: type[BaseException]
longMessage: bool
Expand Down Expand Up @@ -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: ...
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
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: ...
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
# `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.
Expand Down Expand Up @@ -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: ...
Expand Down