Type checking unreachable code #1671
Replies: 1 comment 1 reply
-
mypy has a Your case is a little unfortunate, but we could probably get mypy to check the unreachable statement before breaking out of the block I think it'd be a good idea for |
Beta Was this translation helpful? Give feedback.
-
It seems like
mypy
andpyright
disagree on whether code that's (allegedly) unreachable should be checked.pyright
pyright-play link
Pyright seems to approach reach-ability a bit differently. Code under an
if False
orif not TYPE_CHECKING
block will be truly skipped from analysis. But some other code paths will have a different effect:mypy
mypy-play link
On the same code snippet,
mypy
is completely silent. I discovered this because I was confused that areveal_type(x)
didn't show anything.what's the "correct" behaviour?
To me the main use case for these "unreachable" checks is for runtime checking of types. Not everyone is using a type checker, and a type checker isn't guaranteed to prevent passing a value of the wrong type to the wrong function (for example by using
Any
). In that case, I think it makes sense to check the code in that block for errorsBut maybe it's going to be difficult to change in
mypy
because it uses the same "unreachable" label forif not TYPE_CHECKING
-style conditions and the "impossible"isinstance
?..Beta Was this translation helpful? Give feedback.
All reactions