Repository navigation
update --warn-unreachable to respect if the code should be type checked - #10291
Conversation
611ab55 to
51beba1
Compare
Mypy currently will warn about unreachable code regardless if the code "should" be type checked or not. Adding `reveal_type` to a function would then always produce `Any` which is unhelpful and confusing. This change updates mypy to only warn about unreachable code if the code is in a checked function.
51beba1 to
9f71fd3
Compare
|
@JukkaL I'm not sure if there's anything I should do to move this forward |
|
@JukkaL friendly ping, it looks like you (or someone else) allowed the CI to run so someone took the time to at least glance at this 😅 It would be nice to get this into the next release, which I'm sure you're busy with, since it's quite a small change, but right now we're disabling |
|
I can't speak for Jukka, but I think he's hesitant at this point to add new features to avoid destabilizing the release. Based on the test case, the behavior from this PR also seems confusing: it makes |
Ok, if it can't make it in that's fine. There's always the next release.
I could look into adjusting that, but that's actually not what the test case is doing. It's forcing the variable to be referenced which causes mypy to print the error. I just happened to use |
There was a problem hiding this comment.
This change looks good to me.
It is a little weird that we're not hitting
Line 3094 in 0964d9e
This comment has been minimized.
This comment has been minimized.
I looked at the stubs and refreshed my memory that the notes are also annotated in the stubs. So I stepped through it, and while I had expected Lines 1971 to 1975 in ce98118 Since the break happens before accepting the statement the |
|
Yup, that's correct: def test(val: bool) -> None:
assert val is True
assert val is False
reveal_type(val) |
|
@JelleZijlstra I may have misunderstood your comment initially
That is still the case after this PR. This is the tests where a user is expecting to type their code: mypy/test-data/unit/check-unreachable-code.test Lines 1321 to 1340 in 9f71fd3 And this is the case where an untyped function is expected to be ignored: mypy/test-data/unit/check-unreachable-code.test Lines 1312 to 1319 in 9f71fd3 |
|
So I think the suboptimal thing here is that if you change Fixing that seems annoying / I think this change is still probably fine. |
This PR doesn't change that based on this: Lines 1971 to 1975 in ce98118 The break already happens either way, this PR just changes if the Using your example I see: def test(val):
reveal_type(val)
assert val is True
assert val is False
reveal_type(val)by default unreachable line is missing and no warning: printing warnings, an untyped function is printing warnings this PR argues it shouldn't. The second reveal type message is still missing: checking all functions still doesn't warn about unreachable code by default: a user who wants to everything checked still gets the message that their code is unreachable: |
I'm willing to look into this (in a separate PR if it's desired), but it would be pretty specific to Lines 1971 to 1975 in ce98118 The first case would require updating all the existing visitors and making sure future editors know if they should actually be running or not, and the second case would require a 2nd full visit (I'm not sure how many passes there are against the AST already). If the possible performance impact in traversing the tree 2x or maintaining the extra code path is worth it I'm up for trying to take a stab at it, but if either of you think it's not worth it I'll hold off until someone suggests it's something important to add. I do run my code with |
|
I don't think it's worth it! I'm also not aware of any other outputs mypy produces in unchecked functions that could result in false negatives with the reachability warning gone. I'm happy to merge as is, @JelleZijlstra ? |
|
Sounds good. I'd hold off on merging though since Jukka seems to want to avoid more changes before a release. |
Description
Mypy currently will warn about unreachable code regardless if the code "should" be type checked or not. Adding
reveal_typeto a function would then always produceAnywhich is unhelpful and confusing. This change updates mypy to only warn about unreachable code if the code is in a checked function.