Skip to content

Confusing behavior with raise Class #4897

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

Closed
bwo opened this issue Apr 12, 2018 · 6 comments
Closed

Confusing behavior with raise Class #4897

bwo opened this issue Apr 12, 2018 · 6 comments
Labels
bug mypy got something wrong priority-1-normal

Comments

@bwo
Copy link
Contributor

bwo commented Apr 12, 2018

from typing import Type, Mapping, TypeVar


class Foo(Exception):
    pass


class Bar(Foo):
    pass


m = {1: Bar, 2: Foo}  # Mapping[int, Type[Foo]]


def f(i):
    # type: (int) -> Type[Foo]
    return m[i]


def x():
    # type: () -> None
    reveal_type(f(1))
    raise f(1)


def y():
    # type: () -> None
    reveal_type(m[1])
    raise m[1]


# for completeness's sake
def huh(*args, **kwargs):
    # type: (*object, **object) -> Foo
    return Bar()


def z():
    # type: () -> None
    reveal_type(huh)
    raise huh

The first reveal_type shows that f(1) returns a Type[Foo]; x() does not typecheck, with the error "Exception must be derived from BaseException".

The second reveal_type shows that m[1] is def (*args: builtins.object, **kwargs: builtins.object) -> foo.Foo. y() typechecks.

The third reveal_type shows that huh is def (*args: builtins.object, **kwargs: builtins.object) -> foo.Foo. z() does not typecheck (same error as x()).

@gvanrossum
Copy link
Member

gvanrossum commented Apr 13, 2018 via email

@JelleZijlstra
Copy link
Member

Here's a really short repro:

$ python3.6 -m mypy -c 'from typing import Type; X: Type[Exception]; raise X'
<string>:1: error: Exception must be derived from BaseException

mypy -c 'raise Exception' works fine though.

mypy seems to not realize that Type[Exception] is compatible with raise.

@bwo
Copy link
Contributor Author

bwo commented Apr 13, 2018

@gvanrossum sorry, though @JelleZijlstra has basically got it.

If I have a Type[Exception] (as returned by f), I ought to be able to raise it, but instead, mypy complains.

mypy does not complain if I just write raise Bar, but it also thinks (according to reveal_type) that Bar is a function type returning a an instance of Bar. It will, however, complain if I type raise huh, even though reveal_type(huh) and reveal_type(Bar) have identical output.

@gvanrossum
Copy link
Member

Ah, this is definitely a bug. Do you want to give fixing it a try?

@emmatyping emmatyping added bug mypy got something wrong priority-1-normal labels Apr 13, 2018
@bwo
Copy link
Contributor Author

bwo commented Apr 13, 2018

Sure, but I could do with a pointer as to where to start.

@Michael0x2a
Copy link
Collaborator

@bwo -- A good starting point is often to grep for the error message -- you can then see where it's called, attach a breakpoint, etc.

To save you a little bit of work, you'll probably want to start with the type_check_raise method in mypy/checker.py.

ilevkivskyi pushed a commit that referenced this issue May 15, 2018
That is, where E is annotated as a Type, rather than being inferred as a
FunctionLike which is a type object.

This also allows `raise E` where E is Type[Any], which *seems* right but
I was not certain about.

Fixes #4897
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

No branches or pull requests

5 participants