Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyPy Exception Type Error Message Update #15131

Merged
merged 3 commits into from Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 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 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 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 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 a tuple of exception classes)
except (object, E2): pass # E: Exception type must be derived from BaseException (or a tuple of exception classes)
except (E1, (E2,)): pass # E: Exception type must be derived from BaseException (or 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 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 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 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 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 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 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 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 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 a tuple of exception classes)
except BaseException() as b: pass # E: Exception type must be derived from BaseException (or 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 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 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 a tuple of exception classes)


-- Del statement
Expand Down