|
15 | 15 | from typing import TYPE_CHECKING, Any, TypeVar |
16 | 16 |
|
17 | 17 | from astroid import bases, decorators, nodes, util |
| 18 | +from astroid.builder import extract_node |
18 | 19 | from astroid.const import Context |
19 | 20 | from astroid.context import InferenceContext, copy_context |
20 | 21 | from astroid.exceptions import ( |
@@ -527,11 +528,34 @@ def excepthandler_assigned_stmts( |
527 | 528 | ) -> Any: |
528 | 529 | from astroid import objects # pylint: disable=import-outside-toplevel |
529 | 530 |
|
530 | | - for assigned in node_classes.unpack_infer(self.type): |
531 | | - if isinstance(assigned, nodes.ClassDef): |
532 | | - assigned = objects.ExceptionInstance(assigned) |
| 531 | + def _generate_assigned(): |
| 532 | + for assigned in node_classes.unpack_infer(self.type): |
| 533 | + if isinstance(assigned, nodes.ClassDef): |
| 534 | + assigned = objects.ExceptionInstance(assigned) |
533 | 535 |
|
| 536 | + yield assigned |
| 537 | + |
| 538 | + if isinstance(self.parent, node_classes.TryStar): |
| 539 | + # except * handler has assigned ExceptionGroup with caught |
| 540 | + # exceptions under exceptions attribute |
| 541 | + # pylint: disable-next=stop-iteration-return |
| 542 | + eg = next( |
| 543 | + node_classes.unpack_infer( |
| 544 | + extract_node( |
| 545 | + """ |
| 546 | +from builtins import ExceptionGroup |
| 547 | +ExceptionGroup |
| 548 | +""" |
| 549 | + ) |
| 550 | + ) |
| 551 | + ) |
| 552 | + assigned = objects.ExceptionInstance(eg) |
| 553 | + assigned.instance_attrs["exceptions"] = [ |
| 554 | + nodes.List.from_elements(_generate_assigned()) |
| 555 | + ] |
534 | 556 | yield assigned |
| 557 | + else: |
| 558 | + yield from _generate_assigned() |
535 | 559 | return { |
536 | 560 | "node": self, |
537 | 561 | "unknown": node, |
|
0 commit comments