Python: Remove impossible flow for **kwargs params

This commit is contained in:
Rasmus Wriedt Larsen
2022-09-09 13:53:01 +02:00
parent eb600f07b7
commit 503ad544e9
2 changed files with 25 additions and 1 deletions

View File

@@ -54,6 +54,28 @@ class SyntheticPreUpdateNode extends Node, TSyntheticPreUpdateNode {
override Location getLocation() { result = node.getLocation() } override Location getLocation() { result = node.getLocation() }
} }
/**
* Ensures that the a `**kwargs` parameter will not contain elements with names of
* keyword parameters.
*
* For example, for the function below, it's not possible that the `kwargs` dictionary
* can contain an element with the name `a`, since that parameter can be given as a
* keyword argument.
*
* ```py
* def func(a, **kwargs):
* ...
* ```
*/
private predicate dictSplatParameterNodeClearStep(ParameterNode n, DictionaryElementContent c) {
exists(DataFlowCallable callable, ParameterPosition dictSplatPos, ParameterPosition keywordPos |
dictSplatPos.isDictSplat() and
n = callable.getParameter(dictSplatPos) and
exists(callable.getParameter(keywordPos)) and
keywordPos.isKeyword(c.getKey())
)
}
abstract class PostUpdateNodeImpl extends Node { abstract class PostUpdateNodeImpl extends Node {
/** Gets the node before the state update. */ /** Gets the node before the state update. */
abstract Node getPreUpdateNode(); abstract Node getPreUpdateNode();
@@ -673,6 +695,8 @@ predicate clearsContent(Node n, Content c) {
attributeClearStep(n, c) attributeClearStep(n, c)
or or
FlowSummaryImpl::Private::Steps::summaryClearsContent(n, c) FlowSummaryImpl::Private::Steps::summaryClearsContent(n, c)
or
dictSplatParameterNodeClearStep(n, c)
} }
/** /**

View File

@@ -198,5 +198,5 @@ def test_mixed():
args = {"b": arg2, "c": "safe"} # $ arg2 func=mixed args = {"b": arg2, "c": "safe"} # $ arg2 func=mixed
mixed(a=arg1, **args) # $ arg1 mixed(a=arg1, **args) # $ arg1
args = {"a": arg1, "b": arg2, "c": "safe"} # $ bad1="arg1" arg2 func=mixed args = {"a": arg1, "b": arg2, "c": "safe"} # $ arg2 func=mixed MISSING: arg1
mixed(**args) mixed(**args)