astroid
astroid copied to clipboard
TryStar.ExceptHandler incorrectly infers types of caught exceptions
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.
- Create the following test script: (test.py)
try:
raise ExceptionGroup("group", [TypeError("error")])
except* TypeError as eg:
for exc in eg.exceptions:
print(f"Caught TypeError {exc}")
print("Handled all exceptions")
- Run the script
python test.py:
Caught TypeError error
Handled all exceptions
- Run
pylint -E test.py:
************* Module test
test.py:5:15: E1101: Instance of 'TypeError' has no 'exceptions' member (no-member)
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.
python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output
3.1.0