refactor: replace type:ignore with platform check

This commit is contained in:
Warren 2023-08-25 09:26:41 +10:00
parent 2a91c63318
commit 15ebcf55d1
1 changed files with 12 additions and 7 deletions

View File

@ -314,14 +314,19 @@ 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."""
"""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.
# On Emscripten, getuid() is a stub that always returns 0.
if sys.platform in ("win32", "emscripten"):
# 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]
else:
uid = os.getuid()
return uid if uid != -1 else None