Skip to content

Commit ff79d3c

Browse files
committed
Fix type errors
1 parent 30e8e43 commit ff79d3c

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

mypy/dmypy_server.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from typing import Any, Callable, Final
2222
from typing_extensions import TypeAlias as _TypeAlias
2323

24+
from mypy import util
2425
import mypy.build
2526
import mypy.errors
2627
import mypy.main
@@ -841,7 +842,9 @@ def pretty_messages(
841842
is_tty: bool = False,
842843
terminal_width: int | None = None,
843844
) -> list[str]:
844-
use_color = self.options.color_output and is_tty
845+
use_color = (True if util.should_force_color()
846+
else self.formatter.default_colored if self.options.color_output == "auto"
847+
else bool(self.options.color_output))
845848
fit_width = self.options.pretty and is_tty
846849
if fit_width:
847850
messages = self.formatter.fit_in_terminal(

mypy/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def main(
137137
install_types(formatter, options, after_run=True, non_interactive=True)
138138
fscache.flush()
139139
print()
140-
res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
140+
res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr, use_color)
141141
show_messages(messages, stderr, formatter, options, use_color)
142142

143143
if MEM_PROFILE:
@@ -469,7 +469,8 @@ def __call__(
469469

470470
# Coupled with the usage in define_options
471471
class ColorOutputAction(argparse.Action):
472-
def __call__(self, parser, namespace, values, option_string=None):
472+
def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespace,
473+
values: str | Sequence[Any] | None, option_string: str | None = None) -> None:
473474
assert values in ("auto", None)
474475
print(f"{values=}")
475476
setattr(namespace, self.dest, True if values is None else "auto")

mypy/test/test_color_output.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from subprocess import run, PIPE
22
from functools import partial
33
from typing import Any
4+
import pytest
45

56
# TODO Would like help with this test, how do I make it runnable?
67

78
def test(expect_color: bool, *args: Any, **kwargs: Any) -> None:
8-
res = run(*args, stdout=PIPE, stderr=PIPE, **kwargs)
9+
res = run(*args, stdout=PIPE, stderr=PIPE, **kwargs) #type:ignore[call-overload]
10+
if "Found" not in res.stdout: #??
11+
pytest.fail("Command failed to complete or did not detect type error")
912
if expect_color: # Expect color control chars
1013
assert "<string>:1: error:" not in res.stdout
1114
assert "\nFound" not in res.stdout
@@ -16,15 +19,16 @@ def test(expect_color: bool, *args: Any, **kwargs: Any) -> None:
1619
colored = partial(test, True)
1720
not_colored = partial(test, False)
1821

19-
def test_color_output() -> None:
22+
@pytest.mark.parametrize("command", ["mypy", "dmypy run --"])
23+
def test_color_output(command: str) -> None:
2024
# Note: Though we don't check stderr, capturing it is useful
2125
# because it provides traceback if mypy crashes due to exception
2226
# and pytest reveals it upon failure (?)
23-
not_colored("mypy -c \"1+'a'\"")
24-
colored("mypy -c \"1+'a'\"", env={"MYPY_FORCE_COLOR": "1"})
25-
colored("mypy -c \"1+'a'\" --color-output")
26-
not_colored("mypy -c \"1+'a'\" --no-color-output")
27-
colored("mypy -c \"1+'a'\" --no-color-output", env={"MYPY_FORCE_COLOR": "1"}) #TODO
27+
not_colored(f"{command} -c \"1+'a'\"")
28+
colored(f"{command} -c \"1+'a'\"", env={"MYPY_FORCE_COLOR": "1"})
29+
colored(f"{command} -c \"1+'a'\" --color-output")
30+
not_colored(f"{command} -c \"1+'a'\" --no-color-output")
31+
colored(f"{command} -c \"1+'a'\" --no-color-output", env={"MYPY_FORCE_COLOR": "1"}) #TODO
2832

2933
# TODO: Tests in the terminal (require manual testing?)
3034
"""

0 commit comments

Comments
 (0)