Skip to content

Commit

Permalink
Resolve PYTHONWARNDEFAULTENCODING warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtucker committed Dec 9, 2024
1 parent b9c6313 commit 2cbb3d5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/pytest_mypy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from typing import (
Any,
Dict,
IO,
Iterator,
List,
Optional,
TextIO,
Tuple,
Union,
)
Expand Down Expand Up @@ -297,6 +297,7 @@ class MypyResults:
"""Parsed results from Mypy."""

_abspath_errors_type = typing.Dict[str, typing.List[str]]
_encoding = "utf-8"

opts: List[str]
stdout: str
Expand All @@ -305,14 +306,14 @@ class MypyResults:
abspath_errors: _abspath_errors_type
unmatched_stdout: str

def dump(self, results_f: TextIO) -> None:
def dump(self, results_f: IO[bytes]) -> None:
"""Cache results in a format that can be parsed by load()."""
return json.dump(vars(self), results_f)
results_f.write(json.dumps(vars(self)).encode(self._encoding))

@classmethod
def load(cls, results_f: TextIO) -> MypyResults:
def load(cls, results_f: IO[bytes]) -> MypyResults:
"""Get results cached by dump()."""
return cls(**json.load(results_f))
return cls(**json.loads(results_f.read().decode(cls._encoding)))

@classmethod
def from_mypy(
Expand Down Expand Up @@ -360,7 +361,7 @@ def from_session(cls, session: pytest.Session) -> MypyResults:
mypy_results_path = session.config.stash[stash_key["config"]].mypy_results_path
with FileLock(str(mypy_results_path) + ".lock"):
try:
with open(mypy_results_path, mode="r") as results_f:
with open(mypy_results_path, mode="rb") as results_f:
results = cls.load(results_f)
except FileNotFoundError:
results = cls.from_mypy(
Expand All @@ -370,7 +371,7 @@ def from_session(cls, session: pytest.Session) -> MypyResults:
if isinstance(item, MypyFileItem)
],
)
with open(mypy_results_path, mode="w") as results_f:
with open(mypy_results_path, mode="wb") as results_f:
results.dump(results_f)
return results

Expand All @@ -393,7 +394,7 @@ def pytest_terminal_summary(
"""Report mypy results."""
mypy_results_path = config.stash[stash_key["config"]].mypy_results_path
try:
with open(mypy_results_path, mode="r") as results_f:
with open(mypy_results_path, mode="rb") as results_f:
results = MypyResults.load(results_f)
except FileNotFoundError:
# No MypyItems executed.
Expand Down
25 changes: 23 additions & 2 deletions tests/test_pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


MYPY_VERSION = Version(mypy.version.__version__)
PYTEST_VERSION = Version(pytest.__version__)
PYTHON_VERSION = Version(
".".join(
str(token)
Expand Down Expand Up @@ -60,6 +61,26 @@ def pyfunc(x: int) -> int:
assert result.ret == pytest.ExitCode.OK


@pytest.mark.skipif(
PYTEST_VERSION < Version("7.4"),
reason="https://github.com/pytest-dev/pytest/pull/10935",
)
@pytest.mark.skipif(
PYTHON_VERSION < Version("3.10"),
reason="PEP 597 was added in Python 3.10.",
)
def test_mypy_encoding_warnings(testdir, monkeypatch):
"""Ensure no warnings are detected by PYTHONWARNDEFAULTENCODING."""
testdir.makepyfile("")
monkeypatch.setenv("PYTHONWARNDEFAULTENCODING", "1")
result = testdir.runpytest_subprocess("--mypy")
mypy_file_checks = 1
mypy_status_check = 1
mypy_checks = mypy_file_checks + mypy_status_check
expected_warnings = 2 # https://github.com/python/mypy/issues/14603
result.assert_outcomes(passed=mypy_checks, warnings=expected_warnings)


def test_mypy_pyi(testdir, xdist_args):
"""
Verify that a .py file will be skipped if
Expand Down Expand Up @@ -524,7 +545,7 @@ def test_mypy_no_output(testdir, xdist_args):
def pytest_configure(config):
pytest_mypy = config.pluginmanager.getplugin("mypy")
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
with open(mypy_config_stash.mypy_results_path, mode="wb") as results_f:
pytest_mypy.MypyResults(
opts=[],
stdout="",
Expand Down Expand Up @@ -602,7 +623,7 @@ def test_mypy_xfail_reports_stdout(testdir, xdist_args):
def pytest_configure(config):
pytest_mypy = config.pluginmanager.getplugin("mypy")
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
with open(mypy_config_stash.mypy_results_path, mode="wb") as results_f:
pytest_mypy.MypyResults(
opts=[],
stdout="{stdout}",
Expand Down

0 comments on commit 2cbb3d5

Please sign in to comment.