Skip to content

Commit 821f93e

Browse files
committed
Resolve PYTHONWARNDEFAULTENCODING warnings
1 parent b9c6313 commit 821f93e

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/pytest_mypy/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
from typing import (
1717
Any,
1818
Dict,
19+
IO,
1920
Iterator,
2021
List,
2122
Optional,
22-
TextIO,
2323
Tuple,
2424
Union,
2525
)
@@ -297,6 +297,7 @@ class MypyResults:
297297
"""Parsed results from Mypy."""
298298

299299
_abspath_errors_type = typing.Dict[str, typing.List[str]]
300+
_encoding = "utf-8"
300301

301302
opts: List[str]
302303
stdout: str
@@ -305,14 +306,14 @@ class MypyResults:
305306
abspath_errors: _abspath_errors_type
306307
unmatched_stdout: str
307308

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

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

317318
@classmethod
318319
def from_mypy(
@@ -360,7 +361,7 @@ def from_session(cls, session: pytest.Session) -> MypyResults:
360361
mypy_results_path = session.config.stash[stash_key["config"]].mypy_results_path
361362
with FileLock(str(mypy_results_path) + ".lock"):
362363
try:
363-
with open(mypy_results_path, mode="r") as results_f:
364+
with open(mypy_results_path, mode="rb") as results_f:
364365
results = cls.load(results_f)
365366
except FileNotFoundError:
366367
results = cls.from_mypy(
@@ -370,7 +371,7 @@ def from_session(cls, session: pytest.Session) -> MypyResults:
370371
if isinstance(item, MypyFileItem)
371372
],
372373
)
373-
with open(mypy_results_path, mode="w") as results_f:
374+
with open(mypy_results_path, mode="wb") as results_f:
374375
results.dump(results_f)
375376
return results
376377

@@ -393,7 +394,7 @@ def pytest_terminal_summary(
393394
"""Report mypy results."""
394395
mypy_results_path = config.stash[stash_key["config"]].mypy_results_path
395396
try:
396-
with open(mypy_results_path, mode="r") as results_f:
397+
with open(mypy_results_path, mode="rb") as results_f:
397398
results = MypyResults.load(results_f)
398399
except FileNotFoundError:
399400
# No MypyItems executed.

tests/test_pytest_mypy.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ def pyfunc(x: int) -> int:
6060
assert result.ret == pytest.ExitCode.OK
6161

6262

63+
@pytest.mark.skipif(
64+
PYTHON_VERSION < Version("3.10"),
65+
reason="PEP 597 was added in Python 3.10.",
66+
)
67+
def test_mypy_encoding_warnings(testdir, monkeypatch):
68+
"""Ensure no warnings are detected by PYTHONWARNDEFAULTENCODING."""
69+
testdir.makepyfile("")
70+
monkeypatch.setenv("PYTHONWARNDEFAULTENCODING", "1")
71+
result = testdir.runpytest_subprocess("--mypy")
72+
mypy_file_checks = 1
73+
mypy_status_check = 1
74+
mypy_checks = mypy_file_checks + mypy_status_check
75+
expected_warnings = 2 # https://github.com/python/mypy/issues/14603
76+
result.assert_outcomes(passed=mypy_checks, warnings=expected_warnings)
77+
78+
6379
def test_mypy_pyi(testdir, xdist_args):
6480
"""
6581
Verify that a .py file will be skipped if
@@ -524,7 +540,7 @@ def test_mypy_no_output(testdir, xdist_args):
524540
def pytest_configure(config):
525541
pytest_mypy = config.pluginmanager.getplugin("mypy")
526542
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
527-
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
543+
with open(mypy_config_stash.mypy_results_path, mode="wb") as results_f:
528544
pytest_mypy.MypyResults(
529545
opts=[],
530546
stdout="",
@@ -602,7 +618,7 @@ def test_mypy_xfail_reports_stdout(testdir, xdist_args):
602618
def pytest_configure(config):
603619
pytest_mypy = config.pluginmanager.getplugin("mypy")
604620
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
605-
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
621+
with open(mypy_config_stash.mypy_results_path, mode="wb") as results_f:
606622
pytest_mypy.MypyResults(
607623
opts=[],
608624
stdout="{stdout}",

0 commit comments

Comments
 (0)