Skip to content

Commit 1989177

Browse files
authored
Fix #821: Improvements to errors/warnings when dynamic Backend class lookups fail (#834)
* Raise Attribute error from __getattr__ as python docs recommend: https://docs.python.org/3/reference/datamodel.html#module.__getattr__ * Increase warning stacklevel by 1 so warning comes from the code doing the importing.
1 parent 3f00c03 commit 1989177

2 files changed

Lines changed: 18 additions & 28 deletions

File tree

dramatiq/rate_limits/backends/__init__.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,23 @@ def import_memcached():
3737
from .memcached import MemcachedBackend
3838

3939
return MemcachedBackend
40-
except ModuleNotFoundError:
41-
warnings.warn(
42-
"MemcachedBackend is not available. Run `pip install dramatiq[memcached]` "
43-
"to add support for that backend.",
44-
category=ImportWarning,
45-
stacklevel=2,
40+
except ModuleNotFoundError as e:
41+
message = (
42+
"MemcachedBackend is not available. Run `pip install dramatiq[memcached]` to add support for that backend."
4643
)
47-
raise
44+
warnings.warn(message, category=ImportWarning, stacklevel=3)
45+
raise AttributeError(message) from e
4846

4947

5048
def import_redis():
5149
try:
5250
from .redis import RedisBackend
5351

5452
return RedisBackend
55-
except ModuleNotFoundError:
56-
warnings.warn(
57-
"RedisBackend is not available. Run `pip install dramatiq[redis]` to add support for that backend.",
58-
category=ImportWarning,
59-
stacklevel=2,
60-
)
61-
raise
53+
except ModuleNotFoundError as e:
54+
message = "RedisBackend is not available. Run `pip install dramatiq[redis]` to add support for that backend."
55+
warnings.warn(message, category=ImportWarning, stacklevel=3)
56+
raise AttributeError(message) from e
6257

6358

6459
_module_importers = {

dramatiq/results/backends/__init__.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,23 @@ def import_memcached():
3737
from .memcached import MemcachedBackend
3838

3939
return MemcachedBackend
40-
except ModuleNotFoundError:
41-
warnings.warn(
42-
"MemcachedBackend is not available. Run `pip install dramatiq[memcached]` "
43-
"to add support for that backend.",
44-
category=ImportWarning,
45-
stacklevel=2,
40+
except ModuleNotFoundError as e:
41+
message = (
42+
"MemcachedBackend is not available. Run `pip install dramatiq[memcached]` to add support for that backend."
4643
)
47-
raise
44+
warnings.warn(message, category=ImportWarning, stacklevel=3)
45+
raise AttributeError(message) from e
4846

4947

5048
def import_redis():
5149
try:
5250
from .redis import RedisBackend
5351

5452
return RedisBackend
55-
except ModuleNotFoundError:
56-
warnings.warn(
57-
"RedisBackend is not available. Run `pip install dramatiq[redis]` to add support for that backend.",
58-
category=ImportWarning,
59-
stacklevel=2,
60-
)
61-
raise
53+
except ModuleNotFoundError as e:
54+
message = "RedisBackend is not available. Run `pip install dramatiq[redis]` to add support for that backend."
55+
warnings.warn(message, category=ImportWarning, stacklevel=3)
56+
raise AttributeError(message) from e
6257

6358

6459
_module_importers = {

0 commit comments

Comments
 (0)