typing: set warn_unreachable#7613
Conversation
src/_pytest/compat.py
Outdated
| if ( | ||
| p.kind is Parameter.POSITIONAL_OR_KEYWORD | ||
| or p.kind is Parameter.KEYWORD_ONLY | ||
| # TODO: Remove type ignore after https://github.com/python/typeshed/pull/4383. |
There was a problem hiding this comment.
Thanks for looking into this!
FYI the . at end of line breaks the link for me. And perhaps we could add warn_unused_ignores = True to the config so that this will be caught by CI when we upgrade?
There was a problem hiding this comment.
FYI the . at end of line breaks the link for me.
Removed.
And perhaps we could add warn_unused_ignores = True to the config so that this will be caught by CI when we upgrade?
We can't use warn_unused_ignores because there are some warnings that only happen on some Python versions, or some platforms, etc. which will then trigger the warning on others.
What I've done up to now is that when I upgrade the mypy version we use, I set it temporarily and fix what it brings up.
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.
7c04128 to
9ab14c6
Compare
|
|
||
| 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: |
There was a problem hiding this comment.
I'm curious, # type: ignore[unreachable] doesn't work here?
There was a problem hiding this comment.
It works, but then the code is considered unreachable, which I suspect might make the type checking less strict.
This makes mypy raise an error whenever it detects code which is statically unreachable, e.g.
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.