diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2021-09-06 17:24:17 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-09-06 18:03:41 -0500 |
commit | a5396446b772079905f4c99d319c86bedc135c78 (patch) | |
tree | 7549d087a980348064ab317cf3cd31c5aa91ee55 | |
parent | 1dd8b699624753f6550fe93bdab2b93984e688d3 (diff) | |
download | requests-cache-a5396446b772079905f4c99d319c86bedc135c78.tar.gz |
Add note about 'cache_name' argument usage in init_backend()
-rw-r--r-- | requests_cache/backends/__init__.py | 20 | ||||
-rw-r--r-- | requests_cache/session.py | 2 |
2 files changed, 11 insertions, 11 deletions
diff --git a/requests_cache/backends/__init__.py b/requests_cache/backends/__init__.py index 2162551..8c3b413 100644 --- a/requests_cache/backends/__init__.py +++ b/requests_cache/backends/__init__.py @@ -2,7 +2,7 @@ # flake8: noqa: F401 from inspect import signature from logging import getLogger -from typing import Callable, Dict, Iterable, Type, Union +from typing import Callable, Dict, Iterable, Optional, Type, Union from .. import get_placeholder_class, get_valid_kwargs from .base import BaseCache, BaseStorage @@ -72,21 +72,21 @@ BACKEND_CLASSES = { } -def init_backend(backend: BackendSpecifier = None, *args, **kwargs) -> BaseCache: +def init_backend(cache_name: str, backend: Optional[BackendSpecifier], **kwargs) -> BaseCache: """Initialize a backend from a name, class, or instance""" - logger.debug(f'Initializing backend: {backend}') + logger.debug(f'Initializing backend: {backend} {cache_name}') - # Omit 'cache_name' positional arg if an equivalent backend-specific kwarg is specified - # TODO: The difference in parameter names here can be problematic. A better solution for this - # would be nice, if it can be done without breaking backwards-compatibility. - if any([k in kwargs for k in CACHE_NAME_KWARGS]): - args = tuple() + # The 'cache_name' arg has a different purpose depending on the backend. If an equivalent + # backend-specific keyword arg is specified, handle that here to avoid conflicts. A consistent + # positional-only or keyword-only arg would be better, but probably not worth a breaking change. + cache_name_kwargs = [kwargs.pop(k) for k in CACHE_NAME_KWARGS if k in kwargs] + cache_name = cache_name or cache_name_kwargs[0] # Determine backend class if isinstance(backend, BaseCache): return backend elif isinstance(backend, type): - return backend(*args, **kwargs) + return backend(cache_name, **kwargs) elif not backend: backend = 'sqlite' if BACKEND_CLASSES['sqlite'] else 'memory' @@ -94,4 +94,4 @@ def init_backend(backend: BackendSpecifier = None, *args, **kwargs) -> BaseCache if backend not in BACKEND_CLASSES: raise ValueError(f'Invalid backend: {backend}. Choose from: {BACKEND_CLASSES.keys()}') - return BACKEND_CLASSES[backend](*args, **kwargs) + return BACKEND_CLASSES[backend](cache_name, **kwargs) diff --git a/requests_cache/session.py b/requests_cache/session.py index 0fc1a55..4c6c1b2 100644 --- a/requests_cache/session.py +++ b/requests_cache/session.py @@ -57,7 +57,7 @@ class CacheMixin(MIXIN_BASE): stale_if_error: bool = False, **kwargs, ): - self.cache = init_backend(backend, cache_name, **kwargs) + self.cache = init_backend(cache_name, backend, **kwargs) self.allowable_codes = allowable_codes self.allowable_methods = allowable_methods self.expire_after = expire_after |