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

except handler type error should admit the possibility of tuples #5349

Closed
benjaminp opened this issue Jul 13, 2018 · 2 comments · Fixed by #15131
Closed

except handler type error should admit the possibility of tuples #5349

benjaminp opened this issue Jul 13, 2018 · 2 comments · Fixed by #15131

Comments

@benjaminp
Copy link

$ mypy --version
mypy 0.610
$ cat exception.py
def f() -> None:
    excs = [IOError, LookupError]
    try:
        pass
    except excs:
        pass
$ mypy exception.py 
exception.py:5: error: Exception type must be derived from BaseException

While it's true that this code is incorrect, I probably want to fix it by writing excs = (IOError, LookupError) rather than somehow making my list a subclass of BaseException.

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 13, 2018

The error message is confusing in this case -- mypy accepts tuples already. One way to fix this would be to check if the exception is not a type object, and in that case change the message to something like this:

exception.py:5: error: Exception type must be a class derived from BaseException (or a tuple of exception classes)

@bhrutledge
Copy link

I was able to avoid this error by making excs a tuple, instead of a list:

def f() -> None:
    excs = (IOError, LookupError)
    try:
        pass
    except excs:
        pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants