SF.net SVN: docutils:[9800 ] trunk/docutils/test/allte sts.py

aa-turner--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9800
          http://sourceforge.net/p/docutils/code/9800
Author:   aa-turner
Date:     2024-07-31 09:56:18 +0000 (Wed, 31 Jul 2024)
Log Message:
-----------
Add type hints to alltests.py

Modified Paths:
--------------
    trunk/docutils/test/alltests.py

Modified: trunk/docutils/test/alltests.py
===================================================================
--- trunk/docutils/test/alltests.py	2024-07-31 09:20:21 UTC (rev 9799)
+++ trunk/docutils/test/alltests.py	2024-07-31 09:56:18 UTC (rev 9800)
@@ -6,6 +6,8 @@
 #         Garth Kidd <[email protected]>
 # Copyright: This module has been placed in the public domain.
 
+from __future__ import annotations
+
 __doc__ = """\
 All modules named 'test_*.py' in the current directory, and recursively in
 subdirectories (packages) called 'test_*', are loaded and test suites within
@@ -22,7 +24,9 @@
 from pathlib import Path    # noqa: E402
 import platform             # noqa: E402
 import sys                  # noqa: E402
+from typing import TYPE_CHECKING  # noqa: E402
 
+
 # Prepend the "docutils root" to the Python library path
 # so we import the local `docutils` package.
 DOCUTILS_ROOT = Path(__file__).resolve().parents[1]
@@ -30,34 +34,48 @@
 
 import docutils             # noqa: E402
 
+if TYPE_CHECKING:
+    import types
+    from typing import TextIO
+    from unittest.case import TestCase
 
+    from typing_extensions import TypeAlias
+
+    ErrorTriple: TypeAlias = tuple[
+        type[BaseException],
+        BaseException,
+        types.TracebackType,
+    ]
+
+
 class Tee:
+    """Write to a file and stdout simultaneously."""
 
-    """Write to a file and a stream (default: stdout) simultaneously."""
-
-    def __init__(self, filename, stream=sys.__stdout__):
-        self.file = open(filename, 'w', encoding='utf-8',
-                         errors='backslashreplace')
+    def __init__(self, filename: str) -> None:
+        self.file: TextIO | None = open(
+            filename, 'w', encoding='utf-8', errors='backslashreplace',
+        )
         atexit.register(self.close)
-        self.stream = stream
-        self.encoding = getattr(stream, 'encoding', None)
+        self.stream = sys.__stdout__
+        self.encoding: str = sys.__stdout__.encoding
 
-    def close(self):
-        self.file.close()
-        self.file = None
+    def close(self) -> None:
+        if self.file is not None:
+            self.file.close()
+            self.file = None
 
-    def write(self, string):
+    def write(self, string: str) -> None:
         try:
             self.stream.write(string)
         except UnicodeEncodeError:
             bstring = string.encode(self.encoding, errors='backslashreplace')
             self.stream.write(bstring.decode())
-        if self.file:
+        if self.file is not None:
             self.file.write(string)
 
-    def flush(self):
+    def flush(self) -> None:
         self.stream.flush()
-        if self.file:
+        if self.file is not None:
             self.file.flush()
 
 
@@ -69,7 +87,9 @@
 
 class NumbersTestResult(unittest.TextTestResult):
     """Result class that counts subTests."""
-    def addSubTest(self, test, subtest, error):
+    def addSubTest(
+        self, test: TestCase, subtest: TestCase, error: ErrorTriple | None,
+    ) -> None:
         super().addSubTest(test, subtest, error)
         self.testsRun += 1
         if self.dots:

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.