Skip to content

Commit e025139

Browse files
KotlinIslandMorgan Bartholomew
authored andcommitted
default to color and only wrap lines if there is a terminal
1 parent 750fb76 commit e025139

File tree

10 files changed

+25
-13
lines changed

10 files changed

+25
-13
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ jobs:
109109
FORCE_COLOR: 1
110110
# Tox
111111
PY_COLORS: 1
112-
# Mypy (see https://github.com/python/mypy/issues/7771)
113-
TERM: xterm-color
114-
MYPY_FORCE_COLOR: 1
115-
MYPY_FORCE_TERMINAL_WIDTH: 200
116112
# Pytest
117113
PYTEST_ADDOPTS: --color=yes
118114
steps:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
## [Unreleased]
44
### Enhancements
55
- Show 'narrowed from' in `reveal_type` (#550)
6+
- `--color-output` is enabled by default (#531)
7+
- `--ide` will disable color-output (#531)
8+
- Output lines won't wrap if not connected to a terminal (#531)
69
### Fixes
710
- Render star args in error messages properly (#551)
811

mypy/build.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,9 @@ def load_baseline(options: Options, errors: Errors, stdout: TextIO) -> None:
11031103

11041104
from mypy import main
11051105

1106-
formatter = util.FancyFormatter(stdout, stderr, options.hide_error_codes)
1106+
formatter = util.FancyFormatter(
1107+
stdout, stderr, options.hide_error_codes, color=options.color_output
1108+
)
11071109
file = Path(options.baseline_file)
11081110

11091111
if not file.exists():

mypy/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def main(
7272
if clean_exit:
7373
options.fast_exit = False
7474

75-
formatter = util.FancyFormatter(stdout, stderr, options.hide_error_codes)
75+
formatter = util.FancyFormatter(
76+
stdout, stderr, options.hide_error_codes, color=options.color_output
77+
)
7678

7779
if options.install_types and (stdout is not sys.stdout or stderr is not sys.stderr):
7880
# Since --install-types performs user input, we want regular stdout and stderr.
@@ -1362,6 +1364,7 @@ def set_ide_flags() -> None:
13621364
"error_summary": False,
13631365
"pretty": False,
13641366
"show_error_end": True,
1367+
"color_output": False,
13651368
}.items():
13661369
setattr(options, dest, value)
13671370

mypy/test/testcmdline.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
7171
args.append("--no-default-return")
7272
if "--pretty" not in args:
7373
args.append("--no-pretty")
74+
args.append("--no-color-output")
7475
if "--show-error-code-links" not in args and "--ide" not in args:
7576
args.append("--hide-error-code-links")
7677
if "--error-summary" not in args:
@@ -90,6 +91,8 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
9091
extra_path = os.path.join(os.path.abspath(test_temp_dir), "pypath")
9192
env["PYTHONPATH"] = PREFIX
9293
env["__MYPY_UNDER_TEST__"] = "1" if based else "2"
94+
if "--pretty" in args:
95+
env["COLUMNS"] = "80"
9396
if os.path.isdir(extra_path):
9497
env["PYTHONPATH"] += os.pathsep + extra_path
9598
cwd = os.path.join(test_temp_dir, custom_cwd or "")

mypy/test/testpep561.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def test_pep561(testcase: DataDrivenTestCase) -> None:
133133
"--no-strict",
134134
"--no-pretty",
135135
"--hide-error-context",
136+
"--no-color-output",
136137
]
137138
)
138139
if python_executable != sys.executable:

mypy/test/testpythoneval.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None
8585
for s in testcase.input:
8686
file.write(f"{s}\n")
8787
mypy_cmdline.append(f"--cache-dir={cache_dir}")
88+
mypy_cmdline.append("--no-color-output")
8889
output = []
8990
# Type check the program.
9091
out, err, returncode = api.run(mypy_cmdline)

mypy/util.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,14 @@ class FancyFormatter:
522522
This currently only works on Linux and Mac.
523523
"""
524524

525-
def __init__(self, f_out: IO[str], f_err: IO[str], hide_error_codes: bool) -> None:
525+
def __init__(self, f_out: IO[str], f_err: IO[str], hide_error_codes: bool, color=True) -> None:
526526
self.hide_error_codes = hide_error_codes
527527
# Check if we are in a human-facing terminal on a supported platform.
528528
if sys.platform not in ("linux", "darwin", "win32", "emscripten"):
529529
self.dummy_term = True
530530
return
531-
if not should_force_color() and (not f_out.isatty() or not f_err.isatty()):
531+
# TODO: handle NO_COLOR and FORCE_COLOR
532+
if not color:
532533
self.dummy_term = True
533534
return
534535
if sys.platform == "win32":
@@ -652,6 +653,12 @@ def fit_in_terminal(
652653
self, messages: list[str], fixed_terminal_width: int | None = None
653654
) -> list[str]:
654655
"""Improve readability by wrapping error messages and trimming source code."""
656+
if not os.environ.get("COLUMNS"):
657+
# Only wrap if there is actually a terminal
658+
try:
659+
os.get_terminal_size(sys.__stdout__.fileno())
660+
except (AttributeError, ValueError, OSError):
661+
return messages
655662
width = fixed_terminal_width or get_terminal_width()
656663
new_messages = messages.copy()
657664
for i, error in enumerate(messages):

test-data/unit/daemon.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def foo():
282282
[case testDaemonQuickstart]
283283
$ {python} -c "print('x=1')" >foo.py
284284
$ {python} -c "print('x=1')" >bar.py
285-
$ mypy --no-nonlocal-partial-types --cache-fine-grained --follow-imports=error --no-sqlite-cache --python-version=3.11 -- foo.py bar.py
285+
$ mypy --no-nonlocal-partial-types --cache-fine-grained --follow-imports=error --no-sqlite-cache --python-version=3.11 --no-color-output -- foo.py bar.py
286286
Success: no issues found in 2 source files
287287
$ {python} -c "import shutil; shutil.copy('.mypy_cache/3.11/bar.meta.json', 'asdf.json')"
288288
-- update bar's timestamp but don't change the file

tox.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ commands = pre-commit run --all-files --show-diff-on-failure
4848

4949
[testenv:type]
5050
description = type check ourselves
51-
passenv =
52-
TERM
53-
MYPY_FORCE_COLOR
54-
MYPY_FORCE_TERMINAL_WIDTH
5551
commands =
5652
python runtests.py self
5753
# Do a no-strict compatible check, this is needed because generating a baseline from the head of upstream

0 commit comments

Comments
 (0)