Open MartinYe1234 opened 5 months ago
I'm sorry, but it looks like an error has occurred due to a planning failure. Feel free to add more details to the issue description so Sweep can better address it. Alternatively, reach out to Kevin or William for help at https://discord.gg/sweep.
For bonus GPT-4 tickets, please report this bug on Discord (tracking ID: 6ee87f3aeb
).
Please look at the generated plan. If something looks wrong, please add more details to your issue.
File Path | Proposed Changes |
---|---|
astroid/nodes/node_classes.py |
Modify astroid/nodes/node_classes.py with contents: • In astroid/nodes/node_classes.py , modify the ExceptHandler class to enhance its type inference logic. Specifically, in the catch method or potentially in a new method dedicated to handling except* syntax, implement logic to infer ExceptionGroup[UnionException[TypeError]] for except* TypeError blocks. This involves checking if the syntax used is except* and then wrapping the inferred exception type(s) within an ExceptionGroup type.• Add a new method or modify an existing one to specifically handle the inference of ExceptionGroup types, ensuring it can recursively handle nested ExceptionGroup instances. This method should be able to construct a type representation that reflects the user's expectation, such as ExceptionGroup[UnionException[TypeError]] .• Ensure that any new logic added respects the existing architecture of astroid , particularly how type inference is performed and represented.• Update any relevant docstrings and comments to reflect the changes made, especially those related to the handling of except* syntax and ExceptionGroup inference. |
astroid/protocols.py |
Modify astroid/protocols.py with contents: • If necessary, modify or extend the inference rules in astroid/protocols.py to support the new ExceptionGroup inference logic introduced in astroid/nodes/node_classes.py . This may involve adding new protocols or modifying existing ones to account for the ExceptionGroup type and its potential nesting within except* blocks.• Ensure that any changes made are compatible with the existing inference protocols and do not disrupt the inference of other types or nodes within astroid .• Update documentation within the file to accurately describe any new or modified inference protocols, particularly those related to ExceptionGroup handling. |
💡 To recreate the pull request edit the issue title or description.
This is an automated message generated by Sweep AI.
32ebc6bbf8
)[!TIP] I can email you next time I complete a pull request if you set up your email here!
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
astroid/nodes/node_classes.py
✓ https://github.com/MartinYe1234/astroid/commit/a4cfd0844f7bda46ad08b057d40cd2a59f729d95 Edit
Modify astroid/nodes/node_classes.py with contents:
• Import the necessary types at the top of the file to handle `ExceptionGroup` if not already present. This might include importing `ExceptionGroup` from the `types` module if `astroid` is set to be compatible with Python 3.11 and above.
• Locate the `ExceptHandler` class definition within `node_classes.py`.
• Within the `ExceptHandler` class, modify the inference logic in the method responsible for type inference (likely `_infer` or a similar method) to check if the syntax used is `except*` and if so, infer the type as `ExceptionGroup[UnionException[SpecifiedExceptionType]]` where `SpecifiedExceptionType` is the exception type specified in the `except*` clause. This might involve creating utility functions to handle the construction of the nested `ExceptionGroup` and `UnionException` types.
• Ensure that the modified inference logic correctly handles nesting of `ExceptionGroup`s as described in the issue.
• Add or modify unit tests within the `tests/` directory to cover the new behavior, ensuring that `ExceptHandler` now correctly infers `ExceptionGroup` types for `except*` syntax. This step is crucial for validating the changes and preventing regressions in the future.
--- +++ @@ -25,7 +25,8 @@ Union, ) -from astroid import decorators, protocols, util +from astroid import bases, decorators, helpers, protocols, util +from typing import ExceptionGroup from astroid.bases import Instance, _infer_stmts from astroid.const import _EMPTY_OBJECT_MARKER, Context from astroid.context import CallContext, InferenceContext, copy_context @@ -2645,6 +2646,38 @@ return True return any(node.name in exceptions for node in self.type._get_name_nodes()) + @decorators.raise_if_nothing_inferred + def _infer(self, context: InferenceContext | None = None, **kwargs: Any + ) -> Generator[InferenceResult, None, InferenceErrorInfo | None]: + """Infer the exception type for except and except* clauses.""" + if isinstance(self.type, Tuple): + # except* clause + # Infer the exception type as ExceptionGroup[UnionException[SpecifiedExceptionType]] + # where SpecifiedExceptionType is the type specified in the except* clause + # Handle nesting of exception groups as needed + exception_types = [] + for elt in self.type.elts: + elt_types = elt.infer(context=context) + exception_types.append(next(elt_types)) + + union_except = util.Uninferable + if exception_types: + union_except = helpers.object_type( + "UnionException", + types=exception_types, + object_type=bases.Instance, + ) + + yield helpers.object_type( + "ExceptionGroup", + types=[union_except], + object_type=bases.Instance, + ) + else: + # Regular except clause + # Infer exception type normally + yield from super().infer(context=context, **kwargs) + class For( _base_nodes.MultiLineWithElseBlockNode,
astroid/nodes/node_classes.py
✓ Edit
Check astroid/nodes/node_classes.py with contents:
Ran GitHub Actions for a4cfd0844f7bda46ad08b057d40cd2a59f729d95:
I have finished reviewing the code for completeness. I did not find errors for sweep/trystarexcepthandler_incorrectly_infers_daa25
.
💡 To recreate the pull request edit the issue title or description. Something wrong? Let us know.
This is an automated message generated by Sweep AI.
Steps to reproduce
I'm reproducing this using pylint, as that's how I encountered the issue, and I found that the issue originates in astroid.
try: raise ExceptionGroup("group", [TypeError("error")]) except* TypeError as eg: for exc in eg.exceptions: print(f"Caught TypeError {exc}") print("Handled all exceptions")
Caught TypeError error Handled all exceptions
The cause of this is that:
Current behavior
ExceptHandler infers the type of eg to be TypeError.
Expected behavior
ExceptHandler infers the type of eg to be (something along the lines of): ExceptionGroup[UnionException[TypeError]] where UnionException[T] = Union[ExceptionGroup[UnionException[T]], T]
In English: The named variable in the except handler is of type ExceptionGroup, and this group contains one or more members that match the except clause, possibly nested in further ExceptionGroups.
Checklist
- [X] Modify `astroid/nodes/node_classes.py` ✓ https://github.com/MartinYe1234/astroid/commit/a4cfd0844f7bda46ad08b057d40cd2a59f729d95 [Edit](https://github.com/MartinYe1234/astroid/edit/sweep/trystarexcepthandler_incorrectly_infers_daa25/astroid/nodes/node_classes.py) - [X] Running GitHub Actions for `astroid/nodes/node_classes.py` ✓ [Edit](https://github.com/MartinYe1234/astroid/edit/sweep/trystarexcepthandler_incorrectly_infers_daa25/astroid/nodes/node_classes.py)