{"record":{"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":"exception","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/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/_io/terminalwriter.py#L212-L248","documentation":"Raised by TerminalWriter._get_pygments_formatter() when PYTEST_THEME_MODE is not 'dark' or 'light', causing pygments.util.OptionError on the TerminalFormatter's bg parameter. pytest surfaces this as UsageError with the allowed values. The default is 'dark' when the variable is unset.","triggerScenarios":"Setting PYTEST_THEME_MODE=Dark (wrong case), PYTEST_THEME_MODE=black, or any value other than 'dark'/'light'.","commonSituations":"Case sensitivity mistakes; copy-paste from documentation with smart quotes; setting the value to match a terminal scheme name rather than the two allowed literals.","solutions":["Set PYTEST_THEME_MODE to exactly 'dark' or 'light'.","Unset the variable to accept the 'dark' default.","Check shell quoting and case."],"exampleFix":"# before\nexport PYTEST_THEME_MODE=Dark\n# after\nexport PYTEST_THEME_MODE=dark","handlingStrategy":"validation","validationCode":"import os\nmode = os.getenv('PYTEST_THEME_MODE', 'dark')\nif mode not in ('dark', 'light'):\n    os.environ['PYTEST_THEME_MODE'] = 'dark'","typeGuard":"def is_valid_theme_mode(v: str) -> bool:\n    return v in ('dark', 'light')","tryCatchPattern":null,"preventionTips":["Use lowercase 'dark' or 'light' only.","Validate env values at test-session setup."],"tags":["terminalwriter","pygments","environment","config"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}