From f8e8993472fcf7b5135682bfd7cec643a43ccb6d Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 17 Sep 2021 04:13:16 +0300 Subject: [PATCH 1/4] Fixes `raise CustomError` creationg with `__init__` with arguments --- mypy/checker.py | 4 ++++ test-data/unit/check-statements.test | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index af223506ecd3..c475748ec2fe 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -3424,6 +3424,10 @@ def type_check_raise(self, e: Expression, s: RaiseStmt, expected_type.items.append(TupleType([any_type, any_type, any_type], tuple_type)) self.check_subtype(typ, expected_type, s, message_registry.INVALID_EXCEPTION) + if isinstance(typ, FunctionLike): + # https://github.com/python/mypy/issues/11089 + self.expr_checker.check_call(typ, [], [], e) + def visit_try_stmt(self, s: TryStmt) -> None: """Type check a try statement.""" # Our enclosing frame will get the result if the try/except falls through. diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index 33e10650476d..c27b77db2762 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -406,8 +406,7 @@ class MyError(BaseException): pass [out] main:5: error: Exception must be derived from BaseException -[case testRaiseClassobject] -import typing +[case testRaiseClassObject] class A: pass class MyError(BaseException): pass def f(): pass @@ -418,6 +417,19 @@ raise object # E: Exception must be derived from BaseException raise f # E: Exception must be derived from BaseException [builtins fixtures/exception.pyi] +[case testRaiseClassObjectCustomInit] +class MyBaseError(BaseException): + def __init__(self, required) -> None: + ... +class MyError(Exception): + def __init__(self, required) -> None: + ... +raise MyBaseError(1) +raise MyError(1) +raise MyBaseError # E: Too few arguments for "MyBaseError" +raise MyError # E: Too few arguments for "MyError" +[builtins fixtures/exception.pyi] + [case testRaiseExceptionType] import typing x = None # type: typing.Type[BaseException] From 79a81d2d4a4552f137fc6578bb176f44bae06236 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 17 Sep 2021 04:20:34 +0300 Subject: [PATCH 2/4] Improves test --- test-data/unit/check-statements.test | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index c27b77db2762..20bdb0e26a2b 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -422,10 +422,14 @@ class MyBaseError(BaseException): def __init__(self, required) -> None: ... class MyError(Exception): - def __init__(self, required) -> None: + def __init__(self, required1, required2) -> None: ... -raise MyBaseError(1) -raise MyError(1) +raise BaseException +raise Exception +raise BaseException(1) +raise Exception(2) +raise MyBaseError(4) +raise MyError(5, 6) raise MyBaseError # E: Too few arguments for "MyBaseError" raise MyError # E: Too few arguments for "MyError" [builtins fixtures/exception.pyi] From a528548964300b330f252c384654d8f522b19070 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 17 Sep 2021 04:23:33 +0300 Subject: [PATCH 3/4] Improves test --- test-data/unit/check-statements.test | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index 20bdb0e26a2b..b8ae8285ccf2 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -424,14 +424,24 @@ class MyBaseError(BaseException): class MyError(Exception): def __init__(self, required1, required2) -> None: ... +class MyKwError(Exception): + def __init__(self, *, kwonly) -> None: + ... +class MyErrorWithDefault(Exception): + def __init__(self, optional=1) -> None: + ... raise BaseException raise Exception raise BaseException(1) raise Exception(2) raise MyBaseError(4) raise MyError(5, 6) +raise MyKwError(kwonly=7) +raise MyErrorWithDefault(8) +raise MyErrorWithDefault raise MyBaseError # E: Too few arguments for "MyBaseError" raise MyError # E: Too few arguments for "MyError" +raise MyKwError # E: Too few arguments for "MyKwError" [builtins fixtures/exception.pyi] [case testRaiseExceptionType] From 8b8b06e6bac6e66d08518af82ca3dac75eb1381c Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 17 Sep 2021 11:43:47 +0300 Subject: [PATCH 4/4] Fixes test --- test-data/unit/check-statements.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index b8ae8285ccf2..62d82f94a6c1 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -441,7 +441,7 @@ raise MyErrorWithDefault(8) raise MyErrorWithDefault raise MyBaseError # E: Too few arguments for "MyBaseError" raise MyError # E: Too few arguments for "MyError" -raise MyKwError # E: Too few arguments for "MyKwError" +raise MyKwError # E: Missing named argument "kwonly" for "MyKwError" [builtins fixtures/exception.pyi] [case testRaiseExceptionType]