You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[possibly-undefined] False-positive with nested try block and finally #14309
try:
try:
msg=some_func()
exceptException:
raiseprint(msg) # Name "msg" may be undefined [possibly-undefined]finally:
passprint(msg) # <-- should probably be emitted here# EDIT: exception is re-raised - no error should be emitted
The first print will always safely execute since the inner Try block raises. If it throws an uncaught exception, the print will be skipped to the finally block and only the second print will execute which would then raise an error itself as msg would be undefined.
Your Environment
Mypy version used: mypy 1.0.0+dev.96ac8b3e71c743b02ea4e3c84da0248659e40f82 (compiled: no)
If some_func raises an exception, the second print will never be executed because the exception will be re-raised at the end of the finally block. So I think that means no error should be generated for the final line of the code sample.
The original example still reproduces (again, remember to enable the error code). It currently produces possibly-undefined on line 7, but not on line 12. As you mentioned above, there should be no errors, as the exception is reraised.
In the linked example from aiohttp, we do a break in the exception handler, so also should not get any errors. However, a minimal example with break doesn't produce an error, so not sure why it behaves differently on our code base.
$ mypy t.py --enable-error-code=possibly-undefinedt.py:7: error: Name "y" may be undefined [possibly-undefined]Found 1 error in 1 file (checked 1 source file)
importsystry:
ifsys.platform=="win32":
pip_exe=""reveal_type(pip_exe) # Revealed type is "builtins.str"else:
pip_exe=""reveal_type(pip_exe) # Revealed type is "builtins.str"# Name "pip_exe" may be undefined [possibly-undefined]finally:
pass
Activity
erictraut commentedon Dec 17, 2022
If
some_funcraises an exception, the secondprintwill never be executed because the exception will be re-raised at the end of thefinallyblock. So I think that means no error should be generated for the final line of the code sample.ilinum commentedon Dec 19, 2022
Yep, good find! This only occurs with nested statements because we have to do a
hackclever trick with disable_branch_skip.We can probably do something smarter here and only report errors in
finallyblocks when we're indisable_branch_skipmode.I don't really have time to work on this right now, so happy for someone else to pick it up. Otherwise, I could probably start in February.
Dreamsorcerer commentedon Jul 27, 2023
Not entirely sure if this is the same issue or a separate issue, but we are also seeing this where an
exceptthenbreaks from a loop:https://github.com/aio-libs/aiohttp/blob/63f83a0f916ee5030ee0f2dad3ba999b325c167c/aiohttp/web_protocol.py#L540-L549
erictraut commentedon Aug 14, 2023
Is there still an issue here? If so, could someone provide a self-contained minimal code sample?
Dreamsorcerer commentedon Aug 14, 2023
The original example still reproduces (again, remember to enable the error code). It currently produces possibly-undefined on line 7, but not on line 12. As you mentioned above, there should be no errors, as the exception is reraised.
In the linked example from aiohttp, we do a
breakin the exception handler, so also should not get any errors. However, a minimal example with break doesn't produce an error, so not sure why it behaves differently on our code base.andersk commentedon Aug 14, 2023
Here’s the example as a playground link (using inline configuration):
https://mypy-play.net/?mypy=latest&python=3.11&gist=c74b25457397d60fe5f063151ceb5fee
Dreamsorcerer commentedon Aug 14, 2023
Ah, the presence of the
finallyaffects the output. Here's the aiohttp version with abreak:asottile-sentry commentedon Apr 3, 2024
here's a much simpler example that appears to be an exact duplicate:
srittau commentedon Jul 12, 2024
Another instance from python/typeshed#12151 (when using
--platform win32):Fix possibly-undefined false-positive with nesting
Fix possibly-undefined false-positive with nesting (#20276)