diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 371771087aaf88..e074858fe2ad99 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -3203,7 +3203,8 @@ 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. + if exc.errno in (errno.ENXIO, errno.ENOENT, errno.ENOTTY): self.skipTest(str(exc)) else: raise 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__``.