Lightning-AI/pytorch-lightning · warning · NotImplementedError

__setitem__ is not supported

Error message

__setitem__ is not supported

What it means

_Monotonic overrides list to enforce strictly increasing values; item assignment cannot be validated cheaply, so __setitem__ deliberately raises NotImplementedError. Users are meant to only append values, never mutate history.

Source

Thrown at src/lightning/fabric/utilities/throughput.py:719

    def last(self) -> Optional[T]:
        if len(self) > 0:
            return self[-1]
        return None

    @override
    def append(self, x: T) -> None:
        last = self.last
        if last is not None and last >= x:
            raise ValueError(f"Expected the value to increase, last: {last}, current: {x}")
        list.append(self, x)
        # truncate excess
        if len(self) > self.maxlen:
            del self[0]

    @override
    def __setitem__(self, key: Any, value: Any) -> None:
        # assigning is not implemented since we don't use it. it could be by checking all previous values
        raise NotImplementedError("__setitem__ is not supported")

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Rebuild the container by appending values in increasing order instead of assigning by index
  2. If you need a mutable history, copy the values into a plain list: list(monitor._elapsed_interval)
  3. Avoid touching private attributes prefixed with underscore

Example fix

# before
monitor._elapsed_interval[0] = 1.0

# after
values = sorted(list(monitor._elapsed_interval))
values[0] = 1.0
new_list = _Monotonic(maxlen=len(values))
for v in sorted(values):
    new_list.append(v)
Defensive patterns

Strategy: validation

Validate before calling

# nothing to validate: simply never assign into the container
values = list(monotonic_list)  # copy out if you need to edit

Prevention

When it happens

Trigger: Directly indexing the internal list used by ThroughputMonitor (e.g. monitor._elapsed_interval[i] = value) or any code that treats the container as a plain list and assigns by index or slice.

Common situations: Test helpers or monkeypatching that try to rewrite recorded values; copy/deepcopy or serialization code that repopulates via item assignment rather than append; debugging code that edits history.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/8225afa7790e0f72. Report an issue: GitHub.