fix typing issues in mypy 0.920

This commit is contained in:
Anthony Sottile 2021-12-21 20:42:32 -05:00
parent e358bc65a8
commit c69b84f236
8 changed files with 23 additions and 16 deletions

View File

@ -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,

View File

@ -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] = {}

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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