{"id":"60ef119babeff1e2","repo":"pypa/pip","slug":"should-only-be-used-on-unix","errorCode":null,"errorMessage":"should only be used on Unix","messagePattern":"should only be used on Unix","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/platformdirs/unix.py","lineNumber":23,"sourceCode":"import os\nimport sys\nfrom configparser import ConfigParser\nfrom functools import cached_property\nfrom pathlib import Path\nfrom tempfile import gettempdir\nfrom typing import TYPE_CHECKING, NoReturn\n\nfrom ._xdg import XDGMixin\nfrom .api import PlatformDirsABC\n\nif TYPE_CHECKING:\n    from collections.abc import Iterator\n\nif sys.platform == \"win32\":\n\n    def getuid() -> NoReturn:\n        msg = \"should only be used on Unix\"\n        raise RuntimeError(msg)\n\nelse:\n    from os import getuid\n\n\nclass _UnixDefaults(PlatformDirsABC):  # ruff:ignore[too-many-public-methods]\n    \"\"\"Default directories for Unix/Linux without XDG environment variable overrides.\n\n    The XDG env var handling is in :class:`~platformdirs._xdg.XDGMixin`.\n\n    \"\"\"\n\n    @cached_property\n    def _use_site(self) -> bool:\n        return self.use_site_for_root and getuid() == 0\n\n    @property\n    def user_data_dir(self) -> str:","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/platformdirs/unix.py#L5-L41","documentation":"platformdirs defines a win32-only stub for getuid() that raises RuntimeError('should only be used on Unix') whenever sys.platform == 'win32'. The Unix module's getuid (os.getuid) does not exist on Windows, so the stub stands in to fail loudly instead of producing an AttributeError.","triggerScenarios":"Importing/using the Unix platformdirs backend (or calling getuid()) on a Windows interpreter where sys.platform == 'win32'; the conditional at import time binds the stub instead of os.getuid.","commonSituations":"Cross-platform code that imports platformdirs.unix explicitly instead of using the platform-agnostic entry point, or calling os.getuid-derived logic on Windows where no real uid exists.","solutions":["Use the top-level platformdirs API (platformdirs.user_data_dir etc.) which selects the correct backend for the current OS.","Guard Unix-only logic with 'if sys.platform != \"win32\"' before calling getuid/Unix-specific methods.","If you must detect a user id cross-platform, use a helper that falls back (e.g. getpass) on Windows."],"exampleFix":"# before\nfrom pip._vendor.platformdirs.unix import getuid\nuid = getuid()  # RuntimeError on Windows\n\n# after\nimport sys\nif sys.platform != 'win32':\n    from os import getuid\n    uid = getuid()\nelse:\n    uid = None","handlingStrategy":"type-guard","validationCode":"import sys\nif sys.platform == 'win32':\n    raise RuntimeError('getuid() is Unix-only; use a different code path')\nfrom os import getuid\nuid = getuid()","typeGuard":"def is_unix() -> bool:\n    import sys\n    return sys.platform != 'win32'","tryCatchPattern":"try:\n    uid = getuid()\nexcept RuntimeError as e:\n    if 'should only be used on Unix' in str(e):\n        uid = None\n    raise","preventionTips":["Use the platform-agnostic platformdirs entry point, not platformdirs.unix directly.","Guard OS-specific calls with sys.platform checks."],"tags":["python","platformdirs","platform","windows","unix"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}