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] False positive unreachable-code when using typing.Any as return in python 3.8 #9755

Merged
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
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
Loading