Skip to content

Commit

Permalink
Disable 80 col hard wrapping in mypy output (#16488)
Browse files Browse the repository at this point in the history
This fixes part of #16451 by ensuring that the mypy output isn't truncated/hard-wrapped at all. In particular: the terminal width is forcibly set to be very wide ("infinite") because soft-wrapped display in a wide terminal is nicer than it being hard-wrapped/truncated to 80 cols.

This seems to only be possible via an undocumented environment variable: `MYPY_FORCE_TERMINAL_WIDTH`: https://github.com/python/mypy/blob/03638dd670373db0b8f00cc3bcec256d09729d06/mypy/util.py#L468

[ci skip-rust]
  • Loading branch information
huonw authored Aug 19, 2022
1 parent d55493f commit 7074e84
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/python/pants/backend/python/typecheck/mypy/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ async def mypy_typecheck_partition(
env = {
"PEX_EXTRA_SYS_PATH": ":".join(all_used_source_roots),
"MYPYPATH": ":".join(all_used_source_roots),
# Force a fixed terminal width. This is effectively infinite, disabling mypy's
# builtin truncation and line wrapping. Terminals do an acceptable job of soft-wrapping
# diagnostic text and source code is typically already hard-wrapped to a limited width.
# (Unique random number to make it easier to search for the source of this setting.)
"MYPY_FORCE_TERMINAL_WIDTH": "642092230765939",
}

process = await Get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import annotations

import re
from textwrap import dedent

import pytest
Expand Down Expand Up @@ -872,3 +873,31 @@ def test_determine_python_files() -> None:
assert determine_python_files(["f.py", "f.pyi"]) == ("f.pyi",)
assert determine_python_files(["f.pyi", "f.py"]) == ("f.pyi",)
assert determine_python_files(["f.json"]) == ()


def test_colors_and_formatting(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
f"{PACKAGE}/f.py": dedent(
"""\
class incredibly_long_type_name_to_force_wrapping_if_mypy_wrapped_error_messages_12345678901234567890123456789012345678901234567890:
pass
x = incredibly_long_type_name_to_force_wrapping_if_mypy_wrapped_error_messages_12345678901234567890123456789012345678901234567890()
x.incredibly_long_attribute_name_to_force_wrapping_if_mypy_wrapped_error_messages_12345678901234567890123456789012345678901234567890
"""
),
f"{PACKAGE}/BUILD": "python_sources()",
}
)
tgt = rule_runner.get_target(Address(PACKAGE, relative_file_path="f.py"))

result = run_mypy(rule_runner, [tgt], extra_args=["--colors=true", "--mypy-args=--pretty"])

assert len(result) == 1
assert result[0].exit_code == 1
# all one line
assert re.search(
"error:.*incredibly_long_type_name.*incredibly_long_attribute_name", result[0].stdout
)
assert result[0].report == EMPTY_DIGEST

0 comments on commit 7074e84

Please sign in to comment.