Skip to content

Commit e368844

Browse files
committed
Python: Also exclude class scope
Changing the `locals()` dictionary actually _does_ change the attributes of the class being defined, so we shouldn't alert in this case.
1 parent 8d79248 commit e368844

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

python/ql/src/Statements/ModificationOfLocals.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ where
3737
// in module level scope `locals() == globals()`
3838
// see https://docs.python.org/3/library/functions.html#locals
3939
// FP report in https://github.com/github/codeql/issues/6674
40-
not a.getScope() instanceof Module
40+
not a.getScope() instanceof Module and
41+
// in class level scope `locals()` reflects the class namespace,
42+
// so modifications do take effect.
43+
not a.getScope() instanceof Class
4144
select a, "Modification of the locals() dictionary will have no effect on the local variables."

python/ql/test/query-tests/Statements/general/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,9 @@ def assert_ok(seq):
174174
# False positive. ODASA-8042. Fixed in PR #2401.
175175
class false_positive:
176176
e = (x for x in [])
177+
178+
# In class-level scope `locals()` reflects the class namespace,
179+
# so modifications do take effect.
180+
class MyClass:
181+
locals()['x'] = 43 # OK
182+
y = x

0 commit comments

Comments
 (0)