From 897a36baddb3611865a839c6345315da689c52a1 Mon Sep 17 00:00:00 2001 From: yihong Date: Sat, 11 Oct 2025 20:32:57 +0800 Subject: [PATCH 1/3] gh-139935: fix `test_os.test_getlogin` on some platforms (#139936) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This amends 4e7e2dd043c1da85b0c157d3ed24866b77e83a4f to catch errors that `os.getlogin` can raise as specified by POSIX and Linux/glibc [1]. [1]: https://man7.org/linux/man-pages/man3/getlogin.3.html#ERRORS --------- Signed-off-by: yihong0618 Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_os/test_os.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 371771087aaf88..95b175db6fcdcb 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -3203,7 +3203,14 @@ def test_getlogin(self): try: user_name = os.getlogin() except OSError as exc: - if exc.errno in (errno.ENOTTY, errno.ENXIO): + # See https://man7.org/linux/man-pages/man3/getlogin.3.html#ERRORS. + allowed_errors = ( + # defined by POSIX + errno.EMFILE, errno.ENFILE, errno.ENXIO, errno.ERANGE, + # defined by Linux/glibc + errno.ENOENT, errno.ENOMEM, errno.ENOTTY, + ) + if exc.errno in allowed_errors: self.skipTest(str(exc)) else: raise From 2eb32add92d553b320850975442fa2e55addc377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 11 Oct 2025 16:31:34 +0200 Subject: [PATCH 2/3] gh-139935: do not skip test on real errors in `os.getlogin` (#139953) --- Lib/test/test_os/test_os.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 95b175db6fcdcb..e074858fe2ad99 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -3204,13 +3204,7 @@ def test_getlogin(self): user_name = os.getlogin() except OSError as exc: # See https://man7.org/linux/man-pages/man3/getlogin.3.html#ERRORS. - allowed_errors = ( - # defined by POSIX - errno.EMFILE, errno.ENFILE, errno.ENXIO, errno.ERANGE, - # defined by Linux/glibc - errno.ENOENT, errno.ENOMEM, errno.ENOTTY, - ) - if exc.errno in allowed_errors: + if exc.errno in (errno.ENXIO, errno.ENOENT, errno.ENOTTY): self.skipTest(str(exc)) else: raise From 5776d0d2e08f4d93dcd62d875bae8c1396a04ba4 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Date: Sat, 11 Oct 2025 16:14:29 +0100 Subject: [PATCH 3/3] gh-139905: Provide suggestion in error message if `Generic.__init_subclass__` was not called (#139943) --- Lib/test/test_typing.py | 28 +++++++++++++++++++ Lib/typing.py | 16 +++++++++-- ...-10-11-10-02-56.gh-issue-139905.UyJIR_.rst | 3 ++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-10-11-10-02-56.gh-issue-139905.UyJIR_.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 428089d88cc5ad..bc7f14f90a7cf9 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4722,6 +4722,34 @@ class D(Generic[T]): pass with self.assertRaises(TypeError): D[()] + def test_generic_init_subclass_not_called_error(self): + notes = ["Note: this exception may have been caused by " + r"'GenericTests.test_generic_init_subclass_not_called_error..Base.__init_subclass__' " + "(or the '__init_subclass__' method on a superclass) not calling 'super().__init_subclass__()'"] + + class Base: + def __init_subclass__(cls) -> None: + # Oops, I forgot super().__init_subclass__()! + pass + + with self.subTest(): + class Sub(Base, Generic[T]): + pass + + with self.assertRaises(AttributeError) as cm: + Sub[int] + + self.assertEqual(cm.exception.__notes__, notes) + + with self.subTest(): + class Sub[U](Base): + pass + + with self.assertRaises(AttributeError) as cm: + Sub[int] + + self.assertEqual(cm.exception.__notes__, notes) + def test_generic_subclass_checks(self): for typ in [list[int], List[int], tuple[int, str], Tuple[int, str], diff --git a/Lib/typing.py b/Lib/typing.py index 4311a77b8db749..03d5357d4cf51a 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1158,14 +1158,26 @@ def _generic_class_getitem(cls, args): f"Parameters to {cls.__name__}[...] must all be unique") else: # Subscripting a regular Generic subclass. - for param in cls.__parameters__: + try: + parameters = cls.__parameters__ + except AttributeError as e: + init_subclass = getattr(cls, '__init_subclass__', None) + if init_subclass not in {None, Generic.__init_subclass__}: + e.add_note( + f"Note: this exception may have been caused by " + f"{init_subclass.__qualname__!r} (or the " + f"'__init_subclass__' method on a superclass) not " + f"calling 'super().__init_subclass__()'" + ) + raise + for param in parameters: prepare = getattr(param, '__typing_prepare_subst__', None) if prepare is not None: args = prepare(cls, args) _check_generic_specialization(cls, args) new_args = [] - for param, new_arg in zip(cls.__parameters__, args): + for param, new_arg in zip(parameters, args): if isinstance(param, TypeVarTuple): new_args.extend(new_arg) else: diff --git a/Misc/NEWS.d/next/Library/2025-10-11-10-02-56.gh-issue-139905.UyJIR_.rst b/Misc/NEWS.d/next/Library/2025-10-11-10-02-56.gh-issue-139905.UyJIR_.rst new file mode 100644 index 00000000000000..a6876ca2df8299 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-11-10-02-56.gh-issue-139905.UyJIR_.rst @@ -0,0 +1,3 @@ +Add suggestion to error message for :class:`typing.Generic` subclasses when +``cls.__parameters__`` is missing due to a parent class failing to call +:meth:`super().__init_subclass__() ` in its ``__init_subclass__``.