From e30fc0d9e7a0755976b872f1f734674365732468 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 4 Oct 2022 12:11:29 -0700 Subject: [PATCH 1/2] Fall back to FORCE_COLOR environment variable if MYPY_FORCE_COLOR is not present --- mypy/dmypy/client.py | 4 ++-- mypy/util.py | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/mypy/dmypy/client.py b/mypy/dmypy/client.py index 25951befccda..f76295628735 100644 --- a/mypy/dmypy/client.py +++ b/mypy/dmypy/client.py @@ -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_force_color_env_variable, get_terminal_width from mypy.version import __version__ # Argument parser. Subparsers are tied to action functions by the @@ -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 get_force_color_env_variable() > 0 args["terminal_width"] = get_terminal_width() bdata = json.dumps(args).encode("utf8") _, name = get_status(status_file) diff --git a/mypy/util.py b/mypy/util.py index d889d3d9340f..09e8d786b3f9 100644 --- a/mypy/util.py +++ b/mypy/util.py @@ -519,6 +519,10 @@ def parse_gray_color(cup: bytes) -> str: return gray +def get_force_color_env_variable() -> int: + return int(os.getenv("MYPY_FORCE_COLOR", os.getenv("FORCE_COLOR", "0"))) + + class FancyFormatter: """Apply color and bold font to terminal output. @@ -531,7 +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")) + force_color = get_force_color_env_variable() if not force_color and (not f_out.isatty() or not f_err.isatty()): self.dummy_term = True return From 5a81b029d31e37d5afdca9e57601d77ab5d7aae3 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 4 Oct 2022 14:00:45 -0700 Subject: [PATCH 2/2] Address verbal review: make function return a `bool` --- mypy/dmypy/client.py | 4 ++-- mypy/util.py | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/mypy/dmypy/client.py b/mypy/dmypy/client.py index f76295628735..efa1b5f01288 100644 --- a/mypy/dmypy/client.py +++ b/mypy/dmypy/client.py @@ -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_force_color_env_variable, 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 @@ -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 get_force_color_env_variable() > 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) diff --git a/mypy/util.py b/mypy/util.py index 09e8d786b3f9..e836aefb3c7e 100644 --- a/mypy/util.py +++ b/mypy/util.py @@ -519,8 +519,8 @@ def parse_gray_color(cup: bytes) -> str: return gray -def get_force_color_env_variable() -> int: - return int(os.getenv("MYPY_FORCE_COLOR", os.getenv("FORCE_COLOR", "0"))) +def should_force_color() -> bool: + return bool(int(os.getenv("MYPY_FORCE_COLOR", os.getenv("FORCE_COLOR", "0")))) class FancyFormatter: @@ -535,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 = get_force_color_env_variable() - 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":