-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for exception groups and except* (#14020)
Ref #12840 It looks like from the point of view of type checking support is quite easy. Mypyc support however requires some actual work, so I don't include it in this PR.
- Loading branch information
1 parent
807da26
commit e8de6d1
Showing
9 changed files
with
104 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,53 @@ | ||
[case testTryStarDoesNotCrash] | ||
[case testTryStarSimple] | ||
try: | ||
pass | ||
except* Exception as e: | ||
reveal_type(e) # N: Revealed type is "builtins.Exception" | ||
reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.Exception]" | ||
[builtins fixtures/exception.pyi] | ||
|
||
[case testTryStarMultiple] | ||
try: | ||
pass | ||
except* Exception as e: | ||
reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.Exception]" | ||
except* RuntimeError as e: | ||
reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.RuntimeError]" | ||
[builtins fixtures/exception.pyi] | ||
|
||
[case testTryStarBase] | ||
try: | ||
pass | ||
except* BaseException as e: | ||
reveal_type(e) # N: Revealed type is "builtins.BaseExceptionGroup[builtins.BaseException]" | ||
[builtins fixtures/exception.pyi] | ||
|
||
[case testTryStarTuple] | ||
class Custom(Exception): ... | ||
|
||
try: | ||
pass | ||
except* (RuntimeError, Custom) as e: | ||
reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Union[builtins.RuntimeError, __main__.Custom]]" | ||
[builtins fixtures/exception.pyi] | ||
|
||
[case testTryStarInvalidType] | ||
class Bad: ... | ||
try: | ||
pass | ||
except* (RuntimeError, Bad) as e: # E: Exception type must be derived from BaseException | ||
reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Any]" | ||
[builtins fixtures/exception.pyi] | ||
|
||
[case testTryStarGroupInvalid] | ||
try: | ||
pass | ||
except* ExceptionGroup as e: # E: Exception type in except* cannot derive from BaseExceptionGroup | ||
reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Any]" | ||
[builtins fixtures/exception.pyi] | ||
|
||
[case testTryStarGroupInvalidTuple] | ||
try: | ||
pass | ||
except* (RuntimeError, ExceptionGroup) as e: # E: Exception type in except* cannot derive from BaseExceptionGroup | ||
reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Union[builtins.RuntimeError, Any]]" | ||
[builtins fixtures/exception.pyi] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
import sys | ||
from typing import Generic, TypeVar | ||
T = TypeVar('T') | ||
|
||
class object: | ||
def __init__(self): pass | ||
|
||
class type: pass | ||
class tuple(Generic[T]): pass | ||
class tuple(Generic[T]): | ||
def __ge__(self, other: object) -> bool: ... | ||
class function: pass | ||
class int: pass | ||
class str: pass | ||
class unicode: pass | ||
class bool: pass | ||
class ellipsis: pass | ||
|
||
# Note: this is a slight simplification. In Python 2, the inheritance hierarchy | ||
# is actually Exception -> StandardError -> RuntimeError -> ... | ||
class BaseException: | ||
def __init__(self, *args: object) -> None: ... | ||
class Exception(BaseException): pass | ||
class RuntimeError(Exception): pass | ||
class NotImplementedError(RuntimeError): pass | ||
|
||
if sys.version_info >= (3, 11): | ||
_BT_co = TypeVar("_BT_co", bound=BaseException, covariant=True) | ||
_T_co = TypeVar("_T_co", bound=Exception, covariant=True) | ||
class BaseExceptionGroup(BaseException, Generic[_BT_co]): ... | ||
class ExceptionGroup(BaseExceptionGroup[_T_co], Exception): ... |