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.
0 commit comments