diff --git a/mypy/main.py b/mypy/main.py index e1c9f20400bc..d2a28a18c6a8 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -9,6 +9,7 @@ import time from collections import defaultdict from gettext import gettext +from io import TextIOWrapper from typing import IO, Any, Final, NoReturn, Sequence, TextIO from mypy import build, defaults, state, util @@ -74,6 +75,10 @@ def main( if args is None: args = sys.argv[1:] + # Write an escape sequence instead of raising an exception on encoding errors. + if isinstance(stdout, TextIOWrapper) and stdout.errors == "strict": + stdout.reconfigure(errors="backslashreplace") + fscache = FileSystemCache() sources, options = process_options(args, stdout=stdout, stderr=stderr, fscache=fscache) if clean_exit: