From 6da94307e4d876ff2cb10bf8f576a906cd2c4d64 Mon Sep 17 00:00:00 2001 From: tbszz <17814612130@163.com> Date: Mon, 2 Nov 2026 22:47:20 +0800 Subject: [PATCH] Fix --quiet not suppressing warnings when stdout is redirected Environment.log_error() gated the quiet threshold check on stdout_isatty, so when stdout was redirected (e.g. piping output in a script) every warning and info message bypassed the devnull'd stderr and was printed to the original stderr even with -q/-qq, contradicting the documented behavior of --quiet ('Provide twice to suppress warnings as well.'). Drop the stdout_isatty condition: with the appropriate quiet level, messages now go through self.stderr (devnull under --quiet) regardless of whether stdout is a TTY. Single -q still shows warnings, errors are never hidden (their threshold is infinite), and the not-quiet path is unchanged. Also flips test_quiet_quiet_with_check_status_non_zero_pipe, which pinned the buggy behavior, to assert documented suppression. Fixes #1978 --- CHANGELOG.md | 4 ++++ httpie/context.py | 2 +- tests/test_output.py | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0497ac3508..3b76339b26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ This document records all notable changes to [HTTPie](https://httpie.io). This project adheres to [Semantic Versioning](https://semver.org/). +## Unreleased + +- Fix `--quiet` (and `-qq`) not suppressing warnings/errors when stdout is redirected. ([#1978](https://github.com/httpie/httpie/issues/1978)) + ## [3.2.4](https://github.com/httpie/cli/compare/3.2.3...3.2.4) (2024-11-01) - Fix default certs loading and unpin `requests`. ([#1596](https://github.com/httpie/cli/issues/1596)) diff --git a/httpie/context.py b/httpie/context.py index 2a54f46916..fd7e7c6ecc 100644 --- a/httpie/context.py +++ b/httpie/context.py @@ -168,7 +168,7 @@ def as_silent(self) -> Iterator[None]: self.stderr = original_stderr def log_error(self, msg: str, level: LogLevel = LogLevel.ERROR) -> None: - if self.stdout_isatty and self.quiet >= LOG_LEVEL_DISPLAY_THRESHOLDS[level]: + if self.quiet >= LOG_LEVEL_DISPLAY_THRESHOLDS[level]: stderr = self.stderr # Not directly /dev/null, since stderr might be mocked else: stderr = self._orig_stderr diff --git a/tests/test_output.py b/tests/test_output.py index 2242177dbc..9f4eda6519 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -89,7 +89,9 @@ def test_quiet_quiet_with_check_status_non_zero_pipe(self, httpbin): tolerate_error_exit_status=True, env=MockEnvironment(stdout_isatty=False) ) - assert 'http: warning: HTTP 500' in r.stderr + # Warnings are suppressed with -qq regardless of whether stdout + # is a TTY, as documented ("Provide twice to suppress warnings as well"). + assert not r.stderr @mock.patch('httpie.core.program') @pytest.mark.parametrize('flags, expected_warnings', [