MCPcopy
hub / github.com/pytest-dev/pytest / setenv

Method setenv

src/_pytest/monkeypatch.py:308–326  ·  view source on GitHub ↗

Set environment variable ``name`` to ``value``. If ``prepend`` is a character, read the current environment variable value and prepend the ``value`` adjoined with the ``prepend`` character.

(self, name: str, value: str, prepend: str | None = None)

Source from the content-addressed store, hash-verified

306 del dic[name] # type: ignore[attr-defined]
307
308 def setenv(self, name: str, value: str, prepend: str | None = None) -> None:
309 """Set environment variable ``name`` to ``value``.
310
311 If ``prepend`` is a character, read the current environment variable
312 value and prepend the ``value`` adjoined with the ``prepend``
313 character.
314 """
315 if not isinstance(value, str):
316 warnings.warn( # type: ignore[unreachable]
317 PytestWarning(
318 f"Value of environment variable {name} type should be str, but got "
319 f"{value!r} (type: {type(value).__name__}); converted to str implicitly"
320 ),
321 stacklevel=2,
322 )
323 value = str(value)
324 if prepend and name in os.environ:
325 value = value + prepend + os.environ[name]
326 self.setitem(os.environ, name, value)
327
328 def delenv(self, name: str, raising: bool = True) -> None:
329 """Delete ``name`` from the environment.

Calls 3

setitemMethod · 0.95
PytestWarningClass · 0.90
warnMethod · 0.45