Skip to content

Code expected to be unreachable #145

Answered by KotlinIsland
Zeckie asked this question in Q&A
Discussion options

You must be logged in to vote

Expect this feature in python 3.11(or in typing_extensions) with typing.assert_never python/typing/issues/735

If you are expecting a line to be unreachable all you have to do is annotate that line with # type: ignore[unreachable], this only applies to the first unreachable line of a branch. Note that there is an issue where this will not be reported on lines with raise statements, to work around this, just put a filler line, eg:

if False:
    message = "unreachable"  # type: ignore[unreachable]
    raise RuntimeError(message)

Also note that this is unrelated to basedmypy, all this functionality is in mypy.

There is an issue to create a dedicated function for this purpose #147 in basedmypy…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@Zeckie
Comment options

Answer selected by KotlinIsland
Comment options

You must be logged in to vote
3 replies
@KotlinIsland
Comment options

@Zeckie
Comment options

@KotlinIsland
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants