Skip to content

Commit 130ba46

Browse files
authored
Fixes raise CustomError creationg with __init__ with arguments (#11125)
Fixes #11089
1 parent 99fae38 commit 130ba46

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

mypy/checker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3424,6 +3424,10 @@ def type_check_raise(self, e: Expression, s: RaiseStmt,
34243424
expected_type.items.append(TupleType([any_type, any_type, any_type], tuple_type))
34253425
self.check_subtype(typ, expected_type, s, message_registry.INVALID_EXCEPTION)
34263426

3427+
if isinstance(typ, FunctionLike):
3428+
# https://github.com/python/mypy/issues/11089
3429+
self.expr_checker.check_call(typ, [], [], e)
3430+
34273431
def visit_try_stmt(self, s: TryStmt) -> None:
34283432
"""Type check a try statement."""
34293433
# Our enclosing frame will get the result if the try/except falls through.

test-data/unit/check-statements.test

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ class MyError(BaseException): pass
406406
[out]
407407
main:5: error: Exception must be derived from BaseException
408408

409-
[case testRaiseClassobject]
410-
import typing
409+
[case testRaiseClassObject]
411410
class A: pass
412411
class MyError(BaseException): pass
413412
def f(): pass
@@ -418,6 +417,33 @@ raise object # E: Exception must be derived from BaseException
418417
raise f # E: Exception must be derived from BaseException
419418
[builtins fixtures/exception.pyi]
420419

420+
[case testRaiseClassObjectCustomInit]
421+
class MyBaseError(BaseException):
422+
def __init__(self, required) -> None:
423+
...
424+
class MyError(Exception):
425+
def __init__(self, required1, required2) -> None:
426+
...
427+
class MyKwError(Exception):
428+
def __init__(self, *, kwonly) -> None:
429+
...
430+
class MyErrorWithDefault(Exception):
431+
def __init__(self, optional=1) -> None:
432+
...
433+
raise BaseException
434+
raise Exception
435+
raise BaseException(1)
436+
raise Exception(2)
437+
raise MyBaseError(4)
438+
raise MyError(5, 6)
439+
raise MyKwError(kwonly=7)
440+
raise MyErrorWithDefault(8)
441+
raise MyErrorWithDefault
442+
raise MyBaseError # E: Too few arguments for "MyBaseError"
443+
raise MyError # E: Too few arguments for "MyError"
444+
raise MyKwError # E: Missing named argument "kwonly" for "MyKwError"
445+
[builtins fixtures/exception.pyi]
446+
421447
[case testRaiseExceptionType]
422448
import typing
423449
x = None # type: typing.Type[BaseException]

0 commit comments

Comments
 (0)