From c69b84f2367b44c0692589aef037deeca9e11a4a Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 21 Dec 2021 20:42:32 -0500 Subject: [PATCH] fix typing issues in mypy 0.920 --- src/_pytest/capture.py | 2 +- src/_pytest/junitxml.py | 2 +- src/_pytest/logging.py | 19 ++++++++++++------- testing/code/test_source.py | 3 +-- testing/test_assertrewrite.py | 2 +- testing/test_legacypath.py | 2 +- testing/test_pastebin.py | 5 ++++- testing/test_recwarn.py | 4 ++-- 8 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index 884f035e2..d82ee9552 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -112,7 +112,7 @@ def _py36_windowsconsoleio_workaround(stream: TextIO) -> None: buffering = -1 return io.TextIOWrapper( - open(os.dup(f.fileno()), mode, buffering), # type: ignore[arg-type] + open(os.dup(f.fileno()), mode, buffering), f.encoding, f.errors, f.newlines, diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index 13688251f..8249dccff 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -92,7 +92,7 @@ class _NodeReporter: self.xml = xml self.add_stats = self.xml.add_stats self.family = self.xml.family - self.duration = 0 + self.duration = 0.0 self.properties: List[Tuple[str, str]] = [] self.nodes: List[ET.Element] = [] self.attrs: Dict[str, str] = {} diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 31ad83010..796a1c3f8 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -1,4 +1,5 @@ """Access and control log capturing.""" +import io import logging import os import re @@ -13,6 +14,7 @@ from typing import List from typing import Mapping from typing import Optional from typing import Tuple +from typing import TYPE_CHECKING from typing import TypeVar from typing import Union @@ -34,6 +36,11 @@ from _pytest.main import Session from _pytest.stash import StashKey from _pytest.terminal import TerminalReporter +if TYPE_CHECKING: + logging_StreamHandler = logging.StreamHandler[StringIO] +else: + logging_StreamHandler = logging.StreamHandler + DEFAULT_LOG_FORMAT = "%(levelname)-8s %(name)s:%(filename)s:%(lineno)d %(message)s" DEFAULT_LOG_DATE_FORMAT = "%H:%M:%S" @@ -322,11 +329,9 @@ class catching_logs: root_logger.removeHandler(self.handler) -class LogCaptureHandler(logging.StreamHandler): +class LogCaptureHandler(logging_StreamHandler): """A logging handler that stores log records and the log text.""" - stream: StringIO - def __init__(self) -> None: """Create a new log handler.""" super().__init__(StringIO()) @@ -621,7 +626,8 @@ class LoggingPlugin: if not fpath.parent.exists(): fpath.parent.mkdir(exist_ok=True, parents=True) - stream = fpath.open(mode="w", encoding="UTF-8") + # https://github.com/python/mypy/issues/11193 + stream: io.TextIOWrapper = fpath.open(mode="w", encoding="UTF-8") # type: ignore[assignment] if sys.version_info >= (3, 7): old_stream = self.log_file_handler.setStream(stream) else: @@ -633,8 +639,7 @@ class LoggingPlugin: finally: self.log_file_handler.release() if old_stream: - # https://github.com/python/typeshed/pull/5663 - old_stream.close() # type:ignore[attr-defined] + old_stream.close() def _log_cli_enabled(self): """Return whether live logging is enabled.""" @@ -758,7 +763,7 @@ class _FileHandler(logging.FileHandler): pass -class _LiveLoggingStreamHandler(logging.StreamHandler): +class _LiveLoggingStreamHandler(logging_StreamHandler): """A logging StreamHandler used by the live logging feature: it will write a newline before the first log message in each test. diff --git a/testing/code/test_source.py b/testing/code/test_source.py index 53202ee27..b7b3c1711 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -332,8 +332,7 @@ def test_findsource(monkeypatch) -> None: lines = ["if 1:\n", " def x():\n", " pass\n"] co = compile("".join(lines), filename, "exec") - # Type ignored because linecache.cache is private. - monkeypatch.setitem(linecache.cache, filename, (1, None, lines, filename)) # type: ignore[attr-defined] + monkeypatch.setitem(linecache.cache, filename, (1, None, lines, filename)) src, lineno = findsource(co) assert src is not None diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 61f5760e7..a88c7eba3 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -1057,7 +1057,7 @@ class TestAssertionRewriteHookDetails: e = OSError() e.errno = 10 raise e - yield # type:ignore[unreachable] + yield monkeypatch.setattr( _pytest.assertion.rewrite, "atomic_write", atomic_write_failed diff --git a/testing/test_legacypath.py b/testing/test_legacypath.py index 9ab139df4..1d3fdb4bd 100644 --- a/testing/test_legacypath.py +++ b/testing/test_legacypath.py @@ -14,7 +14,7 @@ def test_item_fspath(pytester: pytest.Pytester) -> None: items2, hookrec = pytester.inline_genitems(item.nodeid) (item2,) = items2 assert item2.name == item.name - assert item2.fspath == item.fspath # type: ignore[attr-defined] + assert item2.fspath == item.fspath assert item2.path == item.path diff --git a/testing/test_pastebin.py b/testing/test_pastebin.py index b338519ae..86b231f8b 100644 --- a/testing/test_pastebin.py +++ b/testing/test_pastebin.py @@ -1,3 +1,4 @@ +import email.message import io from typing import List from typing import Union @@ -98,7 +99,9 @@ class TestPaste: def mocked(url, data): calls.append((url, data)) - raise urllib.error.HTTPError(url, 400, "Bad request", {}, io.BytesIO()) + raise urllib.error.HTTPError( + url, 400, "Bad request", email.message.Message(), io.BytesIO() + ) monkeypatch.setattr(urllib.request, "urlopen", mocked) return calls diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index d3f218f16..c5a8ae90f 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -114,13 +114,13 @@ class TestDeprecatedCall: # Type ignored because `onceregistry` and `filters` are not # documented API. onceregistry = warnings.onceregistry.copy() # type: ignore - filters = warnings.filters[:] # type: ignore + filters = warnings.filters[:] warn = warnings.warn warn_explicit = warnings.warn_explicit self.test_deprecated_call_raises() self.test_deprecated_call() assert onceregistry == warnings.onceregistry # type: ignore - assert filters == warnings.filters # type: ignore + assert filters == warnings.filters assert warn is warnings.warn assert warn_explicit is warnings.warn_explicit