Improve the unittest stubs. (#1927)

* Improvements to stdlib/2/unittest.pyi

* Adds a bunch of missing things.
* Fixes the signatures of assertRaises and failUnlessRaises.
* Corrects the name of a TestCase subclass from CallableTestCase
  to FunctionTestCase.
* Makes defaultTestLoader an instance of TestLoader, as it should
  be, rather than an alias. Based on https://docs.python.org/2/library/unittest.html and
  https://github.com/python/cpython/tree/2.7/Lib/unittest.

* Improvements to stdlib/3/unittest/__init__.pyi

* The constructor arguments to TextTestResult are all required.
* There is a module-level load_tests() function.
* removeResult() returns a bool. Based on https://docs.python.org/3/library/unittest.html and
  https://github.com/python/cpython/tree/master/Lib/unittest.
This commit is contained in:
rchen152
2018-02-27 06:46:10 -08:00
committed by Matthias Kramm
parent 9b6df1d6bc
commit 066c434410
2 changed files with 63 additions and 17 deletions

View File

@@ -2,18 +2,19 @@
# Based on http://docs.python.org/2.7/library/unittest.html
# Only a subset of functionality is included.
from mypy_extensions import NoReturn
from typing import (Any, Callable, Dict, FrozenSet, Iterable, List, Optional,
overload, Pattern, Sequence, Set, TextIO, Tuple, Type,
TypeVar, Union)
from typing import (Any, Callable, Dict, FrozenSet, Iterable, Iterator,
List, Optional, overload, Pattern, Sequence, Set, Text,
TextIO, Tuple, Type, TypeVar, Union)
from abc import abstractmethod, ABCMeta
import types
_T = TypeVar('_T')
_FT = TypeVar('_FT')
_ExceptionType = Union[Type[BaseException], Tuple[Type[BaseException], ...]]
_Regexp = Union[Text, Pattern[Text]]
class Testable(metaclass=ABCMeta):
@abstractmethod
def run(self, result: 'TestResult') -> None: ...
@@ -27,18 +28,28 @@ class Testable(metaclass=ABCMeta):
class TestResult:
errors = ... # type: List[Tuple[Testable, str]]
failures = ... # type: List[Tuple[Testable, str]]
testsRun = 0
skipped = ... # type: List[Tuple[Testable, str]]
expectedFailures = ... # type: List[Tuple[Testable, str]]
unexpectedSuccesses = ... # type: List[Testable]
shouldStop = ... # type: bool
testsRun = ... # type: int
buffer = ... # type: bool
failfast = ... # type: bool
def wasSuccessful(self) -> bool: ...
def stop(self) -> None: ...
def startTest(self, test: Testable) -> None: ...
def stopTest(self, test: Testable) -> None: ...
def startTestRun(self) -> None: ...
def stopTestRun(self) -> None: ...
def addError(self, test: Testable,
err: Tuple[type, Any, Any]) -> None: ... # TODO
def addFailure(self, test: Testable,
err: Tuple[type, Any, Any]) -> None: ... # TODO
def addSuccess(self, test: Testable) -> None: ...
def addSkip(self, test: Testable, reason: str) -> None: ...
def addExpectedFailure(self, test: Testable, err: str) -> None: ...
def addUnexpectedSuccess(self, test: Testable) -> None: ...
class _AssertRaisesBaseContext:
expected = ... # type: Any
@@ -54,6 +65,7 @@ class _AssertRaisesContext(_AssertRaisesBaseContext):
class TestCase(Testable):
failureException = ... # type: Type[BaseException]
longMessage = ... # type: bool
maxDiff = ... # type: Optional[int]
def __init__(self, methodName: str = ...) -> None: ...
def setUp(self) -> None: ...
def tearDown(self) -> None: ...
@@ -115,8 +127,23 @@ class TestCase(Testable):
msg: object = ...) -> None: ...
def assertLessEqual(self, first: Any, second: Any,
msg: object = ...) -> None: ...
def assertRaises(self, expected_exception: type, *args: Any, **kwargs: Any) -> Any: ...
def failUnlessRaises(self, expected_exception: type, *args: Any, **kwargs: Any) -> Any: ...
@overload
def assertRaises(self, exception: _ExceptionType, callable: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ...
@overload
def assertRaises(self, exception: _ExceptionType) -> _AssertRaisesContext: ...
@overload
def assertRaisesRegexp(self, exception: _ExceptionType, regexp: _Regexp, callable: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ...
@overload
def assertRaisesRegexp(self, exception: _ExceptionType, regexp: _Regexp) -> _AssertRaisesContext: ...
def assertRegexpMatches(self, text: Text, regexp: _Regexp, msg: object = ...) -> None: ...
def assertNotRegexpMatches(self, text: Text, regexp: _Regexp, msg: object = ...) -> None: ...
def assertItemsEqual(self, first: Iterable[Any], second: Iterable[Any], msg: object = ...) -> None: ...
def assertDictContainsSubset(self, expected: Dict[Any, Any], actual: Dict[Any, Any], msg: object = ...) -> None: ...
def addTypeEqualityFunc(self, typeobj: type, function: Callable[..., None]) -> None: ...
@overload
def failUnlessRaises(self, exception: _ExceptionType, callable: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ...
@overload
def failUnlessRaises(self, exception: _ExceptionType) -> _AssertRaisesContext: ...
def failIf(self, expr: Any, msg: object = ...) -> None: ...
def assertFalse(self, expr: Any, msg: object = ...) -> None: ...
def assertIs(self, first: object, second: object,
@@ -139,13 +166,14 @@ class TestCase(Testable):
def id(self) -> str: ...
def shortDescription(self) -> str: ... # May return None
def addCleanup(function: Any, *args: Any, **kwargs: Any) -> None: ...
def doCleanups(self) -> bool: ...
def skipTest(self, reason: Any) -> None: ...
class CallableTestCase(Testable):
class FunctionTestCase(Testable):
def __init__(self, testFunc: Callable[[], None],
setUp: Callable[[], None] = ...,
tearDown: Callable[[], None] = ...,
description: str = ...) -> None: ...
setUp: Optional[Callable[[], None]] = ...,
tearDown: Optional[Callable[[], None]] = ...,
description: Optional[str] = ...) -> None: ...
def run(self, result: TestResult) -> None: ...
def debug(self) -> None: ...
def countTestCases(self) -> int: ...
@@ -157,6 +185,7 @@ class TestSuite(Testable):
def run(self, result: TestResult) -> None: ...
def debug(self) -> None: ...
def countTestCases(self) -> int: ...
def __iter__(self) -> Iterator[Testable]: ...
class TestLoader:
testMethodPrefix = ... # type: str
@@ -174,11 +203,16 @@ class TestLoader:
top_level_dir: Optional[str] = ...) -> TestSuite: ...
def getTestCaseNames(self, testCaseClass: Type[TestCase] = ...) -> List[str]: ...
defaultTestLoader = TestLoader
defaultTestLoader = ... # type: TestLoader
class TextTestResult(TestResult):
def __init__(self, stream: TextIO, descriptions: bool, verbosity: int) -> None: ...
class TextTestRunner:
def __init__(self, stream: Optional[TextIO] = ..., descriptions: bool = ...,
verbosity: int = ..., failfast: bool = ...) -> None: ...
verbosity: int = ..., failfast: bool = ..., buffer: bool = ...,
resultclass: Optional[Type[TestResult]] = ...) -> None: ...
def _makeResult(self) -> TestResult: ...
class SkipTest(Exception):
...
@@ -200,5 +234,15 @@ def main(module: str = ..., defaultTest: Optional[str] = ...,
failfast: Optional[bool] = ..., catchbreak: Optional[bool] = ...,
buffer: Optional[bool] = ...) -> TestProgram: ...
def load_tests(loader: TestLoader, tests: TestSuite, pattern: Optional[Text]) -> TestSuite: ...
def installHandler() -> None: ...
def registerResult(result: TestResult) -> None: ...
def removeResult(result: TestResult) -> bool: ...
@overload
def removeHandler() -> None: ...
@overload
def removeHandler(function: Callable[..., Any]) -> Callable[..., Any]: ...
# private but occasionally used
util = ... # type: types.ModuleType

View File

@@ -279,8 +279,8 @@ class TestResult:
outcome: Optional[_SysExcInfoType]) -> None: ...
class TextTestResult(TestResult):
def __init__(self, stream: TextIO = ..., descriptions: bool = ...,
verbosity: int = ...) -> None: ...
def __init__(self, stream: TextIO, descriptions: bool,
verbosity: int) -> None: ...
_TextTestResult = TextTestResult
defaultTestLoader = ... # type: TestLoader
@@ -324,10 +324,12 @@ def main(module: str = ..., defaultTest: _DefaultTestType = ...,
buffer: Optional[bool] = ...,
warnings: Optional[str] = ...) -> TestProgram: ...
def load_tests(loader: TestLoader, tests: TestSuite,
pattern: Optional[str]) -> TestSuite: ...
def installHandler() -> None: ...
def registerResult(result: TestResult) -> None: ...
def removeResult(result: TestResult) -> None: ...
def removeResult(result: TestResult) -> bool: ...
@overload
def removeHandler() -> None: ...
@overload