Skip to content

Commit 1d25184

Browse files
committed
Python: Add test for type-tracking through decorators
In general, if there is _some_ decorator on a function, it might not be safe to track content out of it (since the decorator could do anything), but in this case, we can see what the decorator does, so we should be able to handle it (but we don't right now). By my understanding of how type-tracking works, if we track content through `my_decorator`, then we would also track content to the result of `unrelated_func()`, which I wanted to make sure our tests would catch. I found out the core of the problem seems to come from our lack of being able to track to the inner scope, and added an explicit test for that.
1 parent 8a2e063 commit 1d25184

File tree

1 file changed

+35
-0
lines changed
  • python/ql/test/experimental/dataflow/typetracking

1 file changed

+35
-0
lines changed

python/ql/test/experimental/dataflow/typetracking/test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,41 @@ def test_import():
5656
y # $tracked
5757
mymodule.z # $tracked
5858

59+
60+
def to_inner_scope():
61+
x = tracked # $tracked
62+
def foo():
63+
y = x # $ MISSING: tracked
64+
return y # $ MISSING: tracked
65+
also_x = foo() # $ MISSING: tracked
66+
print(also_x) # $ MISSING: tracked
67+
68+
69+
def my_decorator(func):
70+
# This part doesn't make any sense in a normal decorator, but just shows how we
71+
# handle type-tracking
72+
73+
func() # $tracked
74+
75+
def wrapper():
76+
print("before function call")
77+
val = func() # $ MISSING: tracked
78+
print("after function call")
79+
return val # $ MISSING: tracked
80+
return wrapper
81+
82+
@my_decorator
83+
def get_tracked2():
84+
return tracked # $tracked
85+
86+
@my_decorator
87+
def unrelated_func():
88+
return "foo"
89+
90+
def use_funcs_with_decorators():
91+
x = get_tracked2() # $ MISSING: tracked
92+
y = unrelated_func()
93+
5994
# ------------------------------------------------------------------------------
6095

6196
def expects_int(x): # $int

0 commit comments

Comments
 (0)