Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport maintenance/3.2.x] Fix a false positive unreachable for NoReturn coroutine functions #9845

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9840.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed a false positive `unreachable` for `NoReturn` coroutine functions.

Closes #9840.
6 changes: 5 additions & 1 deletion pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2200,8 +2200,12 @@ def is_terminating_func(node: nodes.Call) -> bool:
inferred._proxied, astroid.UnboundMethod
):
inferred = inferred._proxied._proxied
if (
if ( # pylint: disable=too-many-boolean-expressions
isinstance(inferred, nodes.FunctionDef)
and (
not isinstance(inferred, nodes.AsyncFunctionDef)
or isinstance(node.parent, nodes.Await)
)
and isinstance(inferred.returns, nodes.Name)
and (inferred_func := safe_infer(inferred.returns))
and hasattr(inferred_func, "qname")
Expand Down
26 changes: 26 additions & 0 deletions tests/functional/u/unreachable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import signal
import sys
from typing import NoReturn

def func1():
return 1
Expand Down Expand Up @@ -79,3 +80,28 @@ def func12():
incognito_function()
var = 2 + 2 # [unreachable]
print(var)

def func13():
def inner() -> NoReturn:
while True:
pass

inner()
print("unreachable") # [unreachable]

async def func14():
async def inner() -> NoReturn:
while True:
pass

await inner()
print("unreachable") # [unreachable]

async def func15():
async def inner() -> NoReturn:
while True:
pass

coro = inner()
print("reachable")
await coro
2 changes: 2 additions & 0 deletions tests/functional/u/unreachable.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.9
22 changes: 12 additions & 10 deletions tests/functional/u/unreachable.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
unreachable:10:4:10:24:func1:Unreachable code:HIGH
unreachable:15:8:15:28:func2:Unreachable code:HIGH
unreachable:21:8:21:28:func3:Unreachable code:HIGH
unreachable:25:4:25:16:func4:Unreachable code:HIGH
unreachable:38:4:38:24:func6:Unreachable code:HIGH
unreachable:42:4:42:15:func7:Unreachable code:INFERENCE
unreachable:64:4:64:15:func9:Unreachable code:INFERENCE
unreachable:69:4:69:15:func10:Unreachable code:INFERENCE
unreachable:74:4:74:15:func11:Unreachable code:INFERENCE
unreachable:80:4:80:15:func12:Unreachable code:INFERENCE
unreachable:11:4:11:24:func1:Unreachable code:HIGH
unreachable:16:8:16:28:func2:Unreachable code:HIGH
unreachable:22:8:22:28:func3:Unreachable code:HIGH
unreachable:26:4:26:16:func4:Unreachable code:HIGH
unreachable:39:4:39:24:func6:Unreachable code:HIGH
unreachable:43:4:43:15:func7:Unreachable code:INFERENCE
unreachable:65:4:65:15:func9:Unreachable code:INFERENCE
unreachable:70:4:70:15:func10:Unreachable code:INFERENCE
unreachable:75:4:75:15:func11:Unreachable code:INFERENCE
unreachable:81:4:81:15:func12:Unreachable code:INFERENCE
unreachable:90:4:90:24:func13:Unreachable code:INFERENCE
unreachable:98:4:98:24:func14:Unreachable code:INFERENCE
Loading