Skip to content

Commit

Permalink
Fall back to FORCE_COLOR environment variable if MYPY_FORCE_COLOR is …
Browse files Browse the repository at this point in the history
…not present (#13814)

Fixes #13806

This PR adds support for a FORCE_COLOR environment variable. If both
MYPY_FORCE_COLOR and FORCE_COLOR are present, mypy will continue to use
MYPY_FORCE_COLOR over FORCE_COLOR. However, if only FORCE_COLOR is set,
mypy will use that environment variable in much the same way it
currently uses MYPY_FORCE_COLOR.

MYPY_FORCE_COLOR appears to be undocumented and untested currently, so
this PR doesn't add any tests. However, @hugovk has tested this change
manually and using GitHub Actions, and reports that it appears to work
as expected.
  • Loading branch information
AlexWaygood committed Oct 4, 2022
1 parent 9033bc5 commit 0cab544
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions mypy/dmypy/client.py
Expand Up @@ -19,7 +19,7 @@
from mypy.dmypy_os import alive, kill
from mypy.dmypy_util import DEFAULT_STATUS_FILE, receive
from mypy.ipc import IPCClient, IPCException
from mypy.util import check_python_version, get_terminal_width
from mypy.util import check_python_version, get_terminal_width, should_force_color
from mypy.version import __version__

# Argument parser. Subparsers are tied to action functions by the
Expand Down Expand Up @@ -653,7 +653,7 @@ def request(
args["command"] = command
# Tell the server whether this request was initiated from a human-facing terminal,
# so that it can format the type checking output accordingly.
args["is_tty"] = sys.stdout.isatty() or int(os.getenv("MYPY_FORCE_COLOR", "0")) > 0
args["is_tty"] = sys.stdout.isatty() or should_force_color()
args["terminal_width"] = get_terminal_width()
bdata = json.dumps(args).encode("utf8")
_, name = get_status(status_file)
Expand Down
7 changes: 5 additions & 2 deletions mypy/util.py
Expand Up @@ -519,6 +519,10 @@ def parse_gray_color(cup: bytes) -> str:
return gray


def should_force_color() -> bool:
return bool(int(os.getenv("MYPY_FORCE_COLOR", os.getenv("FORCE_COLOR", "0"))))


class FancyFormatter:
"""Apply color and bold font to terminal output.
Expand All @@ -531,8 +535,7 @@ def __init__(self, f_out: IO[str], f_err: IO[str], hide_error_codes: bool) -> No
if sys.platform not in ("linux", "darwin", "win32", "emscripten"):
self.dummy_term = True
return
force_color = int(os.getenv("MYPY_FORCE_COLOR", "0"))
if not force_color and (not f_out.isatty() or not f_err.isatty()):
if not should_force_color() and (not f_out.isatty() or not f_err.isatty()):
self.dummy_term = True
return
if sys.platform == "win32":
Expand Down

0 comments on commit 0cab544

Please sign in to comment.