Files
codeql/python/ql/test/library-tests/ControlFlow/evaluation-order/test_unpacking.py
Taus 71cd5be513 Python: Add self-validating CFG tests
These tests consist of various Python constructions (hopefully a
somewhat comprehensive set) with specific timestamp annotations
scattered throughout. When the tests are run using the Python 3
interpreter, these annotations are checked and compared to the "current
timestamp" to see that they are in agreement. This is what makes the
tests "self-validating".

There are a few different kinds of annotations: the basic `t[4]` style
(meaning this is executed at timestamp 4), the `t[dead(4)]` variant
(meaning this _would_ happen at timestamp 4, but it is in a dead
branch), and `t[never]` (meaning this is never executed at all).

In addition to this, there is a query, MissingAnnotations, which checks
whether we have applied these annotations maximally. Many expression
nodes are not actually annotatable, so there is a sizeable list of
excluded nodes for that query.
2026-05-12 12:42:29 +00:00

49 lines
1.1 KiB
Python

"""Unpacking and star expressions evaluation order."""
from timer import test
@test
def test_tuple_unpack(t):
"""RHS expression evaluates, then unpacking assigns targets."""
a, b = (1 @ t[0], 2 @ t[1]) @ t[2]
x = (a @ t[3] + b @ t[4]) @ t[5]
@test
def test_list_unpack(t):
"""List unpacking: RHS elements left to right, then unpack."""
[a, b] = [1 @ t[0], 2 @ t[1]] @ t[2]
x = (a @ t[3] + b @ t[4]) @ t[5]
@test
def test_star_unpack(t):
"""Star unpacking: RHS evaluates first."""
a, *b = [1 @ t[0], 2 @ t[1], 3 @ t[2], 4 @ t[3]] @ t[4]
x = (a @ t[5], b @ t[6]) @ t[7]
@test
def test_nested_unpack(t):
"""Nested unpacking: RHS evaluates first."""
(a, b), c = ((1 @ t[0], 2 @ t[1]) @ t[2], 3 @ t[3]) @ t[4]
x = ((a @ t[5] + b @ t[6]) @ t[7] + c @ t[8]) @ t[9]
@test
def test_swap(t):
a = 1 @ t[0]
b = 2 @ t[1]
a, b = (b @ t[2], a @ t[3]) @ t[4]
x = a @ t[5]
y = b @ t[6]
@test
def test_unpack_for(t):
pairs = [(1 @ t[0], 2 @ t[1]) @ t[2], (3 @ t[3], 4 @ t[4]) @ t[5]] @ t[6]
for a, b in pairs @ t[7]:
x = a @ t[8, 10]
y = b @ t[9, 11]