Skip to content

Re-add fallback for zero terminal width #9651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mypy/test/testutil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
from unittest import mock, TestCase

from mypy.util import get_terminal_width


class TestGetTerminalSize(TestCase):
def test_get_terminal_size_in_pty_defaults_to_80(self) -> None:
# when run using a pty, `os.get_terminal_size()` returns `0, 0`
ret = os.terminal_size((0, 0))
with mock.patch.object(os, 'get_terminal_size', return_value=ret):
assert get_terminal_width() == 80
5 changes: 4 additions & 1 deletion mypy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
re.compile(br'([ \t\v]*#.*(\r\n?|\n))??[ \t\v]*#.*coding[:=][ \t]*([-\w.]+)') # type: Final

DEFAULT_SOURCE_OFFSET = 4 # type: Final
DEFAULT_COLUMNS = 80 # type: Final

# At least this number of columns will be shown on each side of
# error location when printing source code snippet.
Expand Down Expand Up @@ -416,7 +417,9 @@ def split_words(msg: str) -> List[str]:

def get_terminal_width() -> int:
"""Get current terminal width if possible, otherwise return the default one."""
return int(os.getenv('MYPY_FORCE_TERMINAL_WIDTH', '0')) or shutil.get_terminal_size().columns
return (int(os.getenv('MYPY_FORCE_TERMINAL_WIDTH', '0'))
or shutil.get_terminal_size().columns
or DEFAULT_COLUMNS)


def soft_wrap(msg: str, max_len: int, first_offset: int,
Expand Down