Skip to content

Commit

Permalink
Update message about invalid exception type in try (#15131)
Browse files Browse the repository at this point in the history
Fixes #5349 - Updating Error Messaging for Invalid Exception Type
  • Loading branch information
ajrpeggio committed Apr 25, 2023
1 parent 97e7f3e commit e4217f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
4 changes: 3 additions & 1 deletion mypy/message_registry.py
Expand Up @@ -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"
)
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-modules.test
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-python311.test
Expand Up @@ -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]

Expand Down
32 changes: 16 additions & 16 deletions test-data/unit/check-statements.test
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e4217f2

Please sign in to comment.