typing: set warn_unreachable

This makes mypy raise an error whenever it detects code which is
statically unreachable, e.g.

    x: int
    if isinstance(x, str):
        ... # Statement is unreachable  [unreachable]

This is really neat and finds quite a few logic and typing bugs.

Sometimes the code is intentionally unreachable in terms of types, e.g.
raising TypeError when a function is given an argument with a wrong
type. In these cases a `type: ignore[unreachable]` is needed, but I
think it's a nice code hint.
This commit is contained in:
Ran Benita
2020-08-03 19:15:21 +03:00
parent 0dd5e169d0
commit 9ab14c6d9c
24 changed files with 60 additions and 46 deletions
+1 -1
View File
@@ -238,7 +238,7 @@ def test_getline_finally() -> None:
c(1) # type: ignore
finally:
if teardown:
teardown()
teardown() # type: ignore[unreachable]
source = excinfo.traceback[-1].statement
assert str(source).strip() == "c(1) # type: ignore"
+3 -3
View File
@@ -381,7 +381,7 @@ class TestAssertionRewrite:
)
def f7() -> None:
assert False or x()
assert False or x() # type: ignore[unreachable]
assert (
getmsg(f7, {"x": x})
@@ -416,7 +416,7 @@ class TestAssertionRewrite:
def test_short_circuit_evaluation(self) -> None:
def f1() -> None:
assert True or explode # type: ignore[name-defined] # noqa: F821
assert True or explode # type: ignore[name-defined,unreachable] # noqa: F821
getmsg(f1, must_pass=True)
@@ -471,7 +471,7 @@ class TestAssertionRewrite:
assert getmsg(f1) == "assert ((3 % 2) and False)"
def f2() -> None:
assert False or 4 % 2
assert False or 4 % 2 # type: ignore[unreachable]
assert getmsg(f2) == "assert (False or (4 % 2))"
+1 -1
View File
@@ -360,7 +360,7 @@ def test_issue156_undo_staticmethod(Sample: "Type[Sample]") -> None:
monkeypatch.setattr(Sample, "hello", None)
assert Sample.hello is None
monkeypatch.undo()
monkeypatch.undo() # type: ignore[unreachable]
assert Sample.hello()
+3 -1
View File
@@ -23,7 +23,9 @@ def test_make_hook_recorder(testdir) -> None:
recorder = testdir.make_hook_recorder(item.config.pluginmanager)
assert not recorder.getfailures()
pytest.xfail("internal reportrecorder tests need refactoring")
# (The silly condition is to fool mypy that the code below this is reachable)
if 1 + 1 == 2:
pytest.xfail("internal reportrecorder tests need refactoring")
class rep:
excinfo = None