Skip to content

Commit fc441ba

Browse files
authored
Merge pull request #189 from dmtucker/issue152
Resolve PYTHONWARNDEFAULTENCODING warnings
2 parents b9c6313 + eb30be4 commit fc441ba

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/pytest_mypy/__init__.py

+9-8
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

+27-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
MYPY_VERSION = Version(mypy.version.__version__)
13+
PYTEST_VERSION = Version(pytest.__version__)
1314
PYTHON_VERSION = Version(
1415
".".join(
1516
str(token)
@@ -60,6 +61,30 @@ def pyfunc(x: int) -> int:
6061
assert result.ret == pytest.ExitCode.OK
6162

6263

64+
@pytest.mark.skipif(
65+
PYTEST_VERSION < Version("7.4"),
66+
reason="https://github.com/pytest-dev/pytest/pull/10935",
67+
)
68+
@pytest.mark.skipif(
69+
PYTHON_VERSION < Version("3.10"),
70+
reason="PEP 597 was added in Python 3.10.",
71+
)
72+
@pytest.mark.skipif(
73+
PYTHON_VERSION >= Version("3.12") and MYPY_VERSION < Version("1.5"),
74+
reason="https://github.com/python/mypy/pull/15558",
75+
)
76+
def test_mypy_encoding_warnings(testdir, monkeypatch):
77+
"""Ensure no warnings are detected by PYTHONWARNDEFAULTENCODING."""
78+
testdir.makepyfile("")
79+
monkeypatch.setenv("PYTHONWARNDEFAULTENCODING", "1")
80+
result = testdir.runpytest_subprocess("--mypy")
81+
mypy_file_checks = 1
82+
mypy_status_check = 1
83+
mypy_checks = mypy_file_checks + mypy_status_check
84+
expected_warnings = 2 # https://github.com/python/mypy/issues/14603
85+
result.assert_outcomes(passed=mypy_checks, warnings=expected_warnings)
86+
87+
6388
def test_mypy_pyi(testdir, xdist_args):
6489
"""
6590
Verify that a .py file will be skipped if
@@ -524,7 +549,7 @@ def test_mypy_no_output(testdir, xdist_args):
524549
def pytest_configure(config):
525550
pytest_mypy = config.pluginmanager.getplugin("mypy")
526551
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
527-
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
552+
with open(mypy_config_stash.mypy_results_path, mode="wb") as results_f:
528553
pytest_mypy.MypyResults(
529554
opts=[],
530555
stdout="",
@@ -602,7 +627,7 @@ def test_mypy_xfail_reports_stdout(testdir, xdist_args):
602627
def pytest_configure(config):
603628
pytest_mypy = config.pluginmanager.getplugin("mypy")
604629
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
605-
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
630+
with open(mypy_config_stash.mypy_results_path, mode="wb") as results_f:
606631
pytest_mypy.MypyResults(
607632
opts=[],
608633
stdout="{stdout}",

0 commit comments

Comments
 (0)