Skip to content

Commit 0cab544

Browse files
authored
Fall back to FORCE_COLOR environment variable if MYPY_FORCE_COLOR is 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.
1 parent 9033bc5 commit 0cab544

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

mypy/dmypy/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from mypy.dmypy_os import alive, kill
2020
from mypy.dmypy_util import DEFAULT_STATUS_FILE, receive
2121
from mypy.ipc import IPCClient, IPCException
22-
from mypy.util import check_python_version, get_terminal_width
22+
from mypy.util import check_python_version, get_terminal_width, should_force_color
2323
from mypy.version import __version__
2424

2525
# Argument parser. Subparsers are tied to action functions by the
@@ -653,7 +653,7 @@ def request(
653653
args["command"] = command
654654
# Tell the server whether this request was initiated from a human-facing terminal,
655655
# so that it can format the type checking output accordingly.
656-
args["is_tty"] = sys.stdout.isatty() or int(os.getenv("MYPY_FORCE_COLOR", "0")) > 0
656+
args["is_tty"] = sys.stdout.isatty() or should_force_color()
657657
args["terminal_width"] = get_terminal_width()
658658
bdata = json.dumps(args).encode("utf8")
659659
_, name = get_status(status_file)

mypy/util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ def parse_gray_color(cup: bytes) -> str:
519519
return gray
520520

521521

522+
def should_force_color() -> bool:
523+
return bool(int(os.getenv("MYPY_FORCE_COLOR", os.getenv("FORCE_COLOR", "0"))))
524+
525+
522526
class FancyFormatter:
523527
"""Apply color and bold font to terminal output.
524528
@@ -531,8 +535,7 @@ def __init__(self, f_out: IO[str], f_err: IO[str], hide_error_codes: bool) -> No
531535
if sys.platform not in ("linux", "darwin", "win32", "emscripten"):
532536
self.dummy_term = True
533537
return
534-
force_color = int(os.getenv("MYPY_FORCE_COLOR", "0"))
535-
if not force_color and (not f_out.isatty() or not f_err.isatty()):
538+
if not should_force_color() and (not f_out.isatty() or not f_err.isatty()):
536539
self.dummy_term = True
537540
return
538541
if sys.platform == "win32":

0 commit comments

Comments
 (0)