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 . . .`
This commit is contained in:
parent
2d5bfa8dab
commit
f9aa628bd9
|
@ -313,14 +313,10 @@ def safe_isclass(obj: object) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_user_id(*, UNRELIABLE: int = -1) -> int | None:
|
def get_user_id() -> int | None:
|
||||||
"""Return the current user id, or None if we cannot get it reliably on
|
"""Return the current process's real user id or None if it cannot be
|
||||||
the current platform.
|
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
|
:return: The user id or None
|
||||||
"""
|
"""
|
||||||
# mypy follows the version and platform checking expectation of PEP 484:
|
# 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.
|
# Emscripten has a return 0 stub.
|
||||||
return None
|
return None
|
||||||
else:
|
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()
|
uid = os.getuid()
|
||||||
return uid if uid != UNRELIABLE else None
|
return uid if uid != UNRELIABLE else None
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue