Skip to content

Commit ba3641f

Browse files
propagate fixes, & simplify
1 parent f390f25 commit ba3641f

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

mypy/build.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -876,10 +876,9 @@ def verbosity(self) -> int:
876876
def log(self, *message: str) -> None:
877877
if self.verbosity() >= 1:
878878
if message:
879-
print("LOG: ", *message, file=self.stderr)
879+
print("LOG: ", *message, file=self.stderr, flush=True)
880880
else:
881-
print(file=self.stderr)
882-
self.stderr.flush()
881+
print(file=self.stderr, flush=True)
883882

884883
def log_fine_grained(self, *message: str) -> None:
885884
import mypy.build
@@ -889,15 +888,13 @@ def log_fine_grained(self, *message: str) -> None:
889888
elif mypy.build.DEBUG_FINE_GRAINED:
890889
# Output log in a simplified format that is quick to browse.
891890
if message:
892-
print(*message, file=self.stderr)
891+
print(*message, file=self.stderr, flush=True)
893892
else:
894-
print(file=self.stderr)
895-
self.stderr.flush()
893+
print(file=self.stderr, flush=True)
896894

897895
def trace(self, *message: str) -> None:
898896
if self.verbosity() >= 2:
899-
print("TRACE:", *message, file=self.stderr)
900-
self.stderr.flush()
897+
print("TRACE:", *message, file=self.stderr, flush=True)
901898

902899
def add_stats(self, **kwds: Any) -> None:
903900
for key, value in kwds.items():

mypy/config_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def parse_config_file(
322322
323323
If filename is None, fall back to default config files.
324324
"""
325-
stderr = stderr or sys.stderr
325+
stderr = stderr if stderr is not None else sys.stderr
326326

327327
ret = (
328328
_parse_individual_file(filename, stderr)

mypy/main.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def stat_proxy(path: str) -> os.stat_result:
6262
def main(
6363
*,
6464
args: list[str] | None = None,
65-
stdout: TextIO = sys.stdout,
66-
stderr: TextIO = sys.stderr,
65+
stdout: TextIO | None = None,
66+
stderr: TextIO | None = None,
6767
clean_exit: bool = False,
6868
) -> None:
6969
"""Main entry point to the type checker.
@@ -74,6 +74,15 @@ def main(
7474
clean_exit: Don't hard kill the process on exit. This allows catching
7575
SystemExit.
7676
"""
77+
# As a common pattern around the codebase, we tend to do this instead of
78+
# using default arguments that are mutable objects (due to Python's
79+
# famously counterintuitive behavior about those): use a sentinel, then
80+
# set.
81+
stdout = stdout if stdout is not None else sys.stdout
82+
stderr = stderr if stderr is not None else sys.stderr
83+
# sys.stdout and sys.stderr might technically be None, but this fact isn't
84+
# currently enforced by the stubs (they are marked as MaybeNone (=Any)).
85+
7786
util.check_python_version("mypy")
7887
t0 = time.time()
7988
# To log stat() calls: os.stat = stat_proxy
@@ -150,11 +159,10 @@ def main(
150159
summary = formatter.format_error(
151160
n_errors, n_files, len(sources), blockers=blockers, use_color=options.color_output
152161
)
153-
stdout.write(summary + "\n")
162+
print(summary, file=stdout, flush=True)
154163
# Only notes should also output success
155164
elif not messages or n_notes == len(messages):
156-
stdout.write(formatter.format_success(len(sources), options.color_output) + "\n")
157-
stdout.flush()
165+
print(formatter.format_success(len(sources), options.color_output), file=stdout, flush=True)
158166

159167
if options.install_types and not options.non_interactive:
160168
result = install_types(formatter, options, after_run=True, non_interactive=False)
@@ -180,13 +188,14 @@ def run_build(
180188
options: Options,
181189
fscache: FileSystemCache,
182190
t0: float,
183-
stdout: TextIO,
184-
stderr: TextIO,
191+
stdout: TextIO | None = None,
192+
stderr: TextIO | None = None,
185193
) -> tuple[build.BuildResult | None, list[str], bool]:
186194
formatter = util.FancyFormatter(
187195
stdout, stderr, options.hide_error_codes, hide_success=bool(options.output)
188196
)
189-
197+
stdout = stdout if stdout is not None else sys.stdout
198+
stderr = stderr if stderr is not None else sys.stderr
190199
messages = []
191200
messages_by_file = defaultdict(list)
192201

@@ -238,14 +247,12 @@ def flush_errors(filename: str | None, new_messages: list[str], serious: bool) -
238247

239248

240249
def show_messages(
241-
messages: list[str], f: TextIO, formatter: util.FancyFormatter, options: Options
250+
messages: list[str], f: TextIO | None, formatter: util.FancyFormatter, options: Options
242251
) -> None:
243252
for msg in messages:
244253
if options.color_output:
245254
msg = formatter.colorize(msg)
246-
f.write(msg + "\n")
247-
f.flush()
248-
255+
print(msg, file=f, flush=True)
249256

250257
# Make the help output a little less jarring.
251258
class AugmentedHelpFormatter(argparse.RawDescriptionHelpFormatter):
@@ -399,7 +406,7 @@ def _print_message(self, message: str, file: SupportsWrite[str] | None = None) -
399406
if message:
400407
if file is None:
401408
file = self.stderr
402-
file.write(message)
409+
print(message, file=file)
403410

404411
# ===============
405412
# Exiting methods
@@ -465,8 +472,8 @@ def __call__(
465472
def define_options(
466473
program: str = "mypy",
467474
header: str = HEADER,
468-
stdout: TextIO | None,
469-
stderr: TextIO | None,
475+
stdout: TextIO | None = None,
476+
stderr: TextIO | None = None,
470477
server_options: bool = False,
471478
) -> tuple[CapturableArgumentParser, list[str], list[tuple[str, bool]]]:
472479
"""Define the options in the parser (by calling a bunch of methods that express/build our desired command-line flags).

mypy/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ class FancyFormatter:
592592
"""
593593

594594
def __init__(
595-
self, f_out: IO[str], f_err: IO[str], hide_error_codes: bool, hide_success: bool = False
595+
self, f_out: IO[str] | None, f_err: IO[str] | None, hide_error_codes: bool, hide_success: bool = False
596596
) -> None:
597597
self.hide_error_codes = hide_error_codes
598598
self.hide_success = hide_success
@@ -601,7 +601,7 @@ def __init__(
601601
if sys.platform not in ("linux", "darwin", "win32", "emscripten"):
602602
self.dummy_term = True
603603
return
604-
if not should_force_color() and (not f_out.isatty() or not f_err.isatty()):
604+
if (f_out is None or f_err is None) or not should_force_color() and (not f_out.isatty() or not f_err.isatty()):
605605
self.dummy_term = True
606606
return
607607
if sys.platform == "win32":

0 commit comments

Comments
 (0)