Skip to content

Commit e4217f2

Browse files
authored
Update message about invalid exception type in try (#15131)
Fixes #5349 - Updating Error Messaging for Invalid Exception Type
1 parent 97e7f3e commit e4217f2

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

mypy/message_registry.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
4343
RETURN_VALUE_EXPECTED: Final = ErrorMessage("Return value expected", codes.RETURN_VALUE)
4444
NO_RETURN_EXPECTED: Final = ErrorMessage("Return statement in function which does not return")
4545
INVALID_EXCEPTION: Final = ErrorMessage("Exception must be derived from BaseException")
46-
INVALID_EXCEPTION_TYPE: Final = ErrorMessage("Exception type must be derived from BaseException")
46+
INVALID_EXCEPTION_TYPE: Final = ErrorMessage(
47+
"Exception type must be derived from BaseException (or be a tuple of exception classes)"
48+
)
4749
INVALID_EXCEPTION_GROUP: Final = ErrorMessage(
4850
"Exception type in except* cannot derive from BaseExceptionGroup"
4951
)

test-data/unit/check-modules.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ try:
3939
pass
4040
except m.Err:
4141
pass
42-
except m.Bad: # E: Exception type must be derived from BaseException
42+
except m.Bad: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
4343
pass
4444
[file m.py]
4545
class Err(BaseException): pass
@@ -53,7 +53,7 @@ try:
5353
pass
5454
except Err:
5555
pass
56-
except Bad: # E: Exception type must be derived from BaseException
56+
except Bad: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
5757
pass
5858
[file m.py]
5959
class Err(BaseException): pass

test-data/unit/check-python311.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ except* (RuntimeError, Custom) as e:
3434
class Bad: ...
3535
try:
3636
pass
37-
except* (RuntimeError, Bad) as e: # E: Exception type must be derived from BaseException
37+
except* (RuntimeError, Bad) as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
3838
reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Any]"
3939
[builtins fixtures/exception.pyi]
4040

test-data/unit/check-statements.test

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,9 @@ class E2(E1): pass
659659
try:
660660
pass
661661
except (E1, E2): pass
662-
except (E1, object): pass # E: Exception type must be derived from BaseException
663-
except (object, E2): pass # E: Exception type must be derived from BaseException
664-
except (E1, (E2,)): pass # E: Exception type must be derived from BaseException
662+
except (E1, object): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
663+
except (object, E2): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
664+
except (E1, (E2,)): pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
665665

666666
except (E1, E2): pass
667667
except ((E1, E2)): pass
@@ -690,7 +690,7 @@ except (E1, E2) as e1:
690690
except (E2, E1) as e2:
691691
a = e2 # type: E1
692692
b = e2 # type: E2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E2")
693-
except (E1, E2, int) as e3: # E: Exception type must be derived from BaseException
693+
except (E1, E2, int) as e3: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
694694
pass
695695
[builtins fixtures/exception.pyi]
696696

@@ -750,13 +750,13 @@ def nested_union(exc: Union[Type[E1], Union[Type[E2], Type[E3]]]) -> None:
750750
def error_in_union(exc: Union[Type[E1], int]) -> None:
751751
try:
752752
pass
753-
except exc as e: # E: Exception type must be derived from BaseException
753+
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
754754
pass
755755

756756
def error_in_variadic(exc: Tuple[int, ...]) -> None:
757757
try:
758758
pass
759-
except exc as e: # E: Exception type must be derived from BaseException
759+
except exc as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
760760
pass
761761

762762
[builtins fixtures/tuple.pyi]
@@ -784,15 +784,15 @@ except E1 as e1:
784784
reveal_type(e1) # N: Revealed type is "Any"
785785
except E2 as e2:
786786
reveal_type(e2) # N: Revealed type is "__main__.E2"
787-
except NotBaseDerived as e3: # E: Exception type must be derived from BaseException
787+
except NotBaseDerived as e3: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
788788
pass
789-
except (NotBaseDerived, E1) as e4: # E: Exception type must be derived from BaseException
789+
except (NotBaseDerived, E1) as e4: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
790790
pass
791-
except (NotBaseDerived, E2) as e5: # E: Exception type must be derived from BaseException
791+
except (NotBaseDerived, E2) as e5: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
792792
pass
793-
except (NotBaseDerived, E1, E2) as e6: # E: Exception type must be derived from BaseException
793+
except (NotBaseDerived, E1, E2) as e6: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
794794
pass
795-
except (E1, E2, NotBaseDerived) as e6: # E: Exception type must be derived from BaseException
795+
except (E1, E2, NotBaseDerived) as e6: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
796796
pass
797797
[builtins fixtures/exception.pyi]
798798

@@ -953,8 +953,8 @@ except a as b:
953953
import typing
954954
def exc() -> BaseException: pass
955955
try: pass
956-
except exc as e: pass # E: Exception type must be derived from BaseException
957-
except BaseException() as b: pass # E: Exception type must be derived from BaseException
956+
except exc as e: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
957+
except BaseException() as b: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
958958
[builtins fixtures/exception.pyi]
959959

960960
[case testTupleValueAsExceptionType]
@@ -980,7 +980,7 @@ except exs2 as e2:
980980

981981
exs3 = (E1, (E1_1, (E1_2,)))
982982
try: pass
983-
except exs3 as e3: pass # E: Exception type must be derived from BaseException
983+
except exs3 as e3: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
984984
[builtins fixtures/exception.pyi]
985985

986986
[case testInvalidTupleValueAsExceptionType]
@@ -991,7 +991,7 @@ class E2(E1): pass
991991

992992
exs1 = (E1, E2, int)
993993
try: pass
994-
except exs1 as e: pass # E: Exception type must be derived from BaseException
994+
except exs1 as e: pass # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
995995
[builtins fixtures/exception.pyi]
996996

997997
[case testOverloadedExceptionType]
@@ -1034,7 +1034,7 @@ def h(e: Type[int]):
10341034
[builtins fixtures/exception.pyi]
10351035
[out]
10361036
main:9: note: Revealed type is "builtins.BaseException"
1037-
main:12: error: Exception type must be derived from BaseException
1037+
main:12: error: Exception type must be derived from BaseException (or be a tuple of exception classes)
10381038

10391039

10401040
-- Del statement

0 commit comments

Comments
 (0)