When adding or using caching (e.g., @cached_property, sys.modules-dependent behavior, or internal resolution caches), ensure the cached value depends only on inputs that are already stable.

Standard rules: 1) Don’t cache too early

2) Make runtime-dependent caches test-safe

Example (test isolation for import simulation):

import builtins
from unittest.mock import patch

def failing_import(name, *args, **kwargs):
    if name.startswith('gevent'):
        raise ImportError('Simulated gevent import failure')
    return real_import(name, *args, **kwargs)

real_import = builtins.__import__

with patch.dict('sys.modules', {'gevent': None, 'gevent.monkey': None}):
    with patch('builtins.__import__', side_effect=failing_import):
        result = is_gevent_monkey_patched()
        assert result is False

If you can’t guarantee stable inputs or reliable test isolation, prefer non-cached access (or provide explicit cache invalidation hooks) rather than relying on implicit caching side effects.