Skip to main content

SKY-L034 · Logic and resilience

Repeated mutable alias

Medium

A list built by repeating a list that holds a mutable value, such as [[None] * cols] * rows or [{}] * 3. The * copies references, not values, so every row is the same inner list.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
defect
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A change to one element shows up in all of them. In the example below, booking seat 3 in row 0 books seat 3 in every row, and the venue shows as sold out after a handful of sales.

What Skylos flags

venues/seating.py · PythonReported on line 5
"""Seat maps for a venue."""  def empty_seat_map(rows, seats_per_row):    return [[None] * seats_per_row] * rows  def book(seat_map, row, seat, ticket_id):    seat_map[row][seat] = ticket_id

What Skylos leaves alone

venues/seating.py · Python
"""Seat maps for a venue."""  def empty_row(seats_per_row):    return [None] * seats_per_row  def book(row_map, seat, ticket_id):    row_map[seat] = ticket_id

Why Skylos leaves it alone

The repeated element is None, which cannot be changed in place. row_map[seat] = ticket_id replaces one slot and leaves the others alone, so sharing the same None is harmless.

How Skylos decides

  1. Runs with --quality on Python files. Looks at every * whose one side is a list or tuple literal (or a + or * of such literals) and whose other side is the repeat count.
  2. The sequence must provably contain a mutable element: a list, dict or set literal or comprehension, a call to builtin list(), dict() or set() that is not shadowed in the file, a list repetition such as [None] * cols, or a tuple holding one of these.
  3. The count must be able to be 2 or more. A literal 0 or 1, a comparison, a not expression and a builtin bool() result are skipped; an unknown name such as rows counts.
  4. Reports MEDIUM on the line of the * expression.

How to fix it

Build each inner list separately with a comprehension, so every row is its own object.

venues/seating.py · Python · the fix, against the flagged file
"""Seat maps for a venue."""  def empty_seat_map(rows, seats_per_row):Removed:     return [[None] * seats_per_row] * rowsAdded:     return [[None] * seats_per_row for _ in range(rows)]  def book(seat_map, row, seat, ticket_id):    seat_map[row][seat] = ticket_id

When it can be wrong

  • A grid that is only ever read, never changed in place, is still reported; the sharing is harmless there.
  • It misses repetition of a variable that holds a list, such as row = [] followed by [row] * 3, because only literals and builtin calls written in place are proven mutable.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the line of the * expression.

    Python
    EMPTY = [[]] * 4  # skylos: ignore[SKY-L034] read-only sentinel rows
  • [tool.skylos] ignore = ["SKY-L034"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.