16
16
from typing import (
17
17
Any ,
18
18
Dict ,
19
+ IO ,
19
20
Iterator ,
20
21
List ,
21
22
Optional ,
22
- TextIO ,
23
23
Tuple ,
24
24
Union ,
25
25
)
@@ -297,6 +297,7 @@ class MypyResults:
297
297
"""Parsed results from Mypy."""
298
298
299
299
_abspath_errors_type = typing .Dict [str , typing .List [str ]]
300
+ _encoding = "utf-8"
300
301
301
302
opts : List [str ]
302
303
stdout : str
@@ -305,14 +306,14 @@ class MypyResults:
305
306
abspath_errors : _abspath_errors_type
306
307
unmatched_stdout : str
307
308
308
- def dump (self , results_f : TextIO ) -> None :
309
+ def dump (self , results_f : IO [ bytes ] ) -> None :
309
310
"""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 ) )
311
312
312
313
@classmethod
313
- def load (cls , results_f : TextIO ) -> MypyResults :
314
+ def load (cls , results_f : IO [ bytes ] ) -> MypyResults :
314
315
"""Get results cached by dump()."""
315
- return cls (** json .load (results_f ))
316
+ return cls (** json .loads (results_f . read (). decode ( cls . _encoding ) ))
316
317
317
318
@classmethod
318
319
def from_mypy (
@@ -360,7 +361,7 @@ def from_session(cls, session: pytest.Session) -> MypyResults:
360
361
mypy_results_path = session .config .stash [stash_key ["config" ]].mypy_results_path
361
362
with FileLock (str (mypy_results_path ) + ".lock" ):
362
363
try :
363
- with open (mypy_results_path , mode = "r " ) as results_f :
364
+ with open (mypy_results_path , mode = "rb " ) as results_f :
364
365
results = cls .load (results_f )
365
366
except FileNotFoundError :
366
367
results = cls .from_mypy (
@@ -370,7 +371,7 @@ def from_session(cls, session: pytest.Session) -> MypyResults:
370
371
if isinstance (item , MypyFileItem )
371
372
],
372
373
)
373
- with open (mypy_results_path , mode = "w " ) as results_f :
374
+ with open (mypy_results_path , mode = "wb " ) as results_f :
374
375
results .dump (results_f )
375
376
return results
376
377
@@ -393,7 +394,7 @@ def pytest_terminal_summary(
393
394
"""Report mypy results."""
394
395
mypy_results_path = config .stash [stash_key ["config" ]].mypy_results_path
395
396
try :
396
- with open (mypy_results_path , mode = "r " ) as results_f :
397
+ with open (mypy_results_path , mode = "rb " ) as results_f :
397
398
results = MypyResults .load (results_f )
398
399
except FileNotFoundError :
399
400
# No MypyItems executed.
0 commit comments