Aller au contenu

Mutable global state (ARCH2187)

Ce contenu n’est pas encore disponible dans votre langue.

Key python.quality.mutable-global-state · Severity warning · Category quality

Module-level mutable global state creates hidden dependencies, is unsafe under concurrent access, and makes tests fragile. Encapsulate the state inside a function or class.

cache = {}
def get(key):
if key not in cache:
cache[key] = load(key)
return cache[key]
class Repository:
def __init__(self):
self._cache = {}
def get(self, key):
if key not in self._cache:
self._cache[key] = load(key)
return self._cache[key]

Encapsulate mutable state in a class instance and inject it explicitly.