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
|
|
@ -1236,14 +1236,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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue