diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index 08dfeca1b..544c7eec3 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -314,15 +314,20 @@ def safe_isclass(obj: object) -> bool: def get_user_id() -> int | None: - """Return the current user id, or None if we cannot get it reliably on the current platform.""" - # win32 does not have a getuid() function. - # On Emscripten, getuid() is a stub that always returns 0. - if sys.platform in ("win32", "emscripten"): + """Return the current user id, or None if we cannot get it reliably on + the current platform.""" + # mypy follows the version and platform checking expectation of PEP 484: + # https://mypy.readthedocs.io/en/stable/common_issues.html?highlight=platform#python-version-and-system-platform-checks + # Containment checks are too complex for mypy v1.5.0 and cause failure. + if sys.platform == "win32" or sys.platform == "emscripten": + # win32 does not have a getuid() function. + # Emscripten has a return 0 stub. return None - # getuid shouldn't fail, but cpython defines such a case. + # getuid shouldn't fail but cpython defines such a case. # Let's hope for the best. - uid = os.getuid() # type: ignore[attr-defined] - return uid if uid != -1 else None + else: + uid = os.getuid() + return uid if uid != -1 else None # Perform exhaustiveness checking.