refactor: add self-documenting UNRELIABLE keyword

This commit is contained in:
Warren 2023-08-25 22:57:29 +10:00
parent fe67feb4d3
commit 2d5bfa8dab
1 changed files with 10 additions and 5 deletions

View File

@ -313,9 +313,16 @@ def safe_isclass(obj: object) -> bool:
return False 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 """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: # 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 # 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. # 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. # win32 does not have a getuid() function.
# Emscripten has a return 0 stub. # Emscripten has a return 0 stub.
return None return None
# getuid shouldn't fail but cpython defines such a case.
# Let's hope for the best.
else: else:
uid = os.getuid() uid = os.getuid()
return uid if uid != -1 else None return uid if uid != UNRELIABLE else None
# Perform exhaustiveness checking. # Perform exhaustiveness checking.