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

Python: Modernize py/mixed-tuple-returns #19136

Merged
merged 5 commits into from
Apr 1, 2025
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
15 changes: 10 additions & 5 deletions python/ql/src/Functions/ReturnConsistentTupleSizes.ql
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
* @kind problem
* @tags reliability
* maintainability
* quality
* @problem.severity recommendation
* @sub-severity high
* @precision high
* @id py/mixed-tuple-returns
*/

import python
import semmle.python.ApiGraphs

predicate returns_tuple_of_size(Function func, int size, AstNode origin) {
exists(Return return, TupleValue val |
predicate returns_tuple_of_size(Function func, int size, Tuple tuple) {
exists(Return return, DataFlow::Node value |
value.asExpr() = return.getValue() and
return.getScope() = func and
return.getValue().pointsTo(val, origin)
any(DataFlow::LocalSourceNode n | n.asExpr() = tuple).flowsTo(value)
|
size = val.length()
size = count(int n | exists(tuple.getElt(n)))
)
}

from Function func, int s1, int s2, AstNode t1, AstNode t2
where
returns_tuple_of_size(func, s1, t1) and
returns_tuple_of_size(func, s2, t2) and
s1 < s2
s1 < s2 and
// Don't report on functions that have a return type annotation
not exists(func.getDefinition().(FunctionExpr).getReturns())
select func, func.getQualifiedName() + " returns $@ and $@.", t1, "tuple of size " + s1, t2,
"tuple of size " + s2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
category: minorAnalysis
---

- The `py/mixed-tuple-returns` query no longer flags instances where the tuple is passed into the function as an argument, as this led to too many false positives.
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
| functions_test.py:306:1:306:39 | Function returning_different_tuple_sizes | returning_different_tuple_sizes returns $@ and $@. | functions_test.py:308:16:308:18 | Tuple | tuple of size 2 | functions_test.py:310:16:310:20 | Tuple | tuple of size 3 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might like to put some comments in the tests explaining the expected results.
(in particular that this case no longer gives an alert)

Copy link
Contributor Author

@tausbn tausbn Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I have done so in 6674288.

| functions_test.py:324:1:324:50 | Function indirectly_returning_different_tuple_sizes | indirectly_returning_different_tuple_sizes returns $@ and $@. | functions_test.py:319:12:319:14 | Tuple | tuple of size 2 | functions_test.py:322:12:322:16 | Tuple | tuple of size 3 |
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def function_returning_2_tuple():
def function_returning_3_tuple():
return 1,2,3

def indirectly_returning_different_tuple_sizes(x):
def indirectly_returning_different_tuple_sizes(x): # OK, since we only look at local tuple returns
if x:
return function_returning_2_tuple()
else:
Expand All @@ -347,3 +347,9 @@ def ok_match2(x): # FP
return 0
case _:
return 1

def ok_tuple_returns_captured_in_type(x: bool) -> tuple[int, ...]: # OK because there is a type annotation present
if x:
return 1, 2
else:
return 1, 2, 3