From 85ca7d8309865fbfd67b0be24922087647aeace0 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Thu, 19 Nov 2026 23:43:11 -0700 Subject: [PATCH] Make a falsy non-string environment value opt out (#46) environment_opts_out() read each variable as `(env.get(name) or "")`, so a 0, False or [] in an explicit mapping counted as unset and left reporting on, while the string "0" opted out. Only a missing (None) value now counts as unset; any other non-string opts out, as the docstring already promised. Co-Authored-By: Claude Opus 5.5 --- tests/test_trace_client.py | 8 ++++++++ trace_client/trace_client.py | 21 +++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/test_trace_client.py b/tests/test_trace_client.py index c10debe..5492a24 100644 --- a/tests/test_trace_client.py +++ b/tests/test_trace_client.py @@ -329,6 +329,14 @@ def get(self, *args, **kwargs): self.assertFalse(client.enabled) self.assertEqual("environment", client.disabled_reason) + def test_a_value_that_is_not_a_string_opts_out_even_when_it_is_empty(self): + for variable in ("TRACE_USAGE_REPORTING", "DO_NOT_TRACK"): + for value in (0, False, [], 0.0, b"", 1, True): + self.assertTrue(environment_opts_out({variable: value}), + "%s=%r is not a string, so it must opt out, not read as unset" % (variable, value)) + self.assertFalse(environment_opts_out({"TRACE_USAGE_REPORTING": None, "DO_NOT_TRACK": None}), + "a None value is a missing variable and leaves the program setting in charge") + def test_blank_or_clearly_on_environment_values_leave_the_program_setting_in_charge(self): for variable, value in (("TRACE_USAGE_REPORTING", "on"), ("TRACE_USAGE_REPORTING", ""), ("TRACE_USAGE_REPORTING", " "), ("TRACE_USAGE_REPORTING", "1"), diff --git a/trace_client/trace_client.py b/trace_client/trace_client.py index 793dbe7..002591a 100644 --- a/trace_client/trace_client.py +++ b/trace_client/trace_client.py @@ -83,19 +83,32 @@ def environment_opts_out(environ: Optional[Mapping[str, str]] = None) -> bool: "on" (``true``, ``on``, ``yes``, ``1``), or ``DO_NOT_TRACK`` set to anything but blank or a clear "no" (``0``, ``false``, ``off``, ``no``), opts out; case and surrounding space do not matter. An environment that - cannot be read -- or a value that is not a string -- opts out too: it may - hold ``DO_NOT_TRACK=1``. Never raises.""" + cannot be read -- or a value that is not a string, even ``0`` or + ``False`` -- opts out too: it may hold ``DO_NOT_TRACK=1``. Never raises.""" try: env = os.environ if environ is None else environ - usage_reporting = (env.get(ENV_TRACE_USAGE_REPORTING) or "").strip().lower() + usage_reporting = _variable(env, ENV_TRACE_USAGE_REPORTING) if usage_reporting and usage_reporting not in _ON_VALUES: return True - do_not_track = (env.get(ENV_DO_NOT_TRACK) or "").strip().lower() + do_not_track = _variable(env, ENV_DO_NOT_TRACK) return bool(do_not_track) and do_not_track not in _NEGATIVE_VALUES except Exception: # noqa: BLE001 - an environment that cannot be read counts as opting out return True +def _variable(env: Mapping[str, str], name: str) -> str: + """The variable ``name`` in ``env``, trimmed and lower-cased; blank when + unset. A value that is not a string raises, so the caller opts out: only + a missing variable may leave reporting on, never a ``0`` or ``False`` + that a blanket ``or ""`` would have read as unset.""" + value = env.get(name) + if value is None: + return "" + if not isinstance(value, str): + raise TypeError("%s is %s, not a string" % (name, type(value).__name__)) + return value.strip().lower() + + def allows_reporting(setting: object) -> bool: """Whether a program's own reporting setting, as read from its configuration, allows reporting -- for ``enabled=``. Fails closed: only