Skip to content

False exception error in except clause with a ternary conditional operator #7356

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
serhiy-storchaka opened this issue Aug 16, 2019 · 3 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-ternary-expression a if b else c

Comments

@serhiy-storchaka
Copy link
Member

import sys
try:
    f = open('.', 'rb')
except PermissionError if sys.platform == "win32" else ():
    pass

MyPy reports an error:

e.py:4: error: Exception type must be derived from BaseException

Actually except accepts either an exception type, or a tuple of exception types (including an empty tuple).

(PermissionError,) if sys.platform == "win32" else () also causes an error. But (PermissionError,) if sys.platform == "win32" else (IsADirectoryError,) is accepted by MyPy.

@serhiy-storchaka
Copy link
Member Author

See also #5128.

@emmatyping emmatyping added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Aug 16, 2019
@emmatyping
Copy link
Member

Yes, it looks like we need to special case except as well as raise as pointed out in #5128.

@hauntsaninja
Copy link
Collaborator

Fixed in #17427

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-ternary-expression a if b else c
Projects
None yet
Development

No branches or pull requests

4 participants