ruff/crates/ty_python_semantic/resources/mdtest/exception/except_star.md
2025-05-03 19:49:15 +02:00

1.4 KiB

except*

except* is only available in Python 3.11 and later:

[environment]
python-version = "3.11"

except* with BaseException

try:
    help()
except* BaseException as e:
    reveal_type(e)  # revealed: BaseExceptionGroup[BaseException]

except* with specific exception

try:
    help()
except* OSError as e:
    # TODO: more precise would be `ExceptionGroup[OSError]` --Alex
    # (needs homogeneous tuples + generics)
    reveal_type(e)  # revealed: BaseExceptionGroup[BaseException]

except* with multiple exceptions

try:
    help()
except* (TypeError, AttributeError) as e:
    # TODO: more precise would be `ExceptionGroup[TypeError | AttributeError]` --Alex
    # (needs homogeneous tuples + generics)
    reveal_type(e)  # revealed: BaseExceptionGroup[BaseException]

except* with mix of Exceptions and BaseExceptions

try:
    help()
except* (KeyboardInterrupt, AttributeError) as e:
    # TODO: more precise would be `BaseExceptionGroup[KeyboardInterrupt | AttributeError]` --Alex
    reveal_type(e)  # revealed: BaseExceptionGroup[BaseException]

Invalid except* handlers

try:
    help()
except* 3 as e:  # error: [invalid-exception-caught]
    reveal_type(e)  # revealed: BaseExceptionGroup[BaseException]

try:
    help()
except* (AttributeError, 42) as e:  # error: [invalid-exception-caught]
    reveal_type(e)  # revealed: BaseExceptionGroup[BaseException]