gh-131277: allow EnvironmentVarGuard to unset more than one environment variable at once (#131280)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This commit is contained in:
Bénédikt Tran 2025-03-16 14:09:33 +01:00 committed by GitHub
parent 9558d22ac3
commit 3185e3115c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 38 additions and 45 deletions

View file

@ -2855,8 +2855,7 @@ def no_color():
swap_attr(_colorize, "can_colorize", lambda file=None: False),
EnvironmentVarGuard() as env,
):
for var in {"FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS"}:
env.unset(var)
env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS")
env.set("NO_COLOR", "1")
yield

View file

@ -720,9 +720,10 @@ else:
class EnvironmentVarGuard(collections.abc.MutableMapping):
"""Class to help protect the environment variable properly.
"""Class to help protect the environment variable properly. Can be used as
a context manager."""
Can be used as a context manager.
"""
def __init__(self):
self._environ = os.environ
@ -756,8 +757,10 @@ class EnvironmentVarGuard(collections.abc.MutableMapping):
def set(self, envvar, value):
self[envvar] = value
def unset(self, envvar):
del self[envvar]
def unset(self, envvar, /, *envvars):
"""Unset one or more environment variables."""
for ev in (envvar, *envvars):
del self[ev]
def copy(self):
# We do what os.environ.copy() does.