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

false unreachable error after pytest.raises block #8985

Closed
rkm opened this issue Jun 10, 2020 · 6 comments
Closed

false unreachable error after pytest.raises block #8985

rkm opened this issue Jun 10, 2020 · 6 comments

Comments

@rkm
Copy link

rkm commented Jun 10, 2020

Hi. mypy returns an unreachable error for code after a pytets.raises block:

$ cat t.py
import pytest

def testeroni() -> None:
    with pytest.raises(Exception):
        raise Exception("oh noes")
    print("hello there")

$ mypy --version
mypy 0.780

$ mypy t.py
t.py: note: In function "testeroni":
t.py:7: error: Statement is unreachable
        print("hello there")
        ^
Found 1 error in 1 file (checked 1 source file)

I'm actually not sure if this is a bug in mypy, since it might not be aware that pytest swallows the exception there. Any advice appreciated!

@JelleZijlstra
Copy link
Member

Mypy by default assumes context managers don't suppress exceptions, because that's more common in general. See https://github.com/python/typeshed/blob/master/CONTRIBUTING.md#conventions (second to last paragraph) and #7214 (comment).

To fix this issue pytest.raises should be annotated as a ContextManager that returns bool.

@emmatyping
Copy link
Collaborator

@JelleZijlstra yeah, this is unfortunate though, because for an un-annotated library (like pytest) there is no way for a user to change mypy's behavior here. Perhaps we should default to context managers that are Any can swallow exceptions?

@JelleZijlstra
Copy link
Member

@ethanhs that would also cause false positives (about missing return statements).

@emmatyping
Copy link
Collaborator

@JelleZijlstra yes but those false positives can be silenced via asserts. I think it is better to have false positives that can be disabled. Furthermore, incorrectly calling a branch dead could lead to false negatives.

@bluetech
Copy link
Contributor

pytest 6 is going to have published type annotations, hopefully (link), though we're still working on the details.

@rkm
Copy link
Author

rkm commented Jul 6, 2020

I'm happy to confirm that this is fixed with pytest-dev/pytest#7448. Thanks again for your work on this @bluetech! :)

@rkm rkm closed this as completed Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants