| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3204,13 +3204,7 @@ def test_getlogin(self): | |||
| 3204 | 3204 | user_name = os.getlogin() | |
| 3205 | 3205 | except OSError as exc: | |
| 3206 | 3206 | # See https://man7.org/linux/man-pages/man3/getlogin.3.html#ERRORS. | |
| 3207 | - allowed_errors = ( | ||
| 3208 | - # defined by POSIX | ||
| 3209 | - errno.EMFILE, errno.ENFILE, errno.ENXIO, errno.ERANGE, | ||
| 3210 | - # defined by Linux/glibc | ||
| 3211 | - errno.ENOENT, errno.ENOMEM, errno.ENOTTY, | ||
| 3212 | - ) | ||
| 3213 | - if exc.errno in allowed_errors: | ||
| 3207 | + if exc.errno in (errno.ENXIO, errno.ENOENT, errno.ENOTTY): | ||
| 3214 | 3208 | self.skipTest(str(exc)) | |
| 3215 | 3209 | else: | |
| 3216 | 3210 | raise | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4722,6 +4722,34 @@ class D(Generic[T]): pass | |||
| 4722 | 4722 | with self.assertRaises(TypeError): | |
| 4723 | 4723 | D[()] | |
| 4724 | 4724 | ||
| 4725 | + def test_generic_init_subclass_not_called_error(self): | ||
| 4726 | + notes = ["Note: this exception may have been caused by " | ||
| 4727 | + r"'GenericTests.test_generic_init_subclass_not_called_error.<locals>.Base.__init_subclass__' " | ||
| 4728 | + "(or the '__init_subclass__' method on a superclass) not calling 'super().__init_subclass__()'"] | ||
| 4729 | + | ||
| 4730 | + class Base: | ||
| 4731 | + def __init_subclass__(cls) -> None: | ||
| 4732 | + # Oops, I forgot super().__init_subclass__()! | ||
| 4733 | + pass | ||
| 4734 | + | ||
| 4735 | + with self.subTest(): | ||
| 4736 | + class Sub(Base, Generic[T]): | ||
| 4737 | + pass | ||
| 4738 | + | ||
| 4739 | + with self.assertRaises(AttributeError) as cm: | ||
| 4740 | + Sub[int] | ||
| 4741 | + | ||
| 4742 | + self.assertEqual(cm.exception.__notes__, notes) | ||
| 4743 | + | ||
| 4744 | + with self.subTest(): | ||
| 4745 | + class Sub[U](Base): | ||
| 4746 | + pass | ||
| 4747 | + | ||
| 4748 | + with self.assertRaises(AttributeError) as cm: | ||
| 4749 | + Sub[int] | ||
| 4750 | + | ||
| 4751 | + self.assertEqual(cm.exception.__notes__, notes) | ||
| 4752 | + | ||
| 4725 | 4753 | def test_generic_subclass_checks(self): | |
| 4726 | 4754 | for typ in [list[int], List[int], | |
| 4727 | 4755 | tuple[int, str], Tuple[int, str], | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1158,14 +1158,26 @@ def _generic_class_getitem(cls, args): | |||
| 1158 | 1158 | f"Parameters to {cls.__name__}[...] must all be unique") | |
| 1159 | 1159 | else: | |
| 1160 | 1160 | # Subscripting a regular Generic subclass. | |
| 1161 | - for param in cls.__parameters__: | ||
| 1161 | + try: | ||
| 1162 | + parameters = cls.__parameters__ | ||
| 1163 | + except AttributeError as e: | ||
| 1164 | + init_subclass = getattr(cls, '__init_subclass__', None) | ||
| 1165 | + if init_subclass not in {None, Generic.__init_subclass__}: | ||
| 1166 | + e.add_note( | ||
| 1167 | + f"Note: this exception may have been caused by " | ||
| 1168 | + f"{init_subclass.__qualname__!r} (or the " | ||
| 1169 | + f"'__init_subclass__' method on a superclass) not " | ||
| 1170 | + f"calling 'super().__init_subclass__()'" | ||
| 1171 | + ) | ||
| 1172 | + raise | ||
| 1173 | + for param in parameters: | ||
| 1162 | 1174 | prepare = getattr(param, '__typing_prepare_subst__', None) | |
| 1163 | 1175 | if prepare is not None: | |
| 1164 | 1176 | args = prepare(cls, args) | |
| 1165 | 1177 | _check_generic_specialization(cls, args) | |
| 1166 | 1178 | ||
| 1167 | 1179 | new_args = [] | |
| 1168 | - for param, new_arg in zip(cls.__parameters__, args): | ||
| 1180 | + for param, new_arg in zip(parameters, args): | ||
| 1169 | 1181 | if isinstance(param, TypeVarTuple): | |
| 1170 | 1182 | new_args.extend(new_arg) | |
| 1171 | 1183 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Add suggestion to error message for :class:`typing.Generic` subclasses when | ||
| 2 | + ``cls.__parameters__`` is missing due to a parent class failing to call | ||
| 3 | + :meth:`super().__init_subclass__() <object.__init_subclass__>` in its ``__init_subclass__``. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments