From 2d5bfa8dabd7789d9c72d8a5f7520c62c44d1f84 Mon Sep 17 00:00:00 2001 From: Warren Date: Fri, 25 Aug 2023 22:57:29 +1000 Subject: [PATCH] refactor: add self-documenting UNRELIABLE keyword --- src/_pytest/compat.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index 544c7eec3..0539886b5 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -313,9 +313,16 @@ def safe_isclass(obj: object) -> bool: return False -def get_user_id() -> int | None: +def get_user_id(*, UNRELIABLE: int = -1) -> int | None: """Return the current user id, or None if we cannot get it reliably on - the current platform.""" + the current platform. + + :param UNRELIABLE: + The platform-specific constant which indicates that the retrieved uid + is unreliable. The default value, -1, is a common unreliability + indicator on UNIX-like systems. + :return: The user id or None + """ # 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. @@ -323,11 +330,9 @@ def get_user_id() -> int | None: # 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. - # Let's hope for the best. else: uid = os.getuid() - return uid if uid != -1 else None + return uid if uid != UNRELIABLE else None # Perform exhaustiveness checking.