-
-
Notifications
You must be signed in to change notification settings - Fork 200
DeprecationWarning still emitted for short CACHE_TYPE names in v2.3.1 #632
Description
Description
When using short CACHE_TYPE names like "simple", "redis", "null", etc., Flask-Caching emits a DeprecationWarning:
/opt/hostedtoolcache/Python/3.12.12/x64/lib/python3.12/site-packages/flask_caching/__init__.py:145: DeprecationWarning:
Using the initialization functions in flask_caching.backend is deprecated. Use the a full path to backend classes directly.
This warning has been present since at least 2021 (see #248, which was closed without a code change). As of v2.3.1, the warning is still emitted and the initialization functions in flask_caching.backends.__init__ are still in place.
Steps to Reproduce
from flask import Flask
from flask_caching import Cache
app = Flask(__name__)
app.config["CACHE_TYPE"] = "simple"
cache = Cache(app)Running the above with -W all or in a test suite that captures warnings will show the DeprecationWarning.
Root Cause
In _set_cache() (src/flask_caching/__init__.py), when CACHE_TYPE has no dot, the code prepends "flask_caching.backends." and imports the result. If the imported object is a function (e.g., flask_caching.backends.simple) rather than a BaseCache subclass, the deprecation warning fires.
The short names resolve to wrapper functions defined in flask_caching/backends/__init__.py (e.g., def simple(app, config, args, kwargs)), which simply delegate to the class's .factory() method.
Issues
- The deprecation has been pending for ~4 years without the deprecated code path being removed or a migration timeline being communicated.
- The warning message contains a typo: "Use the a full path" should be "Use a full path".
- The warning is confusing for users who set
CACHE_TYPE = "SimpleCache"thinking the short class name is the "full path" — the message doesn't clearly explain what the expected value should be.
Suggested Resolution
One of the following approaches:
Option A: Remove the deprecated functions
Remove the wrapper functions from flask_caching/backends/__init__.py and update _set_cache() so that short names like "simple" map directly to the class (e.g., SimpleCache) rather than the wrapper function. This would eliminate the warning while preserving backward compatibility for short names.
Option B: Improve the warning message
If the intent is to eventually require fully-qualified class paths, at minimum:
- Fix the typo ("the a" → "a")
- Include the recommended replacement value in the warning, e.g.:
DeprecationWarning: Using CACHE_TYPE='simple' is deprecated. Use CACHE_TYPE='SimpleCache' or the full path 'flask_caching.backends.simplecache.SimpleCache' instead.
Option C: Just remove the warning
If short names will continue to be supported indefinitely, remove the DeprecationWarning since the wrapper functions work correctly and there's no concrete plan to remove them.
Environment
- Flask-Caching version: 2.3.1
- Python version: 3.12.12
- Flask version: 3.x