{"id":"e9af25b6a063d654","repo":"pytest-dev/pytest","slug":"pytest-theme-mode-environment-variable-has-an-inva","errorCode":null,"errorMessage":"PYTEST_THEME_MODE environment variable has an invalid value: '{theme_mode}'. The allowed values are 'dark' (default) and 'light'.","messagePattern":"PYTEST_THEME_MODE environment variable has an invalid value: '(.+?)'\\. The allowed values are 'dark' \\(default\\) and 'light'\\.","errorType":"validation","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/_io/terminalwriter.py","lineNumber":230,"sourceCode":"            return DiffLexer()\n        else:\n            assert_never(lexer)\n\n    def _get_pygments_formatter(self) -> TerminalFormatter:\n        from _pytest.config.exceptions import UsageError\n\n        theme = os.getenv(\"PYTEST_THEME\")\n        theme_mode = os.getenv(\"PYTEST_THEME_MODE\", \"dark\")\n\n        try:\n            return TerminalFormatter(bg=theme_mode, style=theme)\n        except pygments.util.ClassNotFound as e:\n            raise UsageError(\n                f\"PYTEST_THEME environment variable has an invalid value: '{theme}'. \"\n                \"Hint: See available pygments styles with `pygmentize -L styles`.\"\n            ) from e\n        except pygments.util.OptionError as e:\n            raise UsageError(\n                f\"PYTEST_THEME_MODE environment variable has an invalid value: '{theme_mode}'. \"\n                \"The allowed values are 'dark' (default) and 'light'.\"\n            ) from e\n\n    def _highlight(\n        self, source: str, lexer: Literal[\"diff\", \"python\"] = \"python\"\n    ) -> str:\n        \"\"\"Highlight the given source if we have markup support.\"\"\"\n        if not source or not self.hasmarkup or not self.code_highlight:\n            return source\n\n        pygments_lexer = self._get_pygments_lexer(lexer)\n        pygments_formatter = self._get_pygments_formatter()\n\n        highlighted: str = pygments.highlight(\n            source, pygments_lexer, pygments_formatter\n        )\n        # pygments terminal formatter may add a newline when there wasn't one.","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/_io/terminalwriter.py#L212-L248","documentation":"Pytest uses PYTEST_THEME_MODE to set the background mode ('dark' or 'light') for syntax highlighting in tracebacks. If the value is anything other than 'dark' or 'light', pygments raises OptionError, which pytest wraps as a UsageError. The default is 'dark' when the variable is unset.","triggerScenarios":"Setting PYTEST_THEME_MODE to an invalid value such as 'blue', 'night', 'auto', or 'True'. The error fires in _get_pygments_formatter() when TerminalFormatter(bg=theme_mode, style=theme) rejects the bg argument.","commonSituations":"Users guessing allowed values, copying a value from a different tool's theme config (e.g., a terminal emulator), or including surrounding whitespace/quotes in the env var.","solutions":["Set PYTEST_THEME_MODE to either 'dark' or 'light'.","Unset PYTEST_THEME_MODE to use the default 'dark'.","Trim any whitespace or quotes from the env var value."],"exampleFix":"# before\nexport PYTEST_THEME_MODE=night\n\n# after\nexport PYTEST_THEME_MODE=dark","handlingStrategy":"validation","validationCode":"import os\n\ntheme_mode = os.getenv('PYTEST_THEME_MODE', 'dark')\nif theme_mode not in ('dark', 'light'):\n    raise ValueError(f\"PYTEST_THEME_MODE='{theme_mode}' is invalid. Allowed: 'dark', 'light'\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pin PYTEST_THEME_MODE to 'dark' or 'light' explicitly in CI.","Trim whitespace from env var values in shell scripts to avoid 'dark ' (with trailing space)."],"tags":["config","environment-variable","pygments","terminal"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}