Skip to content

False exception error in raise statement with a ternary conditional operator #5128

Closed
@bonotake

Description

@bonotake

I faced with a strange mypy error, with such a code below:

def foo() -> None:
    message = 'test'
    raise Exception(message) if message else Exception  # -> mypy error


def bar() -> None:
    raise Exception  # ok


def baz() -> None:
    message = 'test'
    raise Exception(message)  # ok


def qux() -> None:
    message = 'test'
    raise Exception if not message else Exception(message)  # -> mypy error


if __name__ == '__main__':
    foo()

It seems legal code, but mypy produces two error messages there.

$ mypy test.py
test.py:3: error: Exception must be derived from BaseException
test.py:17: error: Exception must be derived from BaseException

Actually, this error seems to happen not only with Exception but with any other built-in exceptions.
The version of mypy is 0.600, and that of Python is 3.6.3.

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions