From f9aa628bd9daef4bb84efb2226793a8a1649d2f6 Mon Sep 17 00:00:00 2001 From: Warren Date: Sat, 26 Aug 2023 14:59:53 +1000 Subject: [PATCH] refactor: self-document error flag with a constant The previous implementation required the caller to have knowledge of its platform's `getuid` error code. Although this had the benefit of replacing a hard-coded error value with a descriptive variable, the attempt at future proofing `get_user_id` was flagged as a poor design decision by Nicoddemus: to future proof the function, `get_user_id` should determine the error flag internally. However, future-proofing was not the reason the issue was opened. The issue was opened to resolve an `[attr-defined]` error on Windows platforms caused by *mypy*'s handling of code reachability through platform checks. Over the course of the open issue, additional housekeeping tasks were also undertaken: - adding a comment that links to an explanation of *mypy*'s platform and version checking requirements - adding a variable name and comment to clarify the reason for the conditional return `return uid if uid . . .` --- src/_pytest/compat.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index 0539886b5..d10f725e9 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -313,14 +313,10 @@ def safe_isclass(obj: object) -> bool: return False -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. +def get_user_id() -> int | None: + """Return the current process's real user id or None if it cannot be + retrieved reliably. - :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: @@ -331,6 +327,9 @@ def get_user_id(*, UNRELIABLE: int = -1) -> int | None: # Emscripten has a return 0 stub. return None else: + # On other platforms, a return value of -1 is assumed to indicate that + # the current process's real user id cannot be retrieved reliably. + UNRELIABLE = -1 uid = os.getuid() return uid if uid != UNRELIABLE else None