[7.4.x] fix: closes #11343's [attr-defined] type errors (#11421) · pytest-dev/pytest@d849a3e (original) (raw)

Original file line number Diff line number Diff line change
@@ -380,15 +380,24 @@ def __get__(self, instance, owner=None):
380 380
381 381
382 382 def get_user_id() -> int | None:
383 -"""Return the current user id, or None if we cannot get it reliably on the current platform."""
384 -# win32 does not have a getuid() function.
385 -# On Emscripten, getuid() is a stub that always returns 0.
386 -if sys.platform in ("win32", "emscripten"):
383 +"""Return the current process's real user id or None if it could not be
384 + determined.
385 +
386 + :return: The user id or None if it could not be determined.
387 + """
388 +# mypy follows the version and platform checking expectation of PEP 484:
389 +# https://mypy.readthedocs.io/en/stable/common\_issues.html?highlight=platform#python-version-and-system-platform-checks
390 +# Containment checks are too complex for mypy v1.5.0 and cause failure.
391 +if sys.platform == "win32" or sys.platform == "emscripten":
392 +# win32 does not have a getuid() function.
393 +# Emscripten has a return 0 stub.
387 394 return None
388 -# getuid shouldn't fail, but cpython defines such a case.
389 -# Let's hope for the best.
390 -uid = os.getuid()
391 -return uid if uid != -1 else None
395 +else:
396 +# On other platforms, a return value of -1 is assumed to indicate that
397 +# the current process's real user id could not be determined.
398 +ERROR = -1
399 +uid = os.getuid()
400 +return uid if uid != ERROR else None
392 401
393 402
394 403 # Perform exhaustiveness checking.