diff --git a/mypy/message_registry.py b/mypy/message_registry.py index b5c868f3f1be..b32edc06571a 100644 --- a/mypy/message_registry.py +++ b/mypy/message_registry.py @@ -43,7 +43,9 @@ def with_additional_msg(self, info: str) -> ErrorMessage: RETURN_VALUE_EXPECTED: Final = ErrorMessage("Return value expected", codes.RETURN_VALUE) NO_RETURN_EXPECTED: Final = ErrorMessage("Return statement in function which does not return") INVALID_EXCEPTION: Final = ErrorMessage("Exception must be derived from BaseException") -INVALID_EXCEPTION_TYPE: Final = ErrorMessage("Exception type must be derived from BaseException") +INVALID_EXCEPTION_TYPE: Final = ErrorMessage( + "Exception type must be derived from BaseException (or be a tuple of exception classes)" +) INVALID_EXCEPTION_GROUP: Final = ErrorMessage( "Exception type in except* cannot derive from BaseExceptionGroup" ) diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 1bf0e4874926..d02dcdc7eb99 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -39,7 +39,7 @@ try: pass except m.Err: pass -except m.Bad: # E: Exception type must be derived from BaseException +except m.Bad: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass [file m.py] class Err(BaseException): pass @@ -53,7 +53,7 @@ try: pass except Err: pass -except Bad: # E: Exception type must be derived from BaseException +except Bad: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass [file m.py] class Err(BaseException): pass diff --git a/test-data/unit/check-python311.test b/test-data/unit/check-python311.test index be1f337ca289..5870c7e17bcc 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -34,7 +34,7 @@ except* (RuntimeError, Custom) as e: class Bad: ... try: pass -except* (RuntimeError, Bad) as e: # E: Exception type must be derived from BaseException +except* (RuntimeError, Bad) as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Any]" [builtins fixtures/exception.pyi] diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index b9551870ddfc..3cb8864f9207 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -659,9 +659,9 @@ class E2(E1): pass try: pass except (E1, E2): pass -except (E1, object): pass # E: Exception type must be derived from BaseException -except (object, E2): pass # E: Exception type must be derived from BaseException -except (E1, (E2,)): pass # E: Exception type must be derived from BaseException +except (E1, object): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes) +except (object, E2): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes) +except (E1, (E2,)): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes) except (E1, E2): pass except ((E1, E2)): pass @@ -690,7 +690,7 @@ except (E1, E2) as e1: except (E2, E1) as e2: a = e2 # type: E1 b = e2 # type: E2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E2") -except (E1, E2, int) as e3: # E: Exception type must be derived from BaseException +except (E1, E2, int) as e3: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass [builtins fixtures/exception.pyi] @@ -750,13 +750,13 @@ def nested_union(exc: Union[Type[E1], Union[Type[E2], Type[E3]]]) -> None: def error_in_union(exc: Union[Type[E1], int]) -> None: try: pass - except exc as e: # E: Exception type must be derived from BaseException + except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass def error_in_variadic(exc: Tuple[int, ...]) -> None: try: pass - except exc as e: # E: Exception type must be derived from BaseException + except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass [builtins fixtures/tuple.pyi] @@ -784,15 +784,15 @@ except E1 as e1: reveal_type(e1) # N: Revealed type is "Any" except E2 as e2: reveal_type(e2) # N: Revealed type is "__main__.E2" -except NotBaseDerived as e3: # E: Exception type must be derived from BaseException +except NotBaseDerived as e3: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass -except (NotBaseDerived, E1) as e4: # E: Exception type must be derived from BaseException +except (NotBaseDerived, E1) as e4: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass -except (NotBaseDerived, E2) as e5: # E: Exception type must be derived from BaseException +except (NotBaseDerived, E2) as e5: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass -except (NotBaseDerived, E1, E2) as e6: # E: Exception type must be derived from BaseException +except (NotBaseDerived, E1, E2) as e6: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass -except (E1, E2, NotBaseDerived) as e6: # E: Exception type must be derived from BaseException +except (E1, E2, NotBaseDerived) as e6: # E: Exception type must be derived from BaseException (or be a tuple of exception classes) pass [builtins fixtures/exception.pyi] @@ -953,8 +953,8 @@ except a as b: import typing def exc() -> BaseException: pass try: pass -except exc as e: pass # E: Exception type must be derived from BaseException -except BaseException() as b: pass # E: Exception type must be derived from BaseException +except exc as e: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes) +except BaseException() as b: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes) [builtins fixtures/exception.pyi] [case testTupleValueAsExceptionType] @@ -980,7 +980,7 @@ except exs2 as e2: exs3 = (E1, (E1_1, (E1_2,))) try: pass -except exs3 as e3: pass # E: Exception type must be derived from BaseException +except exs3 as e3: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes) [builtins fixtures/exception.pyi] [case testInvalidTupleValueAsExceptionType] @@ -991,7 +991,7 @@ class E2(E1): pass exs1 = (E1, E2, int) try: pass -except exs1 as e: pass # E: Exception type must be derived from BaseException +except exs1 as e: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes) [builtins fixtures/exception.pyi] [case testOverloadedExceptionType] @@ -1034,7 +1034,7 @@ def h(e: Type[int]): [builtins fixtures/exception.pyi] [out] main:9: note: Revealed type is "builtins.BaseException" -main:12: error: Exception type must be derived from BaseException +main:12: error: Exception type must be derived from BaseException (or be a tuple of exception classes) -- Del statement