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

Emit using-constant-test when testing truth value of a call returning a generator #6911

Merged
merged 4 commits into from
Jun 11, 2022
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/2/2.15/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ False negatives fixed

Closes #6648

* Emit ``using-constant-test`` when testing the truth value of a call that only returns generators.

Closes #6909


Other bug fixes
===============
Expand Down
24 changes: 21 additions & 3 deletions pylint/checkers/base/basic_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from pylint import utils as lint_utils
from pylint.checkers import BaseChecker, utils
from pylint.interfaces import HIGH
from pylint.interfaces import HIGH, INFERENCE
from pylint.reporters.ureports import nodes as reporter_nodes
from pylint.utils import LinterStats

Expand Down Expand Up @@ -317,6 +317,22 @@ def _check_using_constant_test(
emit = isinstance(test, (nodes.Const,) + structs + const_nodes)
if not isinstance(test, except_nodes):
inferred = utils.safe_infer(test)
# Emit if calling a function that only returns GeneratorExp (always tests True)
elif isinstance(test, nodes.Call):
inferred_call = utils.safe_infer(test.func)
if isinstance(inferred_call, nodes.FunctionDef):
# Can't use all(x) or not any(not x) for this condition, because it
# will return True for empty generators, which is not what we want.
all_returns_were_generator = None
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
for return_node in inferred_call._get_return_nodes_skip_functions():
if not isinstance(return_node.value, nodes.GeneratorExp):
all_returns_were_generator = False
break
all_returns_were_generator = True
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved
if all_returns_were_generator:
self.add_message(
"using-constant-test", node=node, confidence=INFERENCE
)

if emit:
self.add_message("using-constant-test", node=test)
Expand All @@ -336,12 +352,14 @@ def _check_using_constant_test(
for inf_call in call_inferred:
if inf_call != astroid.Uninferable:
self.add_message(
"missing-parentheses-for-call-in-test", node=test
"missing-parentheses-for-call-in-test",
node=test,
confidence=INFERENCE,
)
break
except astroid.InferenceError:
pass
self.add_message("using-constant-test", node=test)
self.add_message("using-constant-test", node=test, confidence=INFERENCE)

def visit_module(self, _: nodes.Module) -> None:
"""Check module name, docstring and required arguments."""
Expand Down
30 changes: 15 additions & 15 deletions tests/functional/m/missing/missing_parentheses_for_call_in_test.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
missing-parentheses-for-call-in-test:29:3:29:16::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:35:3:35:19::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:43:3:43:23::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:51:5:51:25::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:56:3:56:14::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:64:3:64:17::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:70:17:70:30::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:72:23:72:34::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:73:24:73:38::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:75:26:75:39::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:76:26:76:37::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:79:26:79:40::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:80:26:80:42::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:85:3:85:26::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:91:3:91:20::Using a conditional statement with potentially wrong function or method call due to missing parentheses:UNDEFINED
missing-parentheses-for-call-in-test:29:3:29:16::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:35:3:35:19::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:43:3:43:23::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:51:5:51:25::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:56:3:56:14::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:64:3:64:17::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:70:17:70:30::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:72:23:72:34::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:73:24:73:38::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:75:26:75:39::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:76:26:76:37::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:79:26:79:40::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:80:26:80:42::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:85:3:85:26::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
missing-parentheses-for-call-in-test:91:3:91:20::Using a conditional statement with potentially wrong function or method call due to missing parentheses:INFERENCE
16 changes: 16 additions & 0 deletions tests/functional/u/using_constant_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,19 @@ def test_good_comprehension_checks():
{data for data in range(100) if abs(data)}
{data: 1 for data in range(100) if data}
{data: 1 for data in range(100)}


# Calls to functions returning generator expressions are always truthy
def get_generator():
return (x for x in range(0))

if get_generator(): # [using-constant-test]
pass

def maybe_get_generator(arg):
if arg:
return (x for x in range(0))
return None

if maybe_get_generator(None):
pass
21 changes: 11 additions & 10 deletions tests/functional/u/using_constant_test.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using-constant-test:22:3:22:14::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:22:3:22:14::Using a conditional statement with a constant value:INFERENCE
using-constant-test:26:3:26:31::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:29:3:29:15:<lambda>:Using a conditional statement with a constant value:UNDEFINED
using-constant-test:32:3:32:11::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:35:3:35:8::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:32:3:32:11::Using a conditional statement with a constant value:INFERENCE
using-constant-test:35:3:35:8::Using a conditional statement with a constant value:INFERENCE
using-constant-test:38:3:38:4::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:41:3:41:7::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:44:3:44:5::Using a conditional statement with a constant value:UNDEFINED
Expand All @@ -14,14 +14,15 @@ using-constant-test:59:3:59:12::Using a conditional statement with a constant va
using-constant-test:62:3:62:5::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:65:3:65:12::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:68:3:68:5::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:73:3:73:12::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:73:3:73:12::Using a conditional statement with a constant value:INFERENCE
using-constant-test:76:8:76:9::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:80:36:80:39:test_comprehensions:Using a conditional statement with a constant value:UNDEFINED
using-constant-test:80:36:80:39:test_comprehensions:Using a conditional statement with a constant value:INFERENCE
using-constant-test:81:36:81:37:test_comprehensions:Using a conditional statement with a constant value:UNDEFINED
using-constant-test:82:36:82:39:test_comprehensions:Using a conditional statement with a constant value:UNDEFINED
using-constant-test:82:36:82:39:test_comprehensions:Using a conditional statement with a constant value:INFERENCE
using-constant-test:83:36:83:37:test_comprehensions:Using a conditional statement with a constant value:UNDEFINED
using-constant-test:84:36:84:39:test_comprehensions:Using a conditional statement with a constant value:UNDEFINED
using-constant-test:85:39:85:42:test_comprehensions:Using a conditional statement with a constant value:UNDEFINED
using-constant-test:89:3:89:15::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:93:3:93:18::Using a conditional statement with a constant value:UNDEFINED
using-constant-test:84:36:84:39:test_comprehensions:Using a conditional statement with a constant value:INFERENCE
using-constant-test:85:39:85:42:test_comprehensions:Using a conditional statement with a constant value:INFERENCE
using-constant-test:89:3:89:15::Using a conditional statement with a constant value:INFERENCE
using-constant-test:93:3:93:18::Using a conditional statement with a constant value:INFERENCE
comparison-of-constants:117:3:117:8::"Comparison between constants: '2 < 3' has a constant value":HIGH
using-constant-test:156:0:157:8::Using a conditional statement with a constant value:INFERENCE