[3.13] gh-139905: Provide suggestion in error message if Generic.__init_subclass__ was not called (GH-139943) (#139956)

gh-139905: Provide suggestion in error message if `Generic.__init_subclass__` was not called (GH-139943)
(cherry picked from commit 5776d0d2e0)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-10-11 17:36:44 +02:00 committed by GitHub
parent 6a9908ee94
commit cbb415e992
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 2 deletions

View file

@ -4582,6 +4582,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.<locals>.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],