mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[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:
parent
6a9908ee94
commit
cbb415e992
3 changed files with 45 additions and 2 deletions
|
|
@ -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],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue