Skip to content
Sarj Code Standards

no-shared-mutable-default

Do not share mutable function defaults through aliases or read-only annotations.

Why

A read-only annotation does not make a list or dictionary immutable, and a named mutable default still shares one object between calls.

Fix

Use an immutable default or accept None and create the mutable value inside the function. Keep an intentional shared cache in an explicit owner.

Examples

Before — flagged A Sequence annotation still shares a mutable list
service/batch.py
from collections.abc import Sequence
def batch(values: Sequence[int] = []):
return values
After — preferred An immutable tuple can be shared safely
service/batch.py
from collections.abc import Sequence
def batch(values: Sequence[int] = ()):
return values