Python · maintainability
prefer-immutable-module-constant
python:prefer-immutable-module-constant module-level constant collections expose mutable shared state; use tuple, frozenset, or an immutable mapping
- Code
- SARJ096
- Default
- error
- Fix
- none
- Languages
- python
Why
A constant-looking mutable collection can be changed by any importer, so its value depends on process history rather than the module's source.
Fix
Use a tuple for ordered values, a frozenset for membership, or an immutable mapping for keyed values.
Before / after
Executed by this rule’s unit tests.
Before
Mutable collection exposed as a module constant
ROLE_NAMES = ["admin", "member"]
After
Immutable tuple used for a module constant
ROLE_NAMES = ("admin", "member")
Limits
- Empty collections and collections intentionally mutated or passed to unknown calls are not reported.
- Test and generated files are excluded.