Skip to content

Commit

Permalink
[unreachable-code] Fix the false positive in python 3.8 (#9753)
Browse files Browse the repository at this point in the history
Refs #9751

Co-authored-by: John Snow <[email protected]>
(cherry picked from commit d281f2f)
  • Loading branch information
Pierre-Sassoulas authored and github-actions[bot] committed Jun 28, 2024
1 parent 6c3ab77 commit df88781
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9751.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed a false positive ``unreachable-code`` when using ``typing.Any`` as return type in python
3.8, the ``typing.NoReturn`` are not taken into account anymore for python 3.8 however.

Closes #9751
4 changes: 3 additions & 1 deletion pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,9 @@ def is_terminating_func(node: nodes.Call) -> bool:
*TYPING_NEVER,
*TYPING_NORETURN,
# In Python 3.7 - 3.8, NoReturn is alias of '_SpecialForm'
"typing._SpecialForm",
# "typing._SpecialForm",
# But 'typing.Any' also inherits _SpecialForm
# See #9751
)
):
return True
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/r/regression_02/regression_9751.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
pylint 3.2.4 regression
https://github.com/pylint-dev/pylint/issues/9751
"""

# pylint: disable=missing-function-docstring

from typing import Any

def repro() -> Any:
return 5

def main():
x = repro() + 5
print(x)
18 changes: 1 addition & 17 deletions tests/functional/u/used/used_before_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# pylint: disable=consider-using-f-string, missing-function-docstring
import datetime
import sys
from typing import NoReturn
# from typing import NoReturn # uncomment when we reunite with used_before_assignment_py38.py

MSG = "hello %s" % MSG # [used-before-assignment]

Expand Down Expand Up @@ -206,19 +206,3 @@ def inner_if_continues_outer_if_has_no_other_statements():
else:
order = None
print(order)


class PlatformChecks:
"""https://github.com/pylint-dev/pylint/issues/9674"""
def skip(self, msg) -> NoReturn:
raise Exception(msg) # pylint: disable=broad-exception-raised

def print_platform_specific_command(self):
if sys.platform == "linux":
cmd = "ls"
elif sys.platform == "win32":
cmd = "dir"
else:
self.skip("only runs on Linux/Windows")

print(cmd)
26 changes: 26 additions & 0 deletions tests/functional/u/used/used_before_assignment_py38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Temporary file until we drop python 3.8
See https://github.com/pylint-dev/pylint/issues/9751
Please reunite with used_before_assignment.py at this point
"""

# pylint: disable=missing-docstring

import sys
from typing import NoReturn


class PlatformChecks:
"""https://github.com/pylint-dev/pylint/issues/9674"""
def skip(self, msg) -> NoReturn:
raise Exception(msg) # pylint: disable=broad-exception-raised

def print_platform_specific_command(self):
if sys.platform == "linux":
cmd = "ls"
elif sys.platform == "win32":
cmd = "dir"
else:
self.skip("only runs on Linux/Windows")

print(cmd)
2 changes: 2 additions & 0 deletions tests/functional/u/used/used_before_assignment_py38.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.9

0 comments on commit df88781

Please sign in to comment.