Skip to content

Commit

Permalink
Fix a false positive unreachable for NoReturn coroutine functions (
Browse files Browse the repository at this point in the history
…#9844) (#9845)

(cherry picked from commit 83ade13)

Co-authored-by: Tomasz Michalski <tomasz.michalski@rtbhouse.com>
  • Loading branch information
github-actions[bot] and tomasz-michalski authored Jul 31, 2024
1 parent da19566 commit 7d1626c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
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

0 comments on commit 7d1626c

Please sign in to comment.