Skip to content

Commit d849a3e

Browse files
[7.4.x] fix: closes #11343's [attr-defined] type errors (#11421)
Co-authored-by: Warren Markham <[email protected]>
1 parent c39bdf6 commit d849a3e

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/_pytest/compat.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,24 @@ def __get__(self, instance, owner=None):
380380

381381

382382
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.
387394
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
392401

393402

394403
# Perform exhaustiveness checking.

testing/test_parseopt.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ def test_multiple_metavar_help(self, parser: parseopt.Parser) -> None:
291291

292292
def test_argcomplete(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
293293
try:
294-
encoding = locale.getencoding() # New in Python 3.11, ignores utf-8 mode
294+
# New in Python 3.11, ignores utf-8 mode
295+
encoding = locale.getencoding() # type: ignore[attr-defined]
295296
except AttributeError:
296297
encoding = locale.getpreferredencoding(False)
297298
try:

0 commit comments

Comments
 (0)